Which authentication method should you use?
We have covered the three main methods of authenticating, so, which one is the best? Like all solutions, each comes with its pros and cons. You can find a summary of when to use a specific type of authentication by referring to the following list:
SecurityContextHolder
: Interacting directly withSecurityContextHolder
is certainly the easiest way of authenticating a user. It works well when you are authenticating a newly created user or authenticating unconventionally. By usingSecurityContextHolder
directly, we do not have to interact with so many Spring Security layers. The downside is that we do not get some of the more advanced features that Spring Security provides automatically. For example, if we want to send the user to the previously requested page after logging in, we will have to manually integrate that into our controller.UserDetailsService
: Creating a customUserDetailsService
object is an easy mechanism that allows...