Postauthorization evaluates an authorization rule after a method executes. The method is allowed to run, but its returned value is withheld when the result fails the rule. section 11.1.1 section 11.3
Implement postauthorization in Spring Security by following these steps:
- Enable method security with @EnableMethodSecurity on the configuration class. section 11.1.2
- Annotate the protected method with @PostAuthorize and provide a SpEL authorization expression. section 11.3
Protecting employee details
A BookService method can return an Employee only when its roles contain reader: @PostAuthorize("returnObject.roles.contains('reader')"). Emma’s record satisfies the rule, so the details are returned. Natalie’s record does not, so the request receives a 403 Forbidden response. Natalie can still retrieve Emma’s record because the authorization decision uses the returned employee, not the authenticated user alone.
