Broadcom conceptsSpring Professional DevelopSpring Security
2V0-72.22 Security: what the Spring exam tests
What the Spring Professional Develop exam asks about Security: the filter chain, how authentication works, Boot's defaults and method security.
3 min read
Add Spring Security to a Spring Boot web app and it is secured by default, even the /error endpoint. Yet the starter does not switch on method-level authorization until you ask for it.
01 What does the Spring Professional Develop exam test about Security?
It is the night before a mock exam, and your Security notes are a blur of class names. Sort them into three areas: the filter chain, authentication and method security.
Three areas of the Security domain and what to know in each
- Filter chainWhich chain handles a request, and what if none matches.
- AuthenticationWhere the current user is kept, and how a provider is picked.
- Method securityHow annotations are switched on, and which methods stay open.
Spring Security's Servlet support is built on Servlet Filters, so start with the chain.
02 What happens to a request before your controller runs?
With the areas sorted, follow one request. DelegatingFilterProxy hands the work to FilterChainProxy, which runs only the first SecurityFilterChain that matches.
How a request reaches Spring Security's filters
- DelegatingFilterProxy
- FilterChainProxy
- First matching SecurityFilterChain
- Authentication, then authorization
ProviderManager tries its AuthenticationProvider list and fails with ProviderNotFoundException if none can help. The user is then kept in SecurityContextHolder, a ThreadLocal by default.
A chain with no @Order is considered last, and a request no chain matches is unprotected.
03 What does Spring Boot secure before you write any config?
Before you write security code, Boot creates one user named user with a random password. Web requests are redirected to a login page, while service requests get a 401 Unauthorized. Adding your own SecurityFilterChain bean switches off Boot's default web security but keeps its UserDetailsService configuration.
A SecurityFilterChain bean replaces Boot's web security; a UserDetailsService bean replaces its default user.
04 Which methods does method security actually protect?
Finally, look past the URLs to the methods themselves.
Method security annotations and how to switch them on
On with @EnableMethodSecurity
- @PreAuthorize
- @PostAuthorize
- @PreFilter
- @PostFilter
Need an extra attribute
- @Secured: securedEnabled = true
- @RolesAllowed: jsr250Enabled = true
Unannotated methods are not secured, so keep a catch-all rule in HttpSecurity. A failed check throws AccessDeniedException, which becomes a 403 response.
Even with the Boot starter, method security stays off until you add @EnableMethodSecurity.
Summary
- Only the first matching SecurityFilterChain runs for a request.
- Boot secures web apps by default and generates one user named user.
- Method security needs @EnableMethodSecurity, and @Secured also needs securedEnabled = true.
Sources
- Architecture :: Spring Security (opens in a new tab)
- Servlet Authentication Architecture :: Spring Security (opens in a new tab)
- Method Security :: Spring Security (opens in a new tab)
- Spring Security :: Spring Boot (opens in a new tab)
- Java Configuration :: Spring Security (opens in a new tab)
- Hello Spring Security :: Spring Security (opens in a new tab)