@Documented
@Retention(RUNTIME)
@Target({TYPE,METHOD})
@Repeatable(List.class)
public @interface PermissionsAllowed
Lists one or more required permissions that must be granted.
-
Nested Class Summary
Nested Classes -
Required Element Summary
Required Elements -
Optional Element Summary
Optional ElementsModifier and TypeOptional ElementDescriptionbooleanChoose a relation between multiple permissions specified invalue().String[]Mark parameters of the annotated method that should be passed to the constructor of thepermission().Class<? extends Permission>The class that extends thePermissionclass to create a permission specified invalue(). -
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final StringConstant value forparams()indicating that the constructor parameters of thepermission()should be autodetected based on formal parameter names.static final StringColon is used to separate aPermission.getName()and an element of thePermission.getActions().
-
Field Details
-
AUTODETECTED
Constant value forparams()indicating that the constructor parameters of thepermission()should be autodetected based on formal parameter names. For example, consider the following method secured with this annotation:
The@PermissionsAllowed(value = "resource:retrieve", permission = UserPermission.class) public Resource getResource(String param1, String param2, String param3) { // business logic }getResourcemethod parametersparam1andparam3will be matched with theUserPermissionconstructor parametersparam1andparam3.
If no method parameter name matches the constructor parameter name, Quarkus checks names of fields and methods declared on the method parameter type. For example:public class UserPermission extends Permission { public UserPermission(String name, String param3, String param1) { ... } ... }
In this example, resolution of therecord BeanParam2(String param1, String param2) {} record BeanParam3(String param3) {} record BeanParam1(BeanParam2 beanParam2, BeanParam3 beanParam3) { } @PermissionsAllowed(value = "resource:retrieve", permission = UserPermission.class) public Resource getResource(BeanParam1 beanParam) { // business logic }param1andparam3formal parameters is unambiguous. For more complex scenarios, we suggest to specifyparams()explicitly.- See Also:
-
PERMISSION_TO_ACTION_SEPARATOR
Colon is used to separate aPermission.getName()and an element of thePermission.getActions(). For example,StringPermissioncreated for the 'getResource' method:
is equal to the@PermissionsAllowed("resource:retrieve") public Resource getResource() { // business logic }perm:var perm = new StringPermission("resource", "retrieve");- See Also:
-
-
Element Details
-
value
String[] valueSpecifies a list of permissions that grants access to the resource. It is also possible to define permission actions that are permitted for the resource. Consider the `getResource` method:
Two@PermissionsAllowed({"resource:crud", "resource:retrieve", "system-resource:retrieve"}) public Resource getResource() { // business logic }StringPermissionpermissions will be created:
The permission check will pass if eithervar pem1 = new StringPermission("resource", "crud", "retrieve"); var pem2 = new StringPermission("system-resource", "retrieve");pem1orpem2implies user permissions. It is also possible to combine permissions with and without actions like this:
Quarkus will create two permissions:@PermissionsAllowed({"resource:crud", "resource:retrieve", "natural-resource"}) public Resource getResource() { // business logic }
Alternatively, when multiple required permissions must be listed, you can repeat the annotation, for example:var pem1 = new StringPermission("resource", "crud", "retrieve"); var pem2 = new StringPermission("natural-resource");@PermissionsAllowed("create") @PermissionsAllowed("update") public Resource createOrUpdate(Long id) { // business logic }- Returns:
- permissions
- See Also:
-
-
-
inclusive
boolean inclusiveChoose a relation between multiple permissions specified invalue(). By default, at least one of permissions must be granted. You can request that all listed permissions by setting the `inclusive` property to `true`. For example:
Two@PermissionsAllowed(value = {"resource:crud", "resource:retrieve", "natural-resource"}, inclusive = true) public Resource getResource() { // business logic }StringPermissions will be created:
And the permission check will pass if bothvar pem1 = new StringPermission("resource", "crud", "retrieve"); var pem2 = new StringPermission("system-resource", "retrieve");pem1andpem2imply user permissions.- Returns:
- `true` if permissions should be inclusive
- Default:
- false
-
params
String[] paramsMark parameters of the annotated method that should be passed to the constructor of thepermission(). Consider the following three classes:
Next, consider the secured 'getResource' method:class ResourceIdentity { } class User extends ResourceIdentity { } class Admin extends ResourceIdentity { }
In the example above, the parameters@PermissionsAllowed(permission = UserPermission.class, value = "resource", params = {user1, admin1}) public Resource getResource(User user, User user1, Admin admin, Admin admin1) { // business logic }user1andadmin1are marked aspermission()constructor arguments:
Please note that:public class UserPermission extends Permission { private final ResourceIdentity user; private final ResourceIdentity admin; public UserPermission(String name, ResourceIdentity user1, ResourceIdentity admin1) { super(name); this.user = user1; this.admin = admin1; } ... }- The constructor parameter names
user1andadmin1must match respectivePermissionsAllowed#params - `ResourceIdentity` can also be used as a constructor parameter data type
The corresponding@PermissionsAllowed(permission = UserPermission.class, value = "resource", params = {"admin1.param1", "user1.param3"}) public Resource getResource(User user, User user1, Admin admin, Admin admin1) { // business logic } class ResourceIdentity { private final String param1; public String getParam1() { return param1; } } class User extends ResourceIdentity { public String getParam3() { return "param3"; } } class Admin extends ResourceIdentity { }UserPermissionconstructor would look like this:
The constructor parameterpublic class UserPermission extends Permission { public UserPermission(String name, String param1, String param3) { } ... }param1refers to theadmin1#param1secured method parameter and the constructor parameterparam3refers to theuser1#getParam3secured method parameter.- Returns:
- constructor parameters passed to the
permission() - See Also:
- Default:
- {"<<autodetected>>"}
- The constructor parameter names
-
permission
Class<? extends Permission> permissionThe class that extends thePermissionclass to create a permission specified invalue(). For example:public class UserPermission extends Permission { private final String[] permissions; public UserPermission(String name, String... actions) { super(name); this.actions = actions; } ... }actionsparameter is optional and may be omitted.- Returns:
- permission class
- Default:
- io.quarkus.security.StringPermission.class
-