Authentication and authorization for REST APIs
Microservices frequently provide REST APIs. These APIs must be protected so that only authorized callers have access. In the jEAP Blueprint Microservice, the authentication and authorization of the caller is based on OAuth2 (see OIDC in the Blueprint Microservice). A caller has to identify itself with an access token issued by an authorization server. A microservice that offers a REST API (also called a resource) must therefore be able to process such an access token. This processing includes in particular:
- reading the token from the request
- validating the token according to the rules of the jEAP Blueprint Microservice (see Tokens in the Blueprint Microservice)
- checking the authorization of the caller for the specific REST API call
Protected APIs can no longer be called directly. To test them anyway, an API client such as Postman can be used with an access token obtained from the authorization server.
Integration
Spring Security supports OAuth2 directly. The starter jeap-spring-boot-security-starter implements OAuth2-based authorization according to the rules of the jEAP Blueprint Microservice.
<dependency>
<groupId>ch.admin.bit.jeap</groupId>
<artifactId>jeap-spring-boot-security-starter</artifactId>
</dependency>
To use the starter, an OAuth2 authorization server must be available (e.g. Keycloak, an OAuth2 mock server, a B2B Gateway etc.).
The starter supports the Spring WebMvc stack.
If a microservice has to offer APIs that should not be protected with OAuth2 in addition to its OAuth2-protected APIs, these differently protected or unprotected APIs can be exposed with an additional, specific Spring Security filter chain (see Spring Security configuration).
Configuration
To protect a REST API as an OAuth2 resource with jeap-spring-boot-security-starter, the following properties must be configured appropriately:
| Property | Optional | Value |
|---|---|---|
jeap.security.oauth2.resourceserver.authorization-server.issuer | yes | OAuth2 authorization server whose tokens the resource should accept. This property configures an authorization server that issues tokens in the user and system contexts. The value is typically a URI pointing to the authorization server. |
jeap.security.oauth2.resourceserver.authorization-server.jwk-set-uri | yes | URI of the JWK set endpoint of the OAuth2 authorization server. If the authorization server is a Keycloak instance, this property does not have to be defined explicitly; it is then derived automatically from the configured issuer. Configure only together with jeap.security.oauth2.resourceserver.authorization-server.issuer. |
jeap.security.oauth2.resourceserver.b2b-gateway.issuer | yes | B2B gateway whose tokens the resource should accept. This property configures an authorization server that issues tokens in the B2B context. |
jeap.security.oauth2.resourceserver.b2b-gateway.jwk-set-uri | yes | URI of the JWK set endpoint of the B2B gateway. Configure only together with jeap.security.oauth2.resourceserver.b2b-gateway.issuer. |
jeap.security.oauth2.resourceserver.resource-id | yes | ID of the OAuth2 resource. If this property is not set, the application name (spring.application.name) is used as the ID. The ID of the resource is needed for the check against the aud claim (audience) of tokens. |
jeap.security.oauth2.resourceserver.system-name | yes | Name of the business application (system) the microservice belongs to. This system name is used for the authorization against semantic roles, so that the system name does not have to be repeated in every authorization check. If this property is not set, semantic roles cannot be checked. |
jeap.security.oauth2.resourceserver.log.authentication-failure.enabled | yes | If this property is defined and set to true, certain information about authentication failures is logged at the info level. |
jeap.security.oauth2.resourceserver.log.access-denied.enabled | yes | If this property is defined and set to true, certain information about access-denied errors is logged at the info level. |
jeap.security.oauth2.resourceserver.log.access-denied.debug | yes | If this property is defined and set to true, the logged-in user and their permissions are additionally logged for a logged access-denied error. This logging takes place at the debug level. Information about the logged-in user and their permissions is usually sensitive or security-critical. This logging must therefore usually remain disabled, in particular in production-like environments. |
Configuring multiple authorization servers
The authorization-server and b2b-gateway properties described above allow a simple configuration of one Keycloak
authorization server and one B2B gateway. In addition (as of version 12.6.0 of jeap-spring-boot-security-starter), a
more general configuration option is available through the auth-servers properties. They configure a list of
authorization servers that the OAuth2 resource should trust. The authorization servers in this list can be configured
in addition to or as a replacement for the authorization servers under authorization-server and b2b-gateway. In
the more general configuration, each authorization server can be configured with the jEAP authentication contexts
for which it is allowed to issue tokens.
| Property | Optional | Value |
|---|---|---|
jeap.security.oauth2.resourceserver.auth-servers[i].issuer(i=0: first authorization server configuration, i=1: second authorization server configuration, ...) | yes | OAuth2 authorization server whose tokens the resource should accept. The value is typically a URI pointing to the authorization server. |
jeap.security.oauth2.resourceserver.auth-servers[i].jwk-set-uri | yes | URI of the JWK set endpoint of the OAuth2 authorization server. If the authorization server is a Keycloak instance, this property does not have to be defined explicitly; it is then derived automatically from the configured issuer. |
jeap.security.oauth2.resourceserver.auth-servers[i].authentication-contexts[j](j=0: first context, j=1: second context, ...) | yes | List of jEAP authentication contexts for which the OAuth2 authorization server is allowed to issue tokens. Supported values are user, sys and b2b. If the property is not specified, user and sys are configured implicitly. |
The auth-servers properties are most easily defined in a YAML configuration. The following code gives an example:
jeap:
security:
oauth2:
resourceserver:
auth-servers:
- issuer: "https://some-keycloak-auth-server/auth"
- issuer: "https://another-keycloak-auth-server/auth"
authentication-contexts: [sys]
- issuer: "https://b2b-gateway/auth"
jwk-set-uri: "https://b2b-gateway/auth/.well-known/jwks.json"
authentication-contexts: [b2b]
Note: If
jeap-spring-boot-security-starteris added to a microservice as a dependency but no authorization server is configured (viajeap.security.oauth2.resourceserver.authorization-server.issuer,jeap.security.oauth2.resourceserver.b2b-gateway.issuerorjeap.security.oauth2.resourceserver.auth-servers[0].issuer), the starter activates a deny-all protection instead of the OAuth2 resource server protection, which rejects all access to REST APIs. This does not apply to REST APIs that are activated by other jEAP starters (e.g. the monitoring starter) or that are protected by a custom security configuration with a priority higher than the minimal priority.
Info: It is recommended to configure the authorization server, or the "main" one if there are several, via
jeap.security.oauth2.resourceserver.authorization-server.*. This configuration is used by other jEAP starters (e.g. the Swagger starter) to automatically apply certain preconfigurations for the developer's convenience. If the authorization servers of the resource are configured exclusively underjeap.security.oauth2.resourceserver.auth-servers, these preconfigurations are omitted and the developer has to configure the corresponding properties of the affected starters themselves (according to the documentation of the starters).
Example configurations
System name in application.yml:
spring:
application:
name: jme-security-resource-service
jeap:
security:
oauth2:
resourceserver:
system-name: "jme"
Authorization server and B2B gateway in application-<env>.yml (here for the local environment with the OAuth2 mock
server):
jeap:
security:
oauth2:
resourceserver:
log:
authentication-failure:
enabled: true
access-denied:
enabled: true
debug: true
authorization-server:
issuer: "http://localhost:8081/jme-security-auth-scs"
jwk-set-uri: "${jeap.security.oauth2.resourceserver.authorization-server.issuer}/.well-known/jwks.json"
b2b-gateway:
issuer: "http://localhost:8181/jme-security-auth-scs"
jwk-set-uri: "${jeap.security.oauth2.resourceserver.b2b-gateway.issuer}/.well-known/jwks.json"
The same trust configuration expressed with the more general auth-servers properties:
jeap:
security:
oauth2:
resourceserver:
auth-servers:
# OAuth2 mock server 1
- issuer: "http://localhost:8081/jme-security-auth-scs"
# OAuth2 mock server 2
- issuer: "http://localhost:8089/jme-security-auth-scs"
# B2B gateway mock
- issuer: "http://localhost:8181/jme-security-auth-scs"
jwk-set-uri: "http://localhost:8181/jme-security-auth-scs/.well-known/jwks.json"
authentication-contexts: [b2b]
The complete configurations can be found in the jme-security-example project.
Customizing the AuthenticationEntryPoint, AccessDeniedHandler and MethodSecurityExpressionHandler
See How to: customize REST API authentication entry points, access-denied handling and method security
for overriding the AuthenticationEntryPoint and AccessDeniedHandler of the OAuth2 resource server configuration,
and for customizing the MethodSecurityExpressionHandler that jeap-spring-boot-security-starter configures.
Basic access protection
If jeap-spring-boot-security-starter is added to an application, all web endpoints of the application are protected
with OAuth2. If jeap-spring-boot-monitoring-starter is used at the same time, the monitoring endpoints are exposed
as described in Monitoring endpoints (the Prometheus endpoint, for
example, is exposed protected by basic authentication). If jeap-spring-boot-swagger-starter is used as well, the
Swagger UI is exposed as described in
API documentation with OpenAPI / Swagger. Further exceptions must be
defined explicitly in the application with an additional Spring Security filter chain (see
Spring Security configuration).
Authorization
In the Blueprint Microservice, authorization is in principle performed against roles according to the
role concept in the Blueprint Microservice, i.e. against user roles and business partner roles,
specifically the access token claims userroles and bproles. The Blueprint Microservice supports different ways
of interpreting and checking these roles in an access token:
- as simple roles
- as semantic roles
- as authorities
Each of these models supports different query options and places different requirements on the application. The requirements, options and usage of each model are described on the following pages:
An application must choose one of these models for the authorization of its users.
Where authorization takes place
The authorization for REST APIs is a property of REST services. For this reason, the check must be performed in the REST layer. In a clean architecture, the security context should not be accessed outside the REST layer; the service and domain layers should be built technology-neutral. Otherwise there may be problems, for example, with tests or with scheduling and event processing, because no security context exists there.
With declarative authorization this is usually simple: the corresponding annotations can simply be placed in the REST layer. With programmatic authorization it can be more difficult, because, for example, elements of the security context have to be used to filter objects in the database. The REST layer should pass the necessary information on to the other layers, so that they do not have to access the security context directly. The same information can then also be supplied from other inputs.
Example of authorization in the REST layer:
class RestController {
private final ServletSemanticAuthorization jeapAuthorization;
private final ServiceController serviceController;
// Declarative authorization must always take place in the REST layer, i.e. in the same place as e.g. @GetMapping
@PreAuthorize("hasRole('example', 'read')")
@GetMapping("/api/examples/{id}")
Example getExampleById(@PathVariable("id") String id) {
Example example = serviceController.findById(id);
// Programmatic authorization must always take place in the REST layer, i.e. in the same place as e.g. @GetMapping
if (!jeapAuthorization.hasRoleForPartner("example", "read", example.getPartner())) {
throw new NotAllowedException();
}
return example;
}
// Declarative authorization must always take place in the REST layer, i.e. in the same place as e.g. @GetMapping
@PreAuthorize("hasRole('example', 'read')")
@GetMapping("/api/examples")
List<Example> getExamples() {
// When filtering, for example, it makes no sense to perform the authorization after the filtering.
// In that case, the information needed for the filtering should be passed to the service layer.
// The service layer, however, should not access the security context directly.
List<String> partners = jeapAuthorization.getPartnersForRole("example","read");
return serviceController.findAllForPartners(partners);
}
...
}
class Service {
...
List<Example> findAllForPartners(List<String> partners){
// No authorization should take place here. There may, however, be filtering based on the parameters (partners).
..
}
List<Example> findById(String id){
// No authorization should take place here. There may, however, be filtering based on the parameters (id).
..
}
}
Example of authorization in another layer (WRONG):
class RestController {
private final ServiceController serviceController;
@GetMapping("/api/examples/{id}")
Example getExampleById(@PathVariable("id") String id) {
return serviceController.findById(id);
}
@GetMapping("/api/examples")
return serviceController.findAllForPartners(partners);
}
...
}
class Service {
// Authorization beans must not be used outside the REST layer
// DO NOT DO THIS
private final ServletSemanticAuthorization jeapAuthorization;
// DO NOT DO THIS
...
// Declarative authorization outside the REST layer is not allowed
// DO NOT DO THIS
@PreAuthorize("hasRole('example', 'read')")
// DO NOT DO THIS
List<Example> findAllForPartners(List<String> partners){
List<Example> examples = ...
// Programmatic authorization outside the REST layer is not allowed
// Filtering must be based on the inputs, not on the security context
// DO NOT DO THIS
return examples.stream()
.filter(e -> jeapAuthorization.hasRoleForPartner("example", "read", e.getPartner()))
.collect(Collectors.toList());
// DO NOT DO THIS
}
// Declarative authorization outside the REST layer is not allowed
// DO NOT DO THIS
@PreAuthorize("hasRole('example', 'read')")
// DO NOT DO THIS
List<Example> findById(String id){
// Programmatic authorization outside the REST layer is not allowed
// DO NOT DO THIS
if (!jeapAuthorization.hasRoleForPartner("example", "read", example.getPartner())) {
throw new NotAllowedException();
}
// DO NOT DO THIS
...
}
}
Examples
Two example projects are available that show different use cases of the jeap-spring-boot-security-starter library.
- jme-security-example shows
- the authorization with semantic roles (jme-security-resource-service)
- the direct use of access tokens with claims as required by jEAP security
- the calling of OAuth2-protected endpoints (jme-security-client-service)
- the configuration of an OAuth2 mock server instance (jme-security-auth-scs)
- details in the Readme
- jme-security-oauth2-example shows
- the authorization with
- simple roles (jme-security-oauth2-resource-service)
- authorities (jme-security-oauth2-resource-authorities-service)
- the transformation of access tokens with a claim set converter to provide the claims required by jEAP security
- the calling of OAuth2-protected endpoints (jme-security-oauth2-client-service, jme-security-oauth2-client-authorities-service)
- the configuration of an OAuth2 mock server instance (jme-security-oauth2-auth-scs)
- details in the Readme
- the authorization with
Both examples require an authorization server with a matching configuration. Locally and in the DEV environment, an instance of the OAuth2 mock server is used for this; in the REF environment, the corresponding Keycloak server.
Further documentation
Related
- How to: customize REST API authentication entry points, access-denied handling and method security — override the AuthenticationEntryPoint, AccessDeniedHandler or MethodSecurityExpressionHandler.
- Authentication and authorization — the overview of authentication and authorization in jEAP: authentication contexts, functional and data authorization, OpenID Connect and OAuth2.
- Authorization with simple roles — check roles as arbitrary strings.
- Authorization with semantic roles — check roles by system, tenant, resource and operation.
- Authorization with authorities — derive fine-grained permissions from roles.
- Token introspection — validate access tokens against the authorization server.
- Role concept in the Blueprint Microservice — user roles, business partner roles and role models.
- Integrating different authorization servers — claim set converters per issuer.
- Testing secured REST APIs — test support for protected endpoints.
- Audience validation — how the
audclaim is checked against the resource ID. - Spring Security configuration — additional Spring Security configuration for APIs that are not protected as an OAuth2 resource, e.g. public or basic auth protected APIs.