How-Tos: Security
Authentication, tokens, roles, TLS, encryption, signing, and testing security.
Audiences
- Configure Keycloak scopes/mappers for a resource audience — add an audience to an existing resource's access tokens on Keycloak, for a calling client that already exists.
- Create a Keycloak introspection client for a resource — the dedicated-client pattern Keycloak 26.6.2+ requires for its introspection audience check.
- Migrate existing resource access to audience validation — the full rollout for an existing system: introspection compatibility, strict enforcement, or both.
Authorization servers
- Implement a custom claim set converter — adapt an authorization server's or B2B gateway's access-token claims into the format jEAP Security expects.
Protecting and calling REST APIs
- Customize REST API authentication entry points, access-denied handling and method security — override the
AuthenticationEntryPoint,AccessDeniedHandlerorMethodSecurityExpressionHandlerofjeap-spring-boot-security-starter. - Authorize REST API access with roles — check simple or semantic roles declaratively with
@PreAuthorize/@PostAuthorize, or programmatically. - Authorize REST API access with authorities — derive and check fine-grained, application-local authorities from a user's roles.
- Call a secured REST API from Java — add the client starter, configure an OAuth2 client, and create a
RestClientfor the system context or token propagation.
Testing secured REST APIs
- Test a REST controller with MockMvc — set the authentication explicitly on the request without starting a full application server.
- Test a component with a given authentication — verify authorization in isolation with
@WithAuthentication, without going through the HTTP request processing chain. - Test programmatic authorization checks in a unit test — mock a
JeapAuthenticationTokenoutside a Spring application context. - Disable security in a Spring Boot test — lift the security starter's auto-activated web security for a test that only verifies functionality.
- Test a REST endpoint with full OAuth2 security — pass a real access token through the whole resource security chain.
Tokens
- Enable and debug jEAP token introspection — pick an introspection mode, configure Keycloak and the resource server, and debug a user's authorizations when they're fetched by introspection.
- Use dynamic OAuth2 scopes — restrict the business partner roles in a token to one partner, all partners, or none, via Keycloak's
bprolesscope. - Use lightweight access tokens — control which claims a Keycloak lightweight access token carries, and how a client requests one.
- Prune roles from access tokens — cap token size with the Jeap Roles Pruning Token Mapper when a user has too many roles to fit.
Encryption & signing
- Sign and verify messages without jEAP — implement jEAP's message-signing protocol yourself, for a publisher or subscriber that cannot use the jEAP Messaging library.
- Encrypt data with jEAP-Crypto (jeap-crypto) — encrypt/decrypt arbitrary data with a KMS-backed
CryptoService.- Encrypt a JPA entity column transparently with
JeapCryptoStringConverter. - Encrypt/decrypt S3 objects with
JeapCryptoS3Template. - Encrypt a Kafka message type via jeap-messaging's integration with jeap-crypto.
- Encrypt a JPA entity column transparently with
TLS, mTLS & IAM
- Add TLS to a service (jeap-spring-boot-tls-starter) — add the starter and tune the self-signed certificate.
- Expose actuator endpoints over plain HTTP on a separate port when the main port requires mutual TLS.
- Authenticate to AWS with IAM Roles Anywhere — add the starter, configure ARNs/certificate/key, use the credentials provider.
- Test JWE-protected endpoints without a real Vault instance — static test mode and test keys for
jeap-spring-boot-jwe-starter.
Mock authorization server for dev/test
- Run the OAuth2 mock server for local dev and tests (jeap-oauth-mock-server) — create an instance, configure clients/users, reach the login form.
- Add custom token claims via a token customizer bean.
- Point an Angular/Spring client or resource server at the mock server.
- Upgrade the underlying Spring Security Authorization Server version safely.