Advanced API security
On this page 8 topics
Overview
Topics
- OAuth 2.0 flows
- OIDC
- token validation
- authorization design
Learn first
2.5 h of practice (estimate)
Learning outcomes
- Choose OAuth 2.0 flows for user-facing, machine-to-machine, and delegated scenarios.
- Validate tokens fully at every service, not only at the gateway.
- Design scopes and claims for fine-grained authorization, and propagate user identity between services.
- Apply token exchange or client credentials for service-to-service calls.
Lessons
Lab and exercises
Lab
MID-051-LAB1 · Zero implicit trust
Outcomes: 1–4 · Time: ~2.5 h Every Shop service validates tokens; orders calls inventory with its own client credentials; scopes restrict admin operations. Acceptance: a direct call without a valid token fails at every service.
Exercises (assessment: authorization design review)
| ID | Outcome | Task |
|---|---|---|
| MID-051-EX01 | 1 | Choose a flow for each: web storefront, mobile app, nightly report job, support tool acting for a customer. |
| MID-051-EX02 | 2 | Configure a service as a resource server with issuer and audience validation. |
| MID-051-EX03 | 2 | Write tests: expired token, wrong audience, missing token, tampered signature. |
| MID-051-EX04 | 3 | Design scopes and roles for orders and inventory and fill in the endpoint matrix. |
| MID-051-EX05 | 3 | Add an ownership check and a negative test for reading another customer's order. |
| MID-051-EX06 | 4 | Configure orders to obtain a client-credentials token for inventory; limit its scopes. |
| MID-051-EX07 | 1–4 | Review a given design (gateway-only validation, header-based identity, shared service account) and list fixes. |
Show solutions
- EX01 Storefront and mobile: authorization code with PKCE; nightly job: client credentials; support tool acting for the customer: token exchange (or a delegated, audited flow).
- EX02
issuer-uriplus aJwtValidatorcheckingaud; verify the exact Spring Security version API before recording. - EX03 Expect 401 for missing, expired, tampered, and wrong audience tokens.
- EX04 Example:
orders:readcustomer;orders:adminsupport role;inventory:writeonly forordersclient and admin. - EX05 Compare the order's owner with the token subject; return 404 or 403 for others; test with two users.
- EX06 Register an
ordersclient with theinventory:readscope only; use a client-credentialsWebClientfilter. - EX07 Validate tokens in every service, take identity from the token, one client per service with minimal scopes.
Quiz
Outcomes: O1 OAuth flows · O2 token validation · O3 scopes, claims, authorization design · O4 service-to-service identity.
A self-check for you: nothing is graded, saved or sent anywhere. Choose an answer, then check it. You can try again as often as you like.
Practical part
Seven exercises MID-051-EX01 to EX07 (EX07 is the authorization design review) plus the zero-implicit-trust lab. Pass: at least five exercises including EX07, and a lab where a direct call without a valid token fails at every service.
Common mistakes
Trusting a header set by the gateway; one all-powerful service account.
Assessment
Authorization design review.
Career relevance
Zero-trust security architecture (SRB-016).