Authorization — What May You Do?
Asha has logged in to library-service. She may borrow books, but should she be able to delete one from the catalogue? Knowing who someone is does not answer what they may do. That second question is authorization, and Spring Security lets you answer it at the URL, at the method, or both.
The rule
Authorization asks what a known user may do — by URL first, by method for detail.
Every authenticated user carries a list of authorities, simple strings such as ROLE_MEMBER, ROLE_LIBRARIAN or SCOPE_books.write. A role is just an authority whose name starts with ROLE_. That is why hasRole("LIBRARIAN") checks for the authority ROLE_LIBRARIAN: the prefix is added for you.
URL rules go in the filter chain configuration. They are checked top to bottom, and the first match wins:
http.authorizeHttpRequests(auth -> auth
.requestMatchers(HttpMethod.GET, "/books/**").permitAll()
.requestMatchers(HttpMethod.DELETE, "/books/**").hasRole("LIBRARIAN")
.requestMatchers("/books/*/borrow").hasAnyRole("MEMBER", "LIBRARIAN")
.anyRequest().authenticated());Method rules go on the service itself, after turning them on with @EnableMethodSecurity on a configuration class:
@PreAuthorize("hasRole('LIBRARIAN') or #memberId == authentication.principal.id")
public List<Loan> loansFor(long memberId) { ... }The expression can use the method's parameters, so it can say "librarians, or the member asking about their own loans", which a URL rule cannot. (principal.id assumes your own UserDetails class has an id field.) Method security runs through a proxy, so the self-call trap from the AOP lesson applies here too.