AuthenticationManager
The main interface for authentication in Spring Boot is AuthenticationManager, which has a
single method:
public interface AuthenticationManager {
Authentication authenticate(Authentication authentication) throws AuthenticationException;}authenticate() can do one of three things:
- Return an
Authentication(normally withauthenticated=true) if it can verify the input represents a valid principal. - Throw an
AuthenticationExceptionif it believes the input represents an invalid principal. - Return
nullif it can’t decide.
AuthenticationProvider
AuthenticationManager doesn’t perform authentication itself — it delegates to an appropriate
AuthenticationProvider, which is like the manager but with an extra method to let the caller
check whether it supports a given Authentication type:
public interface AuthenticationProvider {
Authentication authenticate(Authentication authentication) throws AuthenticationException;
boolean supports(Class<?> authentication);}The pieces, end to end
UserDetailsService— has a method to load a user by username and return aUserDetailsobject Spring Security can use for authentication and validation.UserDetails— contains the information needed to build anAuthenticationobject (username, password, authorities).UsernamePasswordAuthenticationToken— carries{username, password}from the login request; theAuthenticationManageruses it to authenticate the account.AuthenticationManager— uses aDaoAuthenticationProvider(with help fromUserDetailsServiceandPasswordEncoder) to validate theUsernamePasswordAuthenticationToken. On success, it returns a fully populatedAuthenticationobject, including granted authorities.OncePerRequestFilter— executes once per request to the API. ItsdoFilterInternal()method is where you parse and validate the JWT, load user details (viaUserDetailsService), and check authorization (viaUsernamePasswordAuthenticationToken).AuthenticationEntryPoint— catches authentication errors.- Repository —
UserRepositoryandRoleRepository, imported into the controller to work with the database. - Controller — receives and handles the request after it’s passed through
OncePerRequestFilter.