Tuesday, December 1, 2026

Spring Authorization Server: from zero to a working OIDC provider

Part 2 of the OIDC series. This is the piece everything else hangs off: standing up a real authorization server with Spring Authorization Server (SAS), and learning where the framework ends and your responsibility begins.

What SAS gives you for free

Drop spring-boot-starter-oauth2-authorization-server on the classpath, register a client, and you immediately get the full protocol surface:

  • /.well-known/openid-configuration — discovery, generated correctly
  • /oauth2/authorize, /oauth2/token, /oauth2/jwks, /oauth2/introspect, /oauth2/revoke, /userinfo
  • Authorization Code + PKCE, Refresh Token, Client Credentials grants
  • A consent screen when a client asks for scopes the user hasn't approved

The honest surprise: the protocol endpoints are the easy 20%. SAS is deliberately a protocol engine, not a product — everything around the endpoints is yours to build.

The four repositories that make it real

SAS's default wiring is in-memory — restart and every client, token, and consent evaporates. Persistence means backing four contracts with real storage (JDBC implementations ship with SAS; Flyway migrates the schema):

  • RegisteredClientRepository → oauth2_registered_client — who may ask for tokens, how, and which grant types/scopes they're allowed
  • OAuth2AuthorizationService → oauth2_authorization — every issued token's server-side record (this is what makes revocation work)
  • OAuth2AuthorizationConsentService → oauth2_authorization_consent — "alice approved demo-web for api:read", remembered
  • Your own UserDetailsService → users/authorities — SAS has no user store; you bring it

What nobody warns you about

The gaps between "running SAS" and "running an IdP":

  • Signing keys. SAS generates an ephemeral RSA key at boot — restart invalidates every token in flight. Production needs keys in a table (oidc_signing_keys), wrapped under a KEK env var, with scheduled rotation and a JWKS read cache that keeps serving the last good set through a DB blip.
  • The sub claim. The obvious mapping — username as sub — is a latent account-confusion bug. sub must be an immutable identifier (UUID column, backfilled by migration); the username stays in preferred_username.
  • Two security domains, not one. Your IdP has a browser surface (login form, admin UI — session cookies, CSRF on) and a protocol surface (token endpoints, admin API — stateless, Basic/bearer, CSRF off, 401 not redirect). Mixing them is how "CSRF disabled for convenience" becomes an actual hole.
  • Client auth method matters. client_secret_post puts secrets in request bodies — which land in access logs. Register clients with client_secret_basic only, and migrate old rows to match.
  • Rate limiting is on you. Every credential endpoint (/login, /oauth2/token, /introspect, /userinfo) is a brute-force surface. Per-IP throttling — Redis-backed if you run replicas, per-instance otherwise — isn't optional.

The dev-loop trick that pays for itself

A dev profile on embedded H2 with seeded data — demo users (alice/password), a confidential web client, an M2M service client — means the entire login roundtrip is one mvn spring-boot:run away. It also keeps the prod profile honest: prod gets a startup validator that refuses to boot on missing env vars rather than silently falling back to dev secrets.

Verifying it end to end

Once the provider runs, the first real test is a full authorization-code roundtrip against the relying party (Part 3):

  1. Browser hits the client app → redirected to /oauth2/authorize
  2. Login form → consent screen → redirect back with ?code=
  3. Client exchanges code at /oauth2/token → ID token + access token
  4. /userinfo returns sub = UUID, preferred_username = alice

Watching your own server complete that handshake is the moment the protocol stops being folklore.

Next: the other side of the handshake — building the relying party, and what Spring Security's OAuth2 client handles vs. what it leaves to you.

No comments:

Post a Comment

Java, JavaScript, HTML, XHTML, AJAX, CSS, etc.