Annotation 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 causes UnauthenticatedException; 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 Elements
    Modifier and Type
    Optional Element
    Description
    boolean
    Determines whether an exception should be thrown when the authorization check fails.
    One or more roles that should be excluded.
  • Element Details

    • value

      String[] value
      One 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 of throwIfUnauthorized().
      Default:
      {}
    • throwIfUnauthorized

      boolean throwIfUnauthorized
      Determines 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:

      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