Annotation Interface ForbidsAnyRole
@Documented
@Target({TYPE,CONSTRUCTOR,METHOD,ANNOTATION_TYPE,PACKAGE})
@Retention(RUNTIME)
@Inherited
public @interface ForbidsAnyRole
Indicates that a handler method, class, package, or payload should not be invoked or processed if the current user
has any of the specified roles.
Usage
The same policy applies to handler methods, classes, packages, and payloads. With a nonempty role list, an absent user causesUnauthenticatedException; a user with a forbidden role causes UnauthorizedException.
With throwIfUnauthorized() set to false, the handler or payload is skipped instead.
Other eligible handlers can respond, but a denied payload is not passed to any handler.
This is useful in scenarios where certain roles (e.g. admins) should receive different behavior or be excluded from specific processing paths.
Example: Preventing admin users from handling a message
@ForbidsAnyRole("admin")
@HandleCommand
void handle(UserRequest request) {
}
Notes
- Role checks are performed via the configured
UserProvider. - An authenticated user without any forbidden role passes this check; anonymous access is not granted.
- This annotation supports usage as a meta-annotation for reusable constraints.
- See Also:
-
Optional Element Summary
Optional ElementsModifier and TypeOptional ElementDescriptionbooleanDetermines whether an exception should be thrown when the authorization check fails.String[]One or more roles that should be excluded.
-
Element Details
-
value
String[] valueOne or more roles that should be excluded. If the user has any of these roles, the handler or payload will not be used. An empty list allows every authenticated user and silently skips anonymous users, regardless ofthrowIfUnauthorized().- Default:
{}
-
throwIfUnauthorized
boolean throwIfUnauthorizedDetermines whether an exception should be thrown when the authorization check fails.If
true(the default), an exception will be thrown when the user is unauthorized:UnauthenticatedException– if no authenticated user is present and the role list is nonemptyUnauthorizedException– if the user is present but has forbidden roles
If
false, the annotated handler or message will be silently skipped instead. This opt-out strategy is useful for conditionally invoked handlers where fallback behavior is preferred.Defaults to
true.- Default:
true
-