How to: authorize REST API access with roles
jEAP supports two role models for authorizing access to a REST API: simple roles and semantic roles. Pick the model your application uses, then follow the matching steps below.
Steps: authorize with simple roles
-
Complete the basic integration described in Authentication and authorization for REST APIs – Integration. Leave
jeap.security.oauth2.resourceserver.system-nameunset — setting it activates authorization with semantic roles instead. -
Check a role declaratively in the SpEL expressions of the
@PreAuthorizeand@PostAuthorizeannotations:@PreAuthorize("hasRole('role')")public Data getData(...) {...@PreAuthorize("hasRoleForPartner('role', #partnerId)")public Partner getPartner(String partnerId) {...Note: Unfortunately, IntelliJ cannot recognize these additional methods. Auto-completion etc. is therefore not available inside the SpEL string (see the bug report in IntelliJ).
-
Or, when the check cannot be expressed declaratively — for example because the data has to be loaded first — check a role programmatically through the
ServletSimpleAuthorization(WebMvc) Spring bean:import ch.admin.bit.jeap.security.resource.authentication.ServletSimpleAuthorization;...private ServletSimpleAuthorization jeapAuthorization;...public Partner findPartner(...) {Partner partner = ...//Throw an exception if the user is not allowed to access the partnerif (!jeapAuthorization.hasRoleForPartner("role", partner.getPartnerId())) {throw new AccessDeniedException("Missing role for partner with id '" + partner.getPartnerId());}return partner;}
For the full set of available query methods and a complete worked example, see Authorization with simple roles.
Steps: authorize with semantic roles
-
Complete the basic integration described in Authentication and authorization for REST APIs – Integration, and set
jeap.security.oauth2.resourceserver.system-name— it is required for authorization with semantic roles. -
Check roles declaratively in the SpEL expressions of the
@PreAuthorizeand@PostAuthorizeannotations (MVC and WebFlux):@PreAuthorize("hasRole('resource', 'operation')")public Data getData(...) {...@PreAuthorize("hasRoleForPartner('resource', 'operation', #partnerId)")public Partner getPartner(String partnerId) {...Note: Unfortunately, IntelliJ cannot recognize these additional methods. Auto-completion etc. is therefore not available inside the SpEL string (see the bug report in IntelliJ).
-
Or, when the check cannot be expressed declaratively — for example because the data has to be loaded first — check roles programmatically through the
ServletSemanticAuthorizationSpring bean:import ch.admin.bit.jeap.security.resource.semanticAuthentication.ServletSemanticAuthorization;...private ServletSemanticAuthorization jeapAuthorization;...public Partner findPartner(...) {Partner partner = ...//Throw an exception if the user is not allowed to access the partnerif (!jeapAuthorization.hasRoleForPartner("resource", "operation", partner.getPartnerId())) {throw new AccessDeniedException("Missing role for partner with id '" + partner.getPartnerId());}return partner;}
For the full set of available query methods, including how to query all roles or all business partners a user has access for, see Authorization with semantic roles.
Related
- Role concept in the Blueprint Microservice — understand simple roles, semantic roles and authorities before choosing one.
- Authorization with simple roles — query-method reference and a complete worked example.
- Authorization with semantic roles — query-method reference and a worked example of query results.
- How to: authorize REST API access with authorities — the alternative model for application-local, fine-grained permissions.
- Authentication and authorization for REST APIs — the basic integration these steps build on.