Authentication in Spring Security

In the tutorial Overview about request processing in Spring Security, I gave you a brief introduction to the main components in a flow for the authentication part of the Spring Security framework. In this tutorial, we will learn in more detail how Spring Security handles this authentication part!

First, I will create a new Spring Boot project with Spring Security Starter and Spring Web Starter as an example.

The following results:

As I said in the tutorial Overview about request processing in Spring Security, the AuthenticationManager class will be the class that takes care of authentication in Spring Security:

The request after going through the AuthenticationFilter, the user’s login information will be converted to the Authentication object with the implementation of the UsernamePasswordAuthenticationToken class. The AuthenticationManager will use the information in the UsernamePasswordAuthenticationToken class for authentication.

By default, Spring supports us to authenticate username and password using the DaoAuthenticationProvider class.

When authenticate, with authentication information in the UsernamePasswordAuthenticationToken class, Spring Security will get username information in UsernamePasswordAuthenticationToken to check if UserCache has information for this username? I don’t know why the current code used for UserCache is:

NullUserCache only returns null for UserDetails information. Strange thing!

Since there is no information in the cache, Spring Security will use the object of the UserDetailsService interface to get the UserDetails information using the username.

The UserDetailsService interface has only one abstract method, loadUserByUsername():

The implementation of the UserDetailsService interface used in this case is InMemoryUserDetailsManager because I am not using the database for the example in this tutorial.

If there is user information with the username that we are sending when we click the Login button, the UserDetails object containing that user’s information will be returned.

Here, Spring Security also does a few more checks of the user before checking the user’s password information: including checking if the user is locked, is the user enabled and is the user expired?

The DefaultPreAuthenticationChecks class that implements the UserDetailsChecker interface is used to do this with the check() method.

Spring Security will check the password information passed by the user, is the same as the information in the system? Using the additionalAuthenticationChecks() method.

Here, the password information will depend on the encoder that we are configuring with Spring Security, the corresponding Encoder class will be used during password checking.

After checking the password information, Spring Security will have a step to check if the password is expired using the DefaultPostAuthenticationChecks class with the check() method.

During the process of handling authentication flow, if any exception occurs, the AuthenticationFailureHandler interface with SimpleUrlAuthenticationFailureHandler implementation will handle this exception.

And if everything goes smoothly, without any errors, the AuthenticationSuccessHandler interface with the implementation of the SavedRequestAwareAuthenticationSuccessHandler class will handle..

Details for what happened above, you can read more code of class AbstractAuthenticationProcessingFilter private doFilter() method:

class UsernamePasswordAuthenticationFilter with attemptAuthentication() method:

and authenticate() method in AbstractUserDetailsAuthenticationProvider class:

You can run debug, add breakpoints to these methods, to understand more about how Spring Security is handling the authentication part.

After understanding the details, you can add a custom authentication filter like I did in the tutorial Custom authentication filter login without password in Spring Security easily, depending on your purposes!

Add Comment