Spring Boot Tutorial 0/110 lessons ~6 min read Lesson 46

    OAuth2 Basics

    OAuth 2.0 delegates authorization: users sign in with an identity provider (Google, GitHub, corporate IdP) without giving Orders their IdP password.

    Course progress0%
    Focus
    13 guided sections
    Practice signal
    Examples included
    Career prep
    Interview Q&A included

    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_token carries authenticated user identity claims.
    • Scopes: limit what the client may do on behalf of the user.

    Step-by-step explanation

    1. Decide whether this process is a client, resource server, or both.
    2. Register an OAuth2 client at the IdP; store id/secret as ${OIDC_CLIENT_ID} / ${OIDC_CLIENT_SECRET}.
    3. For API-only Orders, configure JWT resource server (issuer-uri / JWK set).
    4. For browser login, enable OAuth2 login and map authorities from claims.

    Syntax reference

    Authorization Code + PKCE (simplified).

    text
    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.

    yaml
    # application.yml
    spring:
    security:
    oauth2:
    client:
    registration:
    google:
    client-id: ${OIDC_CLIENT_ID}
    client-secret: ${OIDC_CLIENT_SECRET}
    scope: openid,profile,email
    provider:
    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.

    3 questions
    1BeginnerQuestionOAuth2 vs OIDC?+

    Answer

    OAuth2 delegates authorization; OIDC adds authentication/identity (id_token) on top of OAuth2.
    2IntermediateQuestionWhy PKCE?+

    Answer

    It protects public clients from authorization-code interception without requiring a client secret.
    3IntermediateQuestionClient vs resource server in Spring?+

    Answer

    Client obtains tokens / performs login; resource server protects APIs by validating access tokens.

    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.

    Ready to mark this lesson complete?Track your journey across the entire course.