BackendBench
Coffee

Unravel โ€บ Java concepts โ€บ Security

Spring

OAuth 2.0 and OpenID Connect login

OAuth 2.0 lets users sign in or let one service call another without sharing a password.

Learn these first

At a glance

Mental model

A hotel valet key.

What happens, in order
  1. 1Roles: the user (resource owner), the client application, the authorization server (identity provider) and the resource server (API)Spring can play the client (oauth2Login), the resource server (oauth2ResourceServer) or, with Spring Authorization Server, the provider itself.
  2. 2Authorization Code flow with PKCE for browsers and apps: redirect the user to the provider, they log in, the provider redirects back with a one-time code, and the client exchanges the code for an access token (and an ID token with OpenID Connect)
  3. 3Client Credentials flow for service-to-service calls: the service authenticates with its own id and secret and receives a token with no user involvedOAuth2AuthorizedClientManager fetches and refreshes it for WebClient calls.
  4. 4Scopes limit what a token may do; the API checks them (SCOPE_orders.read) or maps roles from claims
Remember
Watch out
Using implicit or password grantsNot validating the audienceClient secret in front-end code or GitForgetting the state and redirect URI checks in custom code
Example (highlighted lines explain it)
@BeanSecurityFilterChain resourceServer(HttpSecurity http) throws Exception {    http.authorizeHttpRequests(a -> a            .requestMatchers(HttpMethod.GET, "/orders/**").hasAuthority("SCOPE_orders.read")            .anyRequest().authenticated())        .oauth2ResourceServer(o -> o.jwt(Customizer.withDefaults()));    return http.build();}
  1. line 6 uses oauth2ResourceServer, the core of OAuth 2.0 and OpenID Connect login
Read the full explanation (about 2 min)

OAuth 2.0 lets users sign in or let one service call another without sharing a password. A trusted identity provider (Keycloak, Okta, Entra ID, Google) authenticates the user and issues tokens; OpenID Connect adds a standard identity layer on top.

A mental model

A hotel valet key. You give the valet (an app) a key that can only drive the car a short distance, not open the glovebox or the boot, and you can cancel it without changing your own keys.

How it works

Roles: the user (resource owner), the client application, the authorization server (identity provider) and the resource server (API). Spring can play the client (oauth2Login), the resource server (oauth2ResourceServer) or, with Spring Authorization Server, the provider itself.

Authorization Code flow with PKCE for browsers and apps: redirect the user to the provider, they log in, the provider redirects back with a one-time code, and the client exchanges the code for an access token (and an ID token with OpenID Connect).

Client Credentials flow for service-to-service calls: the service authenticates with its own id and secret and receives a token with no user involved. OAuth2AuthorizedClientManager fetches and refreshes it for WebClient calls.

Scopes limit what a token may do; the API checks them (SCOPE_orders.read) or maps roles from claims.

Going deeper

Use the code flow with PKCE; the implicit flow and the password grant are deprecated because tokens leak through URLs or apps see the user password.

Validate tokens fully on the API: issuer, audience, signature, expiry. Setting only issuer-uri gives signature and issuer checks; add an audience validator so a token meant for another API is not accepted.

Client secrets belong in a secret store; public clients (single-page apps, mobile) have no secret and rely on PKCE.

Under the hood

Spring registers filters for the flows: OAuth2AuthorizationRequestRedirectFilter builds the redirect with a state value stored in the session to prevent CSRF, OAuth2LoginAuthenticationFilter receives the callback and exchanges the code, and BearerTokenAuthenticationFilter reads Bearer tokens for resource servers and verifies them through the JwtDecoder or an opaque-token introspection endpoint.

Common mistakes

  • Using implicit or password grants.
  • Not validating the audience.
  • Client secret in front-end code or Git.
  • Forgetting the state and redirect URI checks in custom code.

How Unravel recognises it

When you paste code, Unravel looks for these signs of oauth 2.0 and openid connect login. Spotting them yourself is a good habit when you read unfamiliar code:

Part of a learning path

Related concepts

Frequently asked questions

What is OAuth 2.0 and OpenID Connect login?

OAuth 2.0 lets users sign in or let one service call another without sharing a password. A trusted identity provider (Keycloak, Okta, Entra ID, Google) authenticates the user and issues tokens; OpenID Connect adds a standard identity layer on top.

What are the most common mistakes with OAuth 2.0 and OpenID Connect login?

Using implicit or password grants. Not validating the audience. Client secret in front-end code or Git. Forgetting the state and redirect URI checks in custom code.

How can I see OAuth 2.0 and OpenID Connect login in real code?

Paste your own Java into Unravel and it will point out where oauth 2.0 and openid connect login appears, explain how it works at the detail level you choose, and draw the flow of the code around it.