Skip to content
Ameen Abozaid
JNR-040

Role-based authorization

  • 6 Lessons
  • 2.5 h practice (estimate)
On this page 8 topics

Overview

Topics

  • RBAC
  • method security
  • ownership checks

Learn first

2.5 h of practice (estimate)

Learning outcomes

  1. Model roles and authorities and apply RBAC at URL and method level (@PreAuthorize).
  2. Enforce ownership checks so users can access only their own resources.
  3. Explain insecure direct object references (IDOR) and test for them.
  4. Decide where authorization logic belongs (filter, method, domain query).

Lessons

  1. 01Roles, authorities, permissions
  2. 02Method security
  3. 03Ownership checks
  4. 04IDOR: the most common API bug
  5. 05Demo: finding and fixing an IDOR
  6. 06Demo: method security with ownership checks

Lab and exercises

Lab

JNR-040-LAB1 · My orders only

Outcomes: 1–4 · Time: ~2.5 h Customers see only their own carts and orders; admins see all. Acceptance: a test where customer A requests customer B's order receives 404 (no existence leak).

Exercises (assessment: find IDOR bugs in a provided API)

IDOutcomeTask
JNR-040-EX011Define roles and authorities for Shop (customer, admin) and map five endpoints to them.
JNR-040-EX021Protect OrderService.listAll() with @PreAuthorize and say what else must be enabled.
JNR-040-EX032Rewrite findById(id) so it only returns the caller's order.
JNR-040-EX042A create-order request body contains customerId. Explain the risk and fix it.
JNR-040-EX053In a provided API, find three IDOR bugs (read, update, delete) and write the attack request for each.
JNR-040-EX063Write a MockMvc test where customer A requests customer B's order.
JNR-040-EX074For each rule, pick filter, method, or query: "must be logged in"; "only admins list all orders"; "only own orders".
Show solutions
  • EX01 For example: GET /api/products public; POST /api/products ADMIN; GET /api/orders/{id} CUSTOMER with ownership; GET /api/orders ADMIN; POST /api/orders CUSTOMER.
  • EX02 @PreAuthorize("hasRole('ADMIN')") and @EnableMethodSecurity on a configuration class.
  • EX03 findByIdAndCustomerId(id, currentUserId); empty result leads to 404.
  • EX04 A customer could place orders as someone else; take the customer from the authenticated principal and delete the field from the request.
  • EX05 Change the ID in GET, PUT, and DELETE requests while logged in as another customer; each returns the other's data or modifies it.
  • EX06 Log in as A, request B's order id, status().isNotFound().
  • EX07 Must be logged in: filter (URL rule); only admins list all: method security; only own orders: domain query.

Quiz

Outcomes: O1 roles and method security · O2 ownership checks · O3 IDOR · O4 where authorization belongs.

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.

  1. 1In Spring, hasRole("ADMIN") matches the authority:O1
  2. 2@PreAuthorize needs:O1
  3. 3RBAC stands for:O1
  4. 4The current user's id should come from:O2
  5. 5For another customer's order, return:O2
  6. 6The preferred ownership check is:O2
  7. 7IDOR means:O3
  8. 8Using UUIDs instead of numbers:O3
  9. 9"Only admins may list all orders" is best placed in:O4
  10. 10"A customer sees only their own orders" is best placed in:O4

Practical part

Seven exercises JNR-040-EX01 to EX07 plus the lab; finding IDOR bugs in the provided API (EX05) is the assessment. Pass: at least six exercises correct, all three IDOR bugs found, and the 404 test passing.

Common mistakes

Checking roles but not ownership; trusting a customerId from the request body.

Assessment

Find IDOR bugs in a provided API.

Career relevance

Broken access control is the top OWASP risk; authorization design in MID-051.

Type to search levels, phases, modules and lessons.