Spring Boot services · Chapter 28 of 41

Roles and authorities

By the end of this chapter you can decide what each logged-in user may do: roles for who they are, authorities for single permissions. Build it in your browser: a real editor and terminal, AI that explains, and tests that tell you when it works.

  • Part: Security
  • 5 tasks
  • Spring Boot
  • In the all labs plan

The first 2 chapters of Spring Boot services are free. This one comes with the all labs plan.

Why you need this

Priya is a customer, and last night she opened the sales report:

$ curl -u priya:chai123 localhost:8081/reports/sales
{"station":"SBC","orders":412,"total":14380}

Ravi wants it plain: customers order, managers see the numbers, Meera runs the menu, and refunds go only to people he trusts with money.

So far Tapri Cloud only asks "are you logged in?". You could check the caller inside every controller method, but that repeats itself, and the one method someone forgets is open to every customer. The rules belong in the filter chain, in one place, before any controller runs.

What you build: 5 tasks

Each task has tests. You write the code in the lab's editor and press Check.

  1. Task 1

    SecurityConfig.java: only managers see the sales report

    Add a rule so GET /reports/sales needs the role MANAGER: 401 Unauthorized when nobody is logged in, 403 Forbidden for a customer (role USER), and 200 with the report (station SBC) for a manager.

    GET /menu must stay open to everyone.

  2. Task 2

    SecurityConfig.java: a refund needs the orders:refund authority

    Add a rule so POST /orders/{id}/refund needs the authority orders:refund, whatever the caller's role: a manager or a customer without it gets 403, and a caller whose only authority is orders:refund gets 200 with {"orderId": 42, "status": "REFUNDED"} for POST /orders/42/refund.

  3. Task 3

    SecurityConfig.java: only admins take an item off the menu

    Add a rule so DELETE /menu/{name} needs the role ADMIN: a customer or a manager gets 403, and an admin gets 204 No Content (DELETE /menu/Bun maska).

  4. Task 4

    SecurityConfig.java: ADMIN includes MANAGER, and MANAGER includes USER

    Declare a RoleHierarchy bean in which ADMIN includes MANAGER and MANAGER includes USER, so ROLE_ADMIN reaches ROLE_ADMIN, ROLE_MANAGER and ROLE_USER.

    With task 1's rule, an admin then gets 200 on GET /reports/sales, a manager places an order at POST /orders (201, customer anjali), and a customer still gets 403 on the report.

  5. Task 5 · interview question

    Interview: fix Ravi's account so he has both his role and his authority

    Fix Ravi's account in the users bean so his authorities are exactly ROLE_MANAGER and orders:refund. With tasks 1 and 2 done, he then logs in with HTTP Basic (ravi, chai123) and gets 200 on GET /reports/sales and on POST /orders/7/refund.

    Be ready to explain the difference between a role and an authority, and why his role went missing.

What you will learn

  • Authorities and roles: the strings a user carries, and the naming rule that makes one a role.
  • Rules on requests: hasRole and hasAuthority, tried top to bottom, first match wins.
  • A role hierarchy: one bean that says a bigger role includes a smaller one.

More in Security

Build "Roles and authorities" yourself

Tapri Cloud: the counters’ Java grows into a Spring Boot service with a database. A real editor and terminal in your browser, AI that explains, and a Check that says when it works.

The first 2 chapters of Spring Boot services are free. This one comes with the all labs plan.