Skip to content
Tutorials Center
My learning

Topics

Lesson 25 of 39 ยท OAuth2 Login and OIDC

OAuth2 Roles and Grant Types Explained

Understand OAuth2: the four roles, the authorization code flow step by step, state and redirect URI checks, and which grant type fits web apps, SPAs and services.

"Log in with Google" and "let this app read my GitHub repositories" both rely on OAuth 2.0, a protocol that lets one application get limited access to a user's account at another service without ever seeing the user's password. Spring Security supports it as a client (your app logs users in through a provider), as a resource server (your API accepts tokens, see Resource server configuration) and, with a separate project, as an authorization server.

OAuth2 is about authorization (what may this app do?). Signing a user in, which is authentication, is added by OpenID Connect on top of OAuth2, covered in OIDC login and ID tokens.

The four roles

Role Meaning Example
Resource owner The user who owns the data You
Client The app that wants access Your Spring Boot web app
Authorization server Authenticates the user, asks for consent, issues tokens Google, Keycloak, Okta
Resource server The API that holds the data and accepts access tokens Google Calendar API

Who is who

Resource owner The user
Client Your app
Authorization server Issues tokens
Resource server The API
1 / 2

The authorization code flow

This is the flow for web applications with a server side, and the one Spring Security's oauth2Login() uses. The browser is redirected to the provider, the user signs in there, and the provider sends the browser back with a short-lived authorization code. The server then exchanges the code for tokens over a direct, back-channel request.

sequenceDiagram
    participant U as User browser
    participant C as Client (Spring app)
    participant A as Authorization server
    U->>C: Click "Log in with Provider"
    C-->>U: 302 to /authorize?client_id&redirect_uri&scope&state
    U->>A: Follow redirect, sign in, consent
    A-->>U: 302 to redirect_uri?code=abc&state=xyz
    U->>C: GET /login/oauth2/code/provider?code&state
    C->>A: POST /token (code, client_secret)
    A-->>C: access token (+ ID token, refresh token)

Tokens travel on the back channel, never through the browser's address bar.

Why the extra code step? Anything in a redirect URL ends up in browser history and logs. The code is useless without the client's secret (or, for public clients, a PKCE verifier, see PKCE explained), so leaking it is harmless.

State: protecting the redirect

The state parameter is a random value the client creates, remembers (in the session) and checks when the provider calls back. It stops an attacker from tricking your app into completing a login that the attacker started (a form of login CSRF). The provider also validates that redirect_uri is exactly one of the URIs registered for the client: prefix or wildcard matching has caused many account takeovers.

The next program simulates the whole flow with three in-memory parties. It is a stand-in: the real HTTP redirects and provider consent screens cannot run here, but the logic of codes, one-time use and state matching is the same.

Java Authorization code flow simulator (stand-in)

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with IO.readln() or a Scanner), it gets the next line from this box.

Other grant types

Grant Use for Status
Authorization code (+ PKCE) Web apps, SPAs, mobile and desktop apps The default for user logins
Client credentials Server-to-server calls with no user Current, simple
Refresh token Getting new access tokens Current, see Refresh tokens
Device code TVs and CLIs without a browser Current
Implicit Old SPAs (token in the URL) Deprecated, do not use
Resource owner password App collects the user's password Deprecated, do not use

For service-to-service calls Spring offers WebClient or RestClient with an OAuth2AuthorizedClientManager that fetches and caches a client-credentials token automatically:

Java
@Bean
OAuth2AuthorizedClientManager authorizedClientManager(
        ClientRegistrationRepository registrations, OAuth2AuthorizedClientService clients) {
    var provider = OAuth2AuthorizedClientProviderBuilder.builder().clientCredentials().build();
    var manager = new AuthorizedClientServiceOAuth2AuthorizedClientManager(registrations, clients);
    manager.setAuthorizedClientProvider(provider);
    return manager;
}

Scopes

A scope names a permission the client requests, like read:repos or openid email. The user sees them on the consent screen, and the access token is limited to them. Ask for the least you need: users abandon consent screens that demand everything.

An access token proves the app may call the API, not who the user is. Never treat "I received an access token" as a login. Use an ID token from OpenID Connect for that.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. In the authorization code flow, what does the client exchange the code for?

    The code travels through the browser, but the token request goes straight from the client's server to the authorization server.
  2. 2. What is the purpose of the state parameter?

    The client stores a random state, then verifies the callback returns the same value.
  3. 3. Which grant type fits a backend job calling another service with no user involved?

    Client credentials uses the app's own identity and has no user.
  4. 4. Why must the redirect URI be matched exactly?

    If a registered URI could be extended or wildcarded, an attacker could receive the authorization code.

Summary

  • OAuth2 lets a client act on a user's behalf without the user's password; four roles take part.
  • The authorization code flow keeps tokens off the front channel; codes are one-time.
  • state and exact redirect URI matching defend the callback.
  • Use authorization code (with PKCE) for users and client credentials for services; avoid implicit and password grants.
  • OAuth2 is authorization; OpenID Connect adds authentication.

Next: PKCE explained