October DealsAmazon USOctober deal check: compare before you payAmazon US: current deals, useful picks and tech finds.Check DealsWindows FixRecommendedWindows errors stealing your time? Find the fix fastScan stability, cleanup and performance issues.Fix NowOctober DealsAmazon USDeal season is back - check today's better picksAmazon US: current deals, useful picks and tech finds.See Picks×
Skip to content
World desk7 min

Secure a Jakarta EE Web App with OIDC Using pac4j

Use pac4j’s Jakarta Servlet integration and OIDC module to add browser login, protect routes, read user profiles, enforce authorization, and configure logout.

What’s actually slowing this PC down?

Pick the symptom - the matching free tool is one click away.

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

To add browser-based OpenID Connect (OIDC) login to a Jakarta Servlet application, connect pac4j’s security engine to Servlet filters with jakartaee-pac4j and add its pac4j-oidc module. Configure an OIDC client, register the exact callback URL with your identity provider, and map separate filters for protected pages, the callback, and logout.

Check compatibility before you add pac4j

The pac4j guide’s Jakarta example uses Java 17 or later, Maven, a WAR, and a Servlet 6.0 container. It lists Tomcat 10.1 and Jetty 12 with the Jakarta EE 10 environment as examples. Its documented dependency versions are jakartaee-pac4j 8.0.3 and pac4j-oidc 6.5.8; treat these as the guide’s snapshot, not a guarantee that every server and provider combination is interchangeable. Check the current release notes and pin versions compatible with your chosen container. pac4j’s Servlet quickstart and the integration repository document the setup and compatibility information.

As an Amazon Associate I earn from qualifying purchases.

Use the integration that matches your Servlet namespace. Jakarta applications use jakartaee-pac4j; applications still built on legacy Java EE javax.servlet APIs need javaee-pac4j instead. The repository maps integration 8+ to Java 17 and pac4j 6, and integration 7+ to Java 11 and pac4j 5. These are project compatibility notes, not a substitute for checking the specific versions you deploy.

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

This guide uses pac4j’s filter integration. Jakarta Security also offers a separate built-in OIDC mechanism; its configuration model is different, so do not mix its annotations into the pac4j filter setup. The Jakarta EE security tutorial describes that alternative.

Add the dependencies and configure an OIDC client

Start with a Maven web application packaged as a WAR. Add the Servlet integration and OIDC module, and declare the Servlet API with provided scope because the Servlet container supplies it. The pac4j quickstart provides the dependency declarations for its example.

Create an OidcConfiguration with the identity provider’s discovery URI, client ID, and client secret. Use it to construct an OidcClient, then include the client and callback URL in pac4j’s Config. Keep client secrets in deployment configuration or a secrets manager rather than source control. Any literal credentials in a documentation example are demonstrations, not values to reuse.

Use the provider’s discovery document rather than guessing endpoint URLs. It advertises the authorization, token, user-info, and JWKS endpoints used in the OIDC flow. The URL is commonly the provider’s base URL followed by /.well-known/openid-configuration, but use the exact discovery URL the provider documents.

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

One important production caveat: pac4j’s quickstart sets setAllowUnsignedIdTokens(true) as a concession for its public demo server. Do not carry that setting into a real provider configuration. A production application must validate signed ID tokens against the provider’s published keys and expected issuer.

Register the exact callback URL with your provider

The identity provider must allow the callback URL that the application actually sends in the authorization request. For the documented pac4j configuration, the complete URL includes the query parameter ?client_name=OidcClient. Register the full URL—including scheme, host, path, and query string—in the provider console; a URI that differs by one of these parts can produce an invalid redirect URI error.

When the application is behind a reverse proxy, configure the browser-visible public URL, not an internal hostname, port, or HTTP URL. In production, that generally means the externally reachable HTTPS callback. The provider’s registered redirect and the value used by pac4j must match exactly.

Map the three filters

In WEB-INF/web.xml, or in code using pac4j’s FilterHelper, configure these distinct filters. Choose one registration approach rather than doing both.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
Filter Purpose Typical mapping
SecurityFilter Protects selected application routes and starts login when the user is not authenticated. Protected paths such as /account/*
CallbackFilter Handles the identity provider’s return and completes the OIDC callback. The callback path configured in the application and registered with the provider
LogoutFilter Clears the local authentication state and optionally initiates provider logout. A route such as /logout

Map the security filter only to routes that should require authentication, unless you have deliberately chosen catch-all protection. The callback itself must be reachable for the login flow to complete. If you use annotations for filter registration, check that the application scans them: metadata-complete="true" in web.xml suppresses annotation scanning unless components are declared explicitly.

In the example configuration, session renewal is enabled to help guard against session fixation. Review the complete filter setup in the pac4j quickstart and adapt its paths and return URLs to your application rather than copying them blindly.

Understand the browser login flow

  1. A browser requests a route protected by SecurityFilter.
  2. If the user is not authenticated, pac4j redirects the browser to the identity provider.
  3. After authenticating the user, the provider returns the browser to the registered callback.
  4. CallbackFilter completes the authorization-code exchange, validates the ID token, stores the profile in the session, and returns the browser to the original page or a configured default.
  5. The protected application route can then read the authenticated profile.

The provider’s discovery document also matters to token validation. Jakarta Security’s OIDC mechanism, for example, relies on metadata for the authorization endpoint, token endpoint, JWKS URI, issuer, supported subject types, response types, and ID-token signing algorithms. The referenced Jakarta Security 5.0 document is a milestone specification, so verify final specification and server behavior before relying on details specific to that version. Jakarta Security 5.0 milestone 2 specification.

Read the authenticated profile in a protected servlet

In a servlet reached through the security filter, use pac4j’s ProfileManager to retrieve the authenticated OidcProfile. The quickstart demonstrates this access pattern. A profile is available only where authentication has been established; a servlet outside the protected route mapping should not assume one exists.

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

The pac4j example requests the default scopes openid profile email. Scopes request access to categories of information; they do not guarantee the provider will return every corresponding claim. The provider’s claim configuration and the user’s account determine what is present, so handle missing email or name values safely.

Best Value
Sale
The Web Application Hacker's Handbook: Finding and Exploiting Security Flaws
  • Comes with secure packaging
  • It can be a gift item
  • Easy to read text
Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

Keep authorization separate from login

OIDC login establishes who the user is. Your application still needs to decide which pages and actions that user may access. In pac4j, configure authorizers for checks such as authenticated status, roles, or profile attributes.

Decide explicitly where application roles come from. If the provider supplies trusted group claims, map those claims to the application’s authorization model. If it does not, Jakarta Security’s tutorial describes using an application identity store to supply groups; it also discusses configuring group claims through claimsDefinition and sourcing claims from the access token, ID token, or user-info response, depending on provider support and configuration. Do not assume group claims exist just because login succeeds. Jakarta EE security tutorial.

Test both sides of each authorization rule: a user who should be allowed and one who should be denied. Authentication success alone is not proof that role mapping is correct.

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

Configure local and provider logout

The quickstart maps /logout to LogoutFilter, sets destroySession=true, and returns the browser to a default URL. This ends the application’s local session. It does not necessarily end the user’s session at the identity provider.

If the provider supports OIDC logout, enable central logout so the browser can be sent to the provider’s discovered end_session_endpoint. Register the post-logout return URL with the provider where required. If you accept a dynamic url parameter to choose the return destination, constrain it with logoutUrlPattern to prevent unsafe redirects.

Troubleshoot common failures

  • Invalid redirect URI: Compare the callback configured in pac4j with the provider’s registered URL, including the public scheme, host, path, and ?client_name=OidcClient query parameter.
  • Login loops behind a proxy: Check that the callback uses the URL the browser sees rather than the application’s internal host or port.
  • The security filter does not run: Inspect filter URL mappings and annotation scanning. In particular, metadata-complete="true" disables annotation scanning unless components are declared explicitly.
  • No profile in the servlet: Confirm the servlet URL is protected by SecurityFilter and that the callback completed successfully.
  • Email or name claims are missing: Check requested scopes and the provider’s claim settings. The default scopes do not compel the provider to return every claim.
  • OIDC validation fails: Verify the discovery metadata, expected issuer, and key publication from the JWKS endpoint. Do not disable signature validation or enable unsigned ID tokens to bypass a real provider error.

Verify the integration before deployment

The pac4j guide shows building the WAR with mvn clean package and demonstrates a protected URL. That documentation does not establish that the sample has been independently tested against your container or identity provider. Validate your own deployment with this sequence:

Quick Recap

  1. Request a protected page in a fresh browser session and confirm that it redirects to the expected identity provider.
  2. Complete login and confirm the provider returns to the registered callback without a redirect URI or token-validation error.
  3. Confirm the browser reaches the original protected page and the servlet can read the expected profile fields.
  4. Check that a user without the required role or attribute is denied access.
  5. Log out, confirm the local session is cleared, and—if configured—verify the provider logout and registered post-logout return.

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.

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

Leave a Reply

Your email address will not be published. Required fields are marked *

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

More from the Wire

  1. World desk4 min
    How to Spot an AI Voice Scam Before Sending MoneyDon’t rely on how a caller sounds. Pause, call back through a known number, and verify the emergency with another trusted person before sending money.
  2. Mountain View desk4 min
    Google’s SynthID Detector: How to Check AI-Generated Images, Video and AudioGoogle’s SynthID Detector looks for an embedded watermark in supported images, video and audio. Here is what its results do—and do not—show.
  3. Redmond desk20 min
    How to create a link to File or Folder in Windows 11Windows 11 gives you several ways to point to a file or folder without moving or duplicating it. You can create a desktop shortcut,…
Recommended PC Tool
Recommended PC Tool
Crashes, No Sound, or Screen Glitches?Free driver scan
PC Slower Than It Used to Be?Free scan - under a minute

Two free Windows tools

One Free Minute Could Fix That PC

Before you go - each of these free tools takes about a minute and tackles what quietly slows a Windows PC down.

Special offer. View Outbyte info, uninstall instructions, EULA, and Privacy Policy.