spring Security Architecture Principles by Daniel Garnier-Moiroux @ Spring I/O 2024
Abstract
Filter
for security decisions on HTTP requests.Authentication
is the domain language of spring security.AuthenticationProvider
to validate credentials.Filter
+AuthenticationProvider
for custom login.A spring security configuration that can work for most web applications:
Filter
- When creating custom
Filter
, implementsOncePerRequestFilter
.
- Takes
HttpServletRequest, HttpServletResponse
.- Reads from request to:
- sometimes writes to
Response
,- sometimes does nothing!
- You can look at the spring logs on startup to check the order of the
Filter
- To debug any HTTP 401 issue, e.g. to know which
Filter
rejected your HTTP requests, you can downlevel the log level toTRACE
:
logging.level.org.springframework.security=TRACE
- execute your HTTP request, and look at the logs from
FilterChainProxy
Authentication objects
spring Security produces
Authentication
objects, used for:
- Authentication: who is the user?
- Authorization: is the user allowed to perform XYZ?
Vocabulary:
Principal
: user “identity” (name, email, …)GrantedAuthorities
: “permissions” (roles, …).isAuthenticated()
: almost alwaystrue
details
: details about the request(Credentials)
: “password”, oftennull
Use
SecurityContextHolder.getContext().getAuthentication()
to get yourAuthentication
object. TheSecurityContext
is:
- Thread-local,
- not propagated to child threads,
- cleared after requests is processed.
Example of setting your custom
Authentication
in a customFilter
:Some
Filter
produce anAuthentication
:
- read the request and convert to “domain”
Authentication
object,- authenticate (are the credentials valid?),
- save the
Authentication
in theSecurityContext
- or reject the request when credentials are invalid.
AuthenticationProvider
Implement your custom authentication provider by implementing the interface
AuthenticationProvider
:Then add it to your
SecurityConfig
.You can add an
ApplicationListener
to audit who logged in your application:
Authentication
is both an auth request and a successful auth result.AuthenticationProvider
validates credentials.
- Operates only within the “auth” domain (no HTTP, HTML, …).
AuthenticationProvider
leverages spring security infrastructure.