Authorization with authorities
The following sections describe the integration and use of the jeap-spring-boot-security-starter library when
an application authorizes user access against authorities.
Integration
For the integration steps, see
How to: authorize REST API access with authorities.
This page describes the AuthoritiesResolver API used by that integration.
The code that derives authorities from roles has to be integrated into the application. For
this, the application has to provide a Spring bean of type AuthoritiesResolver. If no custom
AuthoritiesResolver is defined, a DefaultAuthoritiesResolver is used, which simply creates an authority for
each role by prefixing the role with ROLE_.
An AuthoritiesResolver implementation has access to both the user roles (userroles) and the business partner
roles (bproles) of the user. From these, the implementation can derive an arbitrary set of authorities in the
form of Spring Security GrantedAuthority instances.
Skeleton of a custom AuthoritiesResolver implementation:
@Component
public class ExampleAuthoritiesResolver implements AuthoritiesResolver {
@Override
public Collection<GrantedAuthority> deriveAuthoritiesFromRoles(Set<String> userRoles, Map<String, Set<String>> businessPartnerRoles) {
// Implementation
}
}
A suitable GrantedAuthority implementation is, for example, the Spring Security class SimpleGrantedAuthority,
which is essentially a simple wrapper around a string.
A concrete example of an AuthoritiesResolver is given in
jme-security-oauth2-resource-authorities-service.
Authorization checks
Authorities are a basic mechanism of Spring Security. To check the authorization of a user based on authorities, the standard Spring Security mechanisms can be used.
See How to: authorize REST API access with authorities
for how to check authorities declaratively with @PreAuthorize/@PostAuthorize and programmatically from the
Authentication.
Examples
The jEAP example project
jme-security-oauth2-example
contains the module jme-security-oauth2-resource-authorities-service with a resource that gives an example of
authorization with authorities, including the
ExampleAuthoritiesResolver
described above.
Further documentation
Related
- Authentication and authorization — the overview of authentication and authorization in jEAP: authentication contexts, functional and data authorization, OpenID Connect and OAuth2.
- Authentication and authorization for REST APIs — integrate jEAP Security into a REST API.
- Role concept in the Blueprint Microservice — understand simple roles, semantic roles and authorities.
- Authorization with simple roles — authorize against roles compared one-to-one as strings.
- Authorization with semantic roles — authorize against roles composed of system, tenant, resource and operation.
- How to: authorize REST API access with authorities — the integration and authorization-check steps.