OAuth2 Basics
OAuth 2.0 delegates authorization: users sign in with an identity provider (Google, GitHub, corporate IdP) without giving Orders their IdP password.
Introduction
OAuth 2.0 delegates authorization: users sign in with an identity provider (Google, GitHub, corporate IdP) without giving Orders their IdP password. OpenID Connect (OIDC) adds an identity layer (id_token) on top.
Spring Security supports OAuth2 client (login with IdP) and resource server (accept access tokens). Orders APIs often act as resource servers; a BFF or auth service acts as client.
For public clients, Authorization Code + PKCE is the modern default. Avoid the implicit flow. Keep client-id / client-secret in env placeholders — never hard-code secrets.
This lesson orients the flows; JWT and refresh lessons already cover token shapes Orders will see.
Purpose of this lesson
Explain OAuth2/OIDC roles and configure Spring as client or resource server for Orders without embedding secrets.
Understanding the topic
Roles: resource owner, client, authorization server, resource server.
- Authorization Code + PKCE: browser/mobile friendly, no secret in the SPA.
- Client credentials: service-to-service without a user present.
- Resource server: Orders API validating access tokens.
- OIDC:
id_tokencarries authenticated user identity claims. - Scopes: limit what the client may do on behalf of the user.
Step-by-step explanation
- Decide whether this process is a client, resource server, or both.
- Register an OAuth2 client at the IdP; store id/secret as
${OIDC_CLIENT_ID}/${OIDC_CLIENT_SECRET}. - For API-only Orders, configure JWT resource server (issuer-uri / JWK set).
- For browser login, enable OAuth2 login and map authorities from claims.
Syntax reference
Authorization Code + PKCE (simplified).
Browser Orders/BFF IdP| hit protected | ||---------------->| || redirect auth | ||<----------------| || /authorize + code_challenge ||--------------------------------------->|| login + consent ||<---------- ?code=... -----------------|| /callback code | code+verifier ||---------------->|--------------------->|| | tokens || |<---------------------|| established | ||<----------------| |
Informative example
Client registration YAML with placeholders.
# application.ymlspring:security:oauth2:client:registration:google:client-id: ${OIDC_CLIENT_ID}client-secret: ${OIDC_CLIENT_SECRET}scope: openid,profile,emailprovider:google:issuer-uri: https://accounts.google.com# Resource server (Orders API)# spring.security.oauth2.resourceserver.jwt.issuer-uri: ${JWT_ISSUER_URI}
Real-world use
TechLearningPRO employees access an internal Orders admin UI via corporate OIDC. Customers use password + JWT on the public API. Separating client login from resource-server validation lets the same order services stay unaware of Google vs Okta specifics — they only trust validated tokens.
Security implications
OAuth2 shifts trust to the IdP and token validation.
- Misconfigured redirect URIs enable code theft.
- Always use HTTPS for authorize and token endpoints.
- Rotate client secrets; prefer secret-less patterns where supported.
Best practices
- PKCE for public clients; confidential clients still protect secrets.
- Validate issuer, audience, and expiry on every token.
- Request least-privilege scopes.
Common mistakes
- Embedding real client secrets in Git.
- Using implicit flow or password grant for new apps.
- Confusing IdP access tokens with your own Orders access tokens without validating audience/issuer.
Advanced interview questions
Interview Prep
Practice concise answers, then expand each card for the explanation.
1BeginnerQuestionOAuth2 vs OIDC?+
Answer
id_token) on top of OAuth2.2IntermediateQuestionWhy PKCE?+
Answer
3IntermediateQuestionClient vs resource server in Spring?+
Answer
Hands-on exercise
Lab. Configure a resource-server issuer-uri with placeholders. Decode a sample JWT's issuer/audience claims (use a disposable test token) and write the validation rules you expect Spring to enforce.
Summary
OAuth2/OIDC explain how identity providers and Orders APIs share trust. Next: hardening REST surfaces end to end.