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

To add browser-based single sign-on to a Java application, register it with an OpenID Connect (OIDC) identity provider and use a framework integration to handle the sign-in redirect, callback, token validation, and local session. For Spring Boot, use Spring Security OAuth2 Client; for a Jakarta EE application, Jakarta Security 3.0 provides an OIDC authentication mechanism. Keycloak can serve as a self-hosted provider, while an existing enterprise provider can use the same OIDC client-registration approach.

What Java SSO does—and what it does not do

Single sign-on lets a person authenticate with a central identity provider (IdP) and reuse that login across applications. In web SSO, applications rely on a shared login session at the identity provider; each application still establishes and manages its own application session.

For a new browser-based Java application, OIDC is generally the default protocol. OIDC adds an authentication layer on top of OAuth 2.0. The Java application acts as the client, also called the relying party, and the IdP authenticates the user. OAuth 2.0 alone is primarily an authorization framework; an OAuth access token should not be treated as proof of a user’s identity.

The usual browser flow is:

  1. The user requests a protected page in the Java application.
  2. The application redirects the browser to the IdP’s authorization endpoint.
  3. The user signs in there. If the IdP already has a valid login session, it may not need to prompt again.
  4. The IdP redirects the browser to the application’s registered callback with an authorization code.
  5. The application exchanges that code with the IdP for tokens, validates the response and relevant token claims, and establishes its own session.
  6. The application uses the authenticated identity and mapped authorities to decide which server-side actions the user may perform.

Use the framework’s supported OIDC mechanisms for the protocol exchange and validation rather than treating a browser-supplied token or claim as trusted. The exact token contents and available claims depend on the provider and its configuration.

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

Choose the Java integration that fits your runtime

Choice Best fit Configuration style Considerations
Spring Security OAuth2 Client Spring Boot applications that need browser login through an OAuth 2.0 or OIDC provider Client registrations and providers configured in application properties or YAML Use OAuth2 Login for interactive sign-in. Configure resource-server support separately if an API must validate bearer access tokens.
Jakarta Security 3.0 OIDC mechanism Jakarta EE applications running on a compatible server Container-integrated authentication configuration, including an OIDC mechanism annotation Jakarta Security 3.0 shipped with Jakarta EE 10 in 2022 and specifies Java SE 11 or newer. Confirm that the chosen runtime supports the required version and configuration.

Both approaches use OIDC concepts such as an issuer, client registration, redirect URI, and provider metadata. Choose based on your application’s runtime, team familiarity, container capabilities, and whether you need separate API resource-server behavior. Jakarta Security’s container integration is a natural fit for Jakarta EE; Spring Security is the direct route for Spring Boot.

Build OIDC login with Spring Boot

1. Add the OAuth2 client support

Add spring-boot-starter-oauth2-client to a Spring Boot application, or the equivalent spring-security-oauth2-client dependency when managing Spring Security dependencies directly. OAuth2 Login is part of Spring Security’s OAuth2 Client support.

2. Register the client with the identity provider

Create a client registration at the IdP for the application. Set the exact callback URI that the application will use, choose client authentication appropriate to the application, and request only the scopes needed. For OIDC login, include openid; profile is commonly requested when profile claims are needed. Keep the client secret in environment-based configuration or a secret manager, not in source control.

Spring Security documents the login-start endpoint as /oauth2/authorization/{registrationId} and the callback pattern as /login/oauth2/code/{registrationId}. The redirect URI configured at the provider must match the application callback exactly, including scheme, host, path, and any relevant port.

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

3. Configure the client and issuer

This YAML shows the configuration shape. Replace the example issuer with the provider’s actual issuer URI, and supply the secret securely at runtime:

spring:
  security:
    oauth2:
      client:
        registration:
          my-oidc-client:
            provider: my-oidc-provider
            client-id: my-client-id
            client-secret: ${OIDC_CLIENT_SECRET}
            authorization-grant-type: authorization_code
            scope: openid,profile
        provider:
          my-oidc-provider:
            issuer-uri: https://idp.example.com

With the openid scope present, Spring Security uses OIDC-specific processing. The issuer URI must identify the provider correctly and support discovery; verify the issuer and callback values against the IdP’s client configuration before testing sign-in.

4. Map identity to application permissions

After a successful login, map validated claims or provider groups to application authorities. Do not assume a claim name, group format, or role hierarchy is identical across providers. Enforce authorization on server-side endpoints and services; hiding a link in the user interface is not an access-control rule.

Build OIDC login in a Jakarta EE application

Jakarta Security 3.0, released for Jakarta EE 10 in 2022, added an OIDC authentication mechanism and specifies Java SE 11 or later. On a compatible runtime, configure @OpenIdAuthenticationMechanismDefinition with the provider URI, client ID, client secret, and redirect behavior. The container acts as the relying party.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
@OpenIdAuthenticationMechanismDefinition(
    providerURI = "https://idp.example.com",
    clientId = "my-client",
    clientSecret = "configured-securely",
    redirectToOriginalResource = true
)
@ApplicationScoped
@ApplicationPath("/rest")
public class ApplicationConfig extends Application {}

The code illustrates the annotation’s configuration shape; do not embed a real secret in a checked-in source file. Use the runtime’s supported secure configuration mechanism, and confirm how the chosen server resolves credentials and handles the callback.

The provider URI must expose OIDC discovery metadata. Jakarta’s OIDC documentation identifies metadata such as the issuer, authorization and token endpoints, JWKS URI, supported subject types and response types, and ID-token signing algorithms. The application or container uses provider metadata and advertised signing keys as part of protocol processing. Follow provider guidance for discovery-data caching and key rotation. If provider groups do not map directly to application roles, add an IdentityStore or an explicit claims-to-roles mapping.

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

Connect Java to Keycloak or another identity provider

Keycloak is a self-hosted identity provider with OIDC, OAuth 2.0, and SAML support. Its documented Java integrations include Spring Boot and WildFly Elytron OIDC. For a Java web application using OIDC, the setup follows the same client-registration model as other providers:

  1. Create or select a Keycloak realm for the users and applications involved.
  2. Register each Java application as a client in that realm. Configure the client type and authentication method to fit the application: a server-side application that can protect a secret differs from a public client that cannot.
  3. Set the application’s exact redirect URI and the appropriate allowed origins. Avoid broad wildcard settings where a precise value is possible.
  4. Configure the Java application with the realm’s issuer/discovery URI and the client details. Store confidential credentials outside source control.
  5. Map realm roles, client roles, or groups into claims the application is configured to consume, then map those claims to application authorities.

For an externally hosted enterprise IdP, use its OIDC discovery and client-registration details in the same way. SAML can remain the appropriate choice when an organization’s established federation or application requirements call for it; protocol choice should follow the provider and client types rather than Java language preference.

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

Secure and operate the sign-in flow

Before production, check each of these areas:

  • Redirect protection: register exact redirect URIs and use HTTPS. Configure logout return URIs deliberately as well.
  • Protocol checks: use the framework’s supported validation of the authorization response, issuer, audience, signature, expiry, state, nonce, and relevant claims. Do not bypass validation because a token arrived through a browser redirect.
  • Credential handling: keep client secrets out of source control and logs, restrict access to secret stores, and rotate credentials according to the provider’s procedures.
  • Access control: map claims and roles deliberately, grant only needed permissions, and enforce them at server-side boundaries.
  • Session lifecycle: define local session expiration and logout behavior, and decide whether refresh tokens are needed and how they will be protected. Signing out of the Java application does not necessarily terminate the IdP’s separate login session.
  • Provider changes: use the provider’s discovery metadata and advertised JWKS endpoint as intended, including its guidance for caching metadata and handling signing-key rotation.
  • Observability and failure handling: audit authentication events without recording credentials or tokens, and decide how the application responds to provider downtime, denied consent, invalid callbacks, or expired sessions.

Test the whole interaction in a staging tenant: initial redirect, successful callback, denied login, expired application session, logout, and role mapping. Test with the actual provider configuration because callback paths, claims, and logout behavior are provider- and runtime-dependent.

Common implementation mistakes

  • Using OAuth access tokens as identity assertions: use OIDC for user authentication and rely on validated identity information rather than assuming an access token is an ID token.
  • Redirect URI mismatch: compare the registered URI with the application’s actual callback, including HTTPS scheme and path.
  • Leaking a client secret: move it to runtime secret configuration and rotate it if it has been exposed.
  • Assuming roles arrive automatically: configure provider claim mappings and application authorization rules explicitly.
  • Confusing login with API protection: interactive OIDC login establishes a user session; an API that accepts bearer tokens needs resource-server configuration and its own authorization policy.

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.