LAB-016: API Gateway¶
Status: Verified
Theory: API Gateway
Objective¶
Add a gateway module that validates a JWT bearer token, enforces scope-based route authorization, and returns the enriched headers that would be forwarded to a downstream service.
Implementation map¶
| Artifact | Purpose |
|---|---|
gateway/build.gradle | New module with spring-boot-starter-web, security, oauth2-resource-server, and the common-security library |
GatewayApplication | Boot entry point |
GatewayConfig | JWT resource-server chain with SCOPE_USER and SCOPE_ADMIN route rules |
GatewayController | Learning routes that echo the caller, target, and forwarded headers |
GatewayLabTest | Five scope, authentication, and header-forwarding tests |
Exercises¶
- Request
/gateway/user/routewith aUSERscoped token. - Request
/gateway/admin/routewith aUSERtoken and observe 403. - Request
/gateway/admin/routewith anADMINtoken. - Request without a token and observe 401.
- Inspect the
forwardedresponse block and confirmX-User-SubjectandX-User-Scopeare present. - Discuss why a real gateway must not log or return the original bearer token.
- Document how a
WebClientorRestClientproxy would route totargetwith the enriched headers.
Verification¶
Five gateway assertions must pass.
Attack checks¶
- Confirm the gateway does not echo the bearer token value in the response.
- Confirm a
USERtoken cannot reach the admin route. - Confirm missing tokens produce 401, not 403.
- Confirm the
scopeclaim is converted toSCOPE_*authorities.
Production extension¶
Use Spring Cloud Gateway with TokenRelayGatewayFilterFactory, per-route scopes, rate limiting, TLS, and token exchange. Ensure downstream services independently validate the forwarded token or an exchanged token scoped to their audience.
Review¶
Complete the gateway questions in the SSO and Federation Quiz.
Next: LAB-017 SAML Relying Party