Spring Boot services · Chapter 29 of 41
Method security
By the end of this chapter you can put security rules on service methods, so they hold however a method is called, and write rules that depend on the arguments, the result or another bean. 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
The URL rules from last chapter guard doors, and Kabir has found a window. The nightly job that emails reports calls OrderService.salesReport directly, and so will the partner dashboard. Neither goes through /reports/sales, so neither meets its rule.
Ravi has a second problem. "Priya should see her orders, not Rahul's. And I run Bengaluru: I close my counter, not Anjali's in Delhi." A URL pattern cannot say that: it does not know whose order or station it is.
What you build: 5 tasks
Each task has tests. You write the code in the lab's editor and press Check.
- Task 1
OrderService.java: switch on method security and keep the sales report for managers
Switch method security on in SecurityConfig, and put a @PreAuthorize rule on OrderService.salesReport so only the role MANAGER may call it, however it is reached.
A direct call to the service while logged in as priya (role USER) throws AccessDeniedException; GET /reports/sales answers 403 for priya and 200 for ravi (MANAGER) with {"station":"SBC","orders":2,"total":130}.
- Task 2
OrderService.java: a customer sees only their own orders
Guard OrderService.ordersOf(username) so it runs only when username is the logged-in user's name, or when the caller has the role MANAGER, checked before the method runs.
GET /customers/priya/orders as priya answers 200 with her one order (Masala chai); GET /customers/rahul/orders answers 403 for priya and 200 with two orders for ravi.
- Task 3 · interview question
Interview: check a single order after loading it, with @PostAuthorize
Guard OrderService.find(id) with @PostAuthorize so the loaded order is returned only when its customer is the logged-in user's name, or when the caller has the role MANAGER.
GET /orders/1 answers 200 for priya (her order); GET /orders/2 answers 403 for priya and 200 for rahul; GET /orders/3 answers 200 for ravi; GET /orders/99 still answers 404.
Be ready to explain when @PostAuthorize runs, and why it is wrong on a method that changes data.
- Task 4
OrderService.java: only admins close the day, with @Secured
Guard OrderService.closeDay with @Secured so only the role ADMIN may call it, and switch @Secured on in SecurityConfig (it is off by default).
POST /day/close answers 403 for ravi (MANAGER) and priya (USER), and 200 for meera (ADMIN) with {"station":"SBC","orders":2}.
- Task 5
StationAccess.java: a manager runs only their own station's counter
Write StationAccess.manages(authentication, station) so it answers true only when MANAGER_OF maps the logged-in user's name to that station (ravi to SBC, anjali to NDLS). Then guard OrderService.setCounter(station, open) so it runs only for the role MANAGER and only when the "stations" bean says the caller manages the station.
PUT /stations/SBC/counter?open=false as ravi answers 200 with {"station":"SBC","open":false}; ravi on NDLS gets 403; anjali on NDLS gets 200; priya gets 403, and so does a caller named ravi whose role is only USER.
What you will learn
- Method security: rules on service methods, checked by a proxy.
- Expressions: rules that read the logged-in user, the arguments and other beans.
- @PostAuthorize: a check on what a method returned (the interview question).
- @Secured: the older, simpler annotation.
More in Security
Build "Method security" 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.