Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

iTechGuides is reader-supported. When you buy through links on our site, we may earn an affiliate commission. As an Amazon Associate I earn from qualifying purchases. Learn more

To secure a Spring WebFlux client application with OIDC using pac4j, add the spring-webflux-pac4j integration and pac4j-oidc client, configure an OidcClient, register its exact callback URI with your identity provider, and protect the intended routes with a pac4j SecurityFilter. The integration runs pac4j’s synchronous security work on Reactor’s bounded elastic scheduler, away from Netty’s event loop.

What you need and which versions the example uses

The pac4j guide’s example targets Spring Boot 3 and Java 17. It uses spring-webflux-pac4j 3.0.1 and pac4j-oidc 6.5.9, and adds spring-jcl explicitly because the integration version excludes it from a transitive Spring dependency while Spring 6 needs the logging bridge at runtime. Treat these as the versions in that documented example, not a claim that they are the latest compatible versions for every Spring baseline. Check dependency management and compatibility for your project before adopting them. See the pac4j Spring WebFlux OIDC guide.

OIDC adds an identity layer to OAuth 2.0. With pac4j, an OidcConfiguration holds provider settings and your client ID and secret; an OidcClient uses that configuration to start the login flow. The identity provider authenticates the user and returns an ID token that establishes the signed-in identity. The pac4j OIDC client reference describes the client and configuration model.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Add and configure the OIDC client

Declare the dependencies

In your Spring Boot Maven application, include the WebFlux starter, the pac4j WebFlux integration, the OIDC client, and the logging bridge shown in the guide. Keep the pac4j versions aligned with the Spring version managed by your application; do not treat the guide’s versioned example as a universal dependency recipe.

Set provider metadata and credentials

Configure an OidcConfiguration with your identity provider’s discovery URI, client ID, and client secret, then create an OidcClient and add it to pac4j Config. The guide uses https://www.casserverpac4j.dev/oidc/.well-known/openid-configuration for its public demo provider. Replace that URL and the sample credentials with your provider’s metadata endpoint and registered client credentials.

The demo configuration enables setAllowUnsignedIdTokens(true). That option is only for the guide’s public demo identity provider; remove it when configuring a real provider. Do not weaken ID-token validation to make a production login succeed.

Register the exact callback URI

The guide configures the callback base path as http://localhost:8080/callback; the provider registration shown for pac4j includes the query string ?client_name=OidcClient. Register the full URI http://localhost:8080/callback?client_name=OidcClient for the local example. OIDC providers commonly reject a callback that differs from the registered URI, including a missing or different query parameter.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

For deployment, register the externally visible HTTPS callback URL, including the pac4j client-name suffix, and make sure the application’s proxy and HTTPS configuration produce that same public URL. Keep callback and logout endpoints outside any protected path prefix so an unauthenticated user and the identity provider can reach them.

Protect the routes that should require login

Create a pac4j SecurityFilter using the OIDC client name and a PathMatcher, then map the filter to the routes that require authentication. The guide’s includePath("/protected/") is a prefix match. It matches /protected/index and deeper paths, but does not match /protected itself or a sibling such as /protected-other.

Decide explicitly which exact and nested routes are private, then test them individually. Add rules for edge routes such as /protected if they are intended to be private; do not assume a trailing-slash prefix covers them. Also verify that public routes, including /callback and /logout, are not intercepted by the protected matcher.

Register callback and logout controllers

Scan org.pac4j.springframework.web so the integration’s callback and logout controllers are registered. The guide uses /callback and /logout. Configure callback session renewal and logout behavior in application properties rather than adding duplicate custom endpoint mappings alongside the built-in controllers.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Callback session renewal can replace the session identifier after authentication, helping protect against session fixation. Allow the integration’s session operations to complete before continuing the request; otherwise authentication state may not be available as expected.

Understand the reactive execution model

Spring WebFlux is reactive, but pac4j’s security logic and OIDC HTTP calls are synchronous and blocking. The WebFlux integration schedules that work on Reactor’s bounded elastic scheduler, keeping it away from Netty’s event loop. The guide says you do not need to add a separate scheduler wrapper around the pac4j security filter yourself.

Read the authenticated OIDC profile

In a protected controller, build a SpringWebfluxWebContext and SpringWebfluxSessionStore from the request’s ServerWebExchange. Use those with ProfileManager to retrieve the authenticated OidcProfile. The pac4j guide demonstrates this profile access pattern.

OIDC profiles can expose standard claims and token data, but the actual attributes depend on the provider and the scopes requested. Check the claim shape your provider returns before mapping claims such as groups to application roles, and ensure trusted administrators control group membership. Treat profile values as untrusted data when rendering responses: encode them for the output context. The guide’s plain-text response is an example, not a safe general rendering strategy.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Choose local or provider logout

Local logout

Local logout removes the pac4j profile and destroys the application session. Configure the logout controller’s return URL allowlist for your actual public origin; the guide’s local HTTP URL and regex are examples, not production settings. Restrict dynamic return destinations rather than accepting arbitrary URLs.

Logout at the identity provider

Central logout is optional. It depends on the provider exposing an end_session_endpoint and on registering an allowed post-logout redirect URI. Confirm both provider metadata and client registration before relying on the identity provider to end its session.

Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

Troubleshoot common integration failures

  • The protected filter does not run: check the path matcher’s prefix semantics and whether the requested path has the exact trailing slash or route shape covered by your rule.
  • Callback or logout returns 404: confirm that component scanning includes org.pac4j.springframework.web and that the built-in controllers are registered.
  • You see duplicate endpoint mappings: remove the redundant custom callback or logout mapping if the integration’s controller is enabled.
  • The provider rejects the redirect: compare the externally visible callback URI exactly, including scheme, host, path, and ?client_name=OidcClient, with the provider registration.
  • A profile claim is missing: verify the provider’s claim response and requested scopes; claim availability is not identical across providers.

When Spring Security may be a better fit

pac4j is one valid way to add OIDC login to WebFlux, not a requirement for every application. Spring Security provides native reactive OAuth2 Client and OIDC Login support. Its documented approach configures oauth2Login() with a reactive client registration, an issuer-uri, and an openid scope; it also supports outbound protected-resource calls through a reactive authorized-client manager and WebClient. See Spring Security’s reactive OAuth2 documentation.

Choose based on the application you already have and the behavior you need:

Free tools Windows power users keep installed

One-click scans. No signup required.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
  • Existing security model: favor the framework already used across the application unless a concrete requirement justifies another integration.
  • Profile and claims: compare how your chosen framework exposes claims and supports your role-mapping approach.
  • Login versus API access: if the application also needs tokens for outbound API calls, assess the authorized-client flow as well as interactive login.
  • Sessions and logout: verify the session behavior and provider logout support required by your deployment.
  • Provider and version fit: confirm provider compatibility and align library versions with the project’s Spring baseline.

Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.