Skip to main content

JME Security Example

Source on GitHub

This project contains example applications that demonstrate how to implement OAuth2-protected resources and clients with jEAP. It covers service-to-service access, browser login, token forwarding, semantic authorization and role introspection. The project is self-contained and uses the included OAuth mock server for local development and integration tests.

Modules​

ModulePurpose
jme-security-resource-serviceOAuth2 resource server with partner- and operation-based semantic authorization
jme-security-client-serviceOAuth2 client that calls the resource and client/resource services
jme-security-clientresource-serviceService acting as both an OAuth2 client and resource server
jme-security-auth-scsConfigured instance of the jEAP OAuth mock server
jme-security-uiAngular/Oblique UI with qd-auth, packaged into the SCS
jme-security-scsSelf-contained Spring Boot backend and Angular UI
jme-security-testEnd-to-end tests using the included OAuth mock server

How the Examples Relate​

The UI uses the Oblique master layout and retains @quadrel-enterprise-ui/auth for OAuth2 login/logout, silent renewal, token-aware HTTP requests and role-based route authorization. The Quadrel UI framework is not required. The Oblique header provides language selection and explicit qd-auth login/logout controls without calling the ePortal service-navigation backend. The configuration endpoint returns mockPams: true to disable qd-auth's legacy header-session check; this flag does not select the OAuth provider, which is configured separately through frontend.authority.

The UI stays on Angular 20 with Oblique 14 to match the peer dependencies of qd-auth 20. The SCS browser tests cover the authorization-code flow, role-based access, backend claims/current-user data, logout, language selection, deep links, silent-renew asset packaging and token isolation on external requests. They run with browser CSP enforcement enabled.

The browser-login example and the service-to-service example are independent. The UI packaged in jme-security-scs authenticates a user with the OAuth mock server and calls only SCS endpoints. The SCS does not call jme-security-resource-service or jme-security-client-service.

Browser -> jme-security-scs
-> jme-security-auth-scs (authorization-code flow)

The service-to-service example starts at jme-security-client-service. Its public example endpoints obtain a system token from the OAuth mock server using the client-credentials flow and use that token to call the protected jme-security-resource-service endpoints.

Caller -> jme-security-client-service -> jme-security-resource-service
|
+-> jme-security-auth-scs (client-credentials flow)

Prerequisites​

  • JDK 25 or later
  • Node.js 22 and npm
  • Google Chrome for the browser integration tests

Build and Test​

Build all modules and run their tests with:

./mvnw install

The end-to-end test in jme-security-test starts the OAuth mock server, the resource, client/resource and client services and the SCS as Maven subprocesses on free ports and exercises the client service endpoints listed under Local Test URLs (semantic authorization, token forwarding, token introspection, current user endpoint, business partner scoped roles) as well as some direct calls to the resource service (strict audience validation, business partner roles, audience check on token introspection against a second, deliberately misconfigured resource service instance, caching of the token introspection responses on a third instance reserved for that test). The services run with their local profiles from the module sources, the SCS together with the UI module. Build the other modules first, otherwise the SCS start also has to build the Angular UI. To run only that test:

./mvnw install -pl '!:jme-security-test'
./mvnw verify -pl jme-security-test

Running Locally​

Build the project first, then start jme-security-auth-scs with the local profile. Start only the resource, client and SCS applications needed for the access chain you want to test.

./mvnw -pl jme-security-auth-scs spring-boot:run -Dspring-boot.run.profiles=local
./mvnw -pl jme-security-resource-service spring-boot:run -Dspring-boot.run.profiles=local
./mvnw -pl jme-security-client-service spring-boot:run -Dspring-boot.run.profiles=local
./mvnw -pl jme-security-clientresource-service spring-boot:run -Dspring-boot.run.profiles=local
./mvnw -pl jme-security-scs spring-boot:run -Dspring-boot.run.profiles=local

The local client secrets are intentionally public test fixtures and must not be reused outside this example.

Trying the Client and Resource Services​

For the basic service-to-service example, start these three applications in separate terminals:

./mvnw -pl jme-security-auth-scs spring-boot:run -Dspring-boot.run.profiles=local
./mvnw -pl jme-security-resource-service spring-boot:run -Dspring-boot.run.profiles=local
./mvnw -pl jme-security-client-service spring-boot:run -Dspring-boot.run.profiles=local

Call the client service without providing credentials:

curl http://localhost:8090/jme-security-client-service/api/partners

The client service obtains a token for jme-security-client-service, adds it as a bearer token to its outgoing request and returns the response from the resource service. The response starts with Partner list: followed by the accessible partners.

Calling the protected resource endpoint without a token returns 401 Unauthorized:

curl -i http://localhost:8070/jme-security-resource-service/api/partners

To call the resource directly, first request a token from the OAuth mock server:

curl --user jme-security-client-service:secret \
--data grant_type=client_credentials \
http://localhost:8081/jme-security-auth-scs/oauth2/token

Copy the access_token from the response and use it in the protected request:

curl --header "Authorization: Bearer <access-token>" \
http://localhost:8070/jme-security-resource-service/api/partners

Multiple Authorization Servers​

The OAuth mock server also provides a local-2 profile. It exposes the same users and roles on another port and signs tokens with another key. This can be used to demonstrate a resource server that trusts multiple issuers.

./mvnw -pl jme-security-auth-scs spring-boot:run -Dspring-boot.run.profiles=local-2
./mvnw -pl jme-security-resource-service spring-boot:run -Dspring-boot.run.profiles=local,local-auth-servers
./mvnw -pl jme-security-client-service spring-boot:run -Dspring-boot.run.profiles=local,local-auth-servers

The client configuration supports the providers mock-server-1 and mock-server-2; mock-server-2 is selected by default in application-local-auth-servers.yml.

Access Chains​

The examples support these access chains:

  • client-service -> resource-service
  • client-service -> clientresource-service -> resource-service
  • UI -> SCS

The partner endpoints on jme-security-client-service call jme-security-resource-service by default. Add target=clientresource to route the call through jme-security-clientresource-service instead:

  • /api/partners
  • /api/partners?target=clientresource

Resource Endpoints​

The resource service demonstrates annotation-based and programmatic authorization, roles scoped to a business partner, roles valid for all partners and authorization by operation only. The client service exposes corresponding endpoints and adds the required access token when forwarding requests.

Partners​

  • /api/partners lists all partners the caller may access.
  • /api/partners/11111 returns a partner by numeric ID.
  • /api/partners/eins returns a partner by external reference.
  • /api/partners/eins/name returns only the partner name.

Things​

  • /api/things lists all accessible things.
  • /api/partners/11111/things lists things belonging to a partner.
  • /api/things/1 returns one thing belonging to a partner.

Operation-only Authorization​

These endpoints demonstrate authorization based on an operation without specifying a semantic resource:

  • /api/operation-things
  • /api/operation-things/partners/11111
  • /api/operation-things/1

Additional Endpoints​

  • /api/info forwards a request protected with basic authentication instead of OAuth2.
  • /api/introspected-roles returns roles added during token introspection.
  • /api/introspected-roles?pruned=true uses a client whose token roles are pruned and recovered by introspection.
  • /api/current-user calls the SCS current-user endpoint with semantic roles in the standard syntax.
  • /api/current-user?alternateRoles=true uses the alternate semantic-role syntax.
  • /api/bproles returns the business-partner roles from the access token.
  • /api/bproles?scoped=true requests a token whose roles are restricted with the bproles:11111 scope.

Local Test URLs​

UI and SCS​

Client Service​

Local Tests with a Simulated B2B Gateway​

The jme-security-auth-scs and jme-security-client-service modules provide a local-b2b profile. This profile starts the applications on separate ports and configures the client service to obtain simulated B2B gateway tokens from the OAuth mock server. The client service still calls jme-security-resource-service, so start the resource service with the local profile.

./mvnw -pl jme-security-auth-scs spring-boot:run -Dspring-boot.run.profiles=local-b2b
./mvnw -pl jme-security-resource-service spring-boot:run -Dspring-boot.run.profiles=local
./mvnw -pl jme-security-client-service spring-boot:run -Dspring-boot.run.profiles=local-b2b

Use this endpoint to test a request with a simulated B2B gateway token:

Preparing and Starting the Local SCS​

To use the UI packaged in the SCS, start the OAuth mock server and then the SCS with the local profile:

./mvnw -pl jme-security-auth-scs spring-boot:run -Dspring-boot.run.profiles=local
./mvnw -pl jme-security-scs spring-boot:run -Dspring-boot.run.profiles=local

Open http://localhost:8080/jme-security-scs/ and use the mock login form to select a user and roles.

To run the Angular development server separately, start the SCS with local-ui, then run npm start in jme-security-ui. The UI is available on port 4200 and calls the SCS on port 8080.

Restricting Business-partner Roles​

The OAuth mock configuration contains the clients jme-security-ui-bpscoped and jme-security-client-service-bpscoped. They support the dynamic scope bproles:*, which restricts business-partner roles in a token to one partner.

For the system context, call:

This uses the scope bproles:11111. For the user context, start jme-security-scs with the profiles local,bpscoped. The UI then requests tokens for jme-security-ui-bpscoped with the same scope.

Running with the Legacy Audience Check​

The resource servers of the example (jme-security-resource-service, jme-security-clientresource-service and jme-security-scs) run with strict audience validation in their local profiles (jeap.security.oauth2.resourceserver.strict-audience-validation: on): they only accept access tokens whose aud claim contains their resource id and reject tokens without an audience. The OAuth mock server therefore issues the tokens of the example clients with the matching audiences. It also validates the audience on token introspection like Keycloak does (mockserver.introspection-endpoint-audience-check: on): its introspection endpoint only reports a token as active if the token's aud claim contains the id of the introspecting client. The resource service therefore introspects with the client jme-security-resource-service, whose id equals its resource id.

To run the example with the legacy, non-strict audience check instead, add the profile local-legacy-aud-check to the local profile of every application. The OAuth mock server then issues access tokens without an aud claim and does not validate the audience on token introspection, and the resource servers switch the strict audience validation off, accepting tokens without an audience as valid for every resource. Tokens that do specify an audience must still address the called resource. The client service does not validate audiences and needs no profile-specific configuration, it just forwards the tokens it obtains.

./mvnw -pl jme-security-auth-scs spring-boot:run -Dspring-boot.run.profiles=local,local-legacy-aud-check
./mvnw -pl jme-security-resource-service spring-boot:run -Dspring-boot.run.profiles=local,local-legacy-aud-check
./mvnw -pl jme-security-client-service spring-boot:run -Dspring-boot.run.profiles=local,local-legacy-aud-check
./mvnw -pl jme-security-clientresource-service spring-boot:run -Dspring-boot.run.profiles=local,local-legacy-aud-check
./mvnw -pl jme-security-scs spring-boot:run -Dspring-boot.run.profiles=local,local-legacy-aud-check

The access chains of the example work unchanged, for example:

To see the difference, request a token from the OAuth mock server as described above: its payload contains no aud claim. A resource server started with the local profile alone rejects this token with 401 Unauthorized.

Caching Token Introspection Responses​

In its local profile, the resource service introspects tokens in the lightweight mode: a token that carries its roles is used as it is, a token whose roles have been pruned by the OAuth mock server is introspected to fetch the roles from the introspection endpoint of the mock server (see /api/introspected-roles?pruned=true above). Every such introspection adds a request to the authorization server to the request to the resource.

As of version 24.30.0 of the jEAP security starter, the introspection responses can be cached locally per authorization server, so that a token presented repeatedly is typically introspected only once per instance. The local profile of jme-security-resource-service enables the cache with its default settings (at most 1000 responses, each kept for at most 5 minutes):

jeap:
security:
oauth2:
resourceserver:
authorization-server:
introspection:
cache:
enabled: true # default: false
maximum-size: 1000 # default
time-to-live: 5m # default

What to know before enabling the cache in a resource server of your own:

  • Whether caching is appropriate depends on the purpose of the introspection, which the application has to judge: a cached response cannot tell whether a token has been revoked in the meantime. An introspection that makes sure a token still belongs to an active session should not be cached. An introspection that loads relatively stable data kept out of the token, like the pruned roles of this example, is a good fit.
  • Only the transparent introspection enriching a token is served from the cache. Explicit validity checks with JeapJwtIntrospection.isValid(...) always query the introspection endpoint and update the cache with the result.
  • A cached response never outlives its token: it expires after the time to live or with the token, whichever comes first. Only responses reporting the token active are cached.
  • The cache is keyed by the issuer, the token id (jti) and a hash of the token value, no bearer tokens are kept in memory. Tokens without a jti claim are not cached.
  • The cache is local to the instance and independent of the introspection mode. The metric jeap.security.token.introspection.cache.lookups (tags issuer and result = hit, miss or skipped) counts the cache lookups.

For all details, e.g. on the expiry of the cached responses or on replacing the default cache by an implementation of your own, see the section "Caching introspection responses" of the jEAP security starter documentation.

To watch the cache at work, the local profile of the resource service also logs the token introspection on the trace level (logger ch.admin.bit.jeap.security.resource.introspection). Start the OAuth mock server, the resource service and the client service with the local profile as described above and call the following URL twice:

The client service reuses its token until it expires, so both calls present the same token to the resource service. The log of the resource service shows the token being introspected on the introspection endpoint and its response being cached on the first call, and the second call being served from the cache. The messages identify the token by its issuer, subject, jti and a prefix of the hash of its value, the token itself is never logged:

No cached introspection response for token [issuer='http://localhost:8081/jme-security-auth-scs', subject='...', jti='...', hash='...'], introspecting it on the introspection endpoint.
Cached the introspection response for token [issuer='http://localhost:8081/jme-security-auth-scs', subject='...', jti='...', hash='...'] for PT5M (time to live PT5M, token expires at 2026-09-08T16:21:05Z, response expires at 2026-09-08T16:21:05Z).
Serving the introspection response for token [issuer='http://localhost:8081/jme-security-auth-scs', subject='...', jti='...', hash='...'] from the cache.

JME​

This repository is part of the JME open source distribution.