OAuth 2.0, OpenID Connect and SAML
Roles, grant types and flows, tokens, PKCE, OIDC ID tokens, SAML federation, OAuth 2.1 and security best current practice, with Spring Boot and cloud examples.
The three protocols
| Protocol | Purpose | Token/assertion | Typical use |
|---|---|---|---|
| OAuth 2.0 (RFC 6749) | Delegated authorization: a client gets limited access to an API on behalf of a user or itself | Access token (often JWT), refresh token | APIs, mobile apps, service-to-service |
| OpenID Connect (on top of OAuth 2.0) | Authentication: who the user is | ID token (JWT) + UserInfo endpoint | Login/SSO for web and mobile apps |
| SAML 2.0 | Authentication/SSO federation via XML assertions | SAML assertion | Enterprise SSO to SaaS, legacy apps |
Roles
Resource owner (user), client (app), authorization server (IdP: Entra ID, Okta, Auth0, Keycloak, Cognito, Ping), resource server (API).
Flows to use (and avoid)
| Scenario | Flow |
|---|---|
| Web app with backend, SPA via BFF, mobile app | Authorization code + PKCE |
| Service-to-service (no user) | Client credentials (with mTLS or private_key_jwt client auth for high security) |
| Devices without browsers (TVs, CLIs) | Device authorization grant (RFC 8628) |
| Delegation across services | Token exchange (RFC 8693) |
| Avoid | Implicit flow, resource owner password credentials (removed in OAuth 2.1 draft) |
sequenceDiagram participant U as User browser participant C as Client (web app / BFF) participant AS as Authorization server participant API as Resource server C->>C: create code_verifier + code_challenge (PKCE) C->>U: redirect to /authorize?response_type=code&client_id&scope=openid orders.read&code_challenge&state U->>AS: login + MFA + consent AS-->>U: redirect back with code + state U->>C: code C->>AS: POST /token (code + code_verifier + client auth) AS-->>C: access token, ID token, refresh token C->>API: GET /orders (Authorization: Bearer access_token) API->>API: validate signature (JWKS), iss, aud, exp, scope API-->>C: data
Best current practice (RFC 9700, 2025)
- Use PKCE for all clients using the code flow.
- Exact redirect URI matching; use
state/nonce against CSRF and replay. - Sender-constrained tokens (mTLS-bound or DPoP) for high-value APIs.
- Short-lived access tokens; refresh token rotation for public clients.
- Validate
audso tokens for one API cannot be replayed at another. - For browser apps, prefer a BFF that keeps tokens server-side.
FAPI 2.0 (OpenID Foundation) profiles these for financial-grade APIs (open banking): PAR (pushed authorization requests), sender-constrained tokens, strong client authentication.
SAML vs OIDC
SAML remains common for enterprise SaaS SSO. OIDC is simpler (JSON/REST), mobile-friendly and the default for new applications. Many IdPs bridge both.
Spring Boot client login (OIDC)
spring:
security:
oauth2:
client:
registration:
corp:
client-id: orders-web
client-secret: ${OIDC_CLIENT_SECRET}
scope: openid, profile, orders.read
authorization-grant-type: authorization_code
provider:
corp:
issuer-uri: https://login.example.com/oauth2/defaultInterview questions and answers
What is the difference between authentication and authorization in OAuth/OIDC?
OIDC authenticates the user and gives the client an ID token describing them. OAuth access tokens authorize the client to call an API with given scopes. An API should never accept an ID token as an access token.
Why is PKCE needed?
It binds the authorization code to the client that started the flow, preventing interception attacks where a malicious app steals the code (especially for public clients that cannot keep a secret).
How do services authenticate to each other?
Client credentials grant with strong client authentication, or workload identity with mTLS (service mesh, SPIFFE), combined with per-service authorization policies.