Skip to the content.

Spring Security in detail

Initial step:

Steps to configure web MVC by adding dependencies explicitly without Spring Boot auto-configure -

  1. Add dependencies
  2. Extend AbstractAnnotationConfigDispatcherServletInitializer, then add implementation for all the methods -
    • Provide a configuration class for the getServletConfigClasses method with the custom config class.
    • Provide URL mapping for the getServletMapping method.
  3. In the custom config class should be marked with @Configuration, @EnableWebMvc and @ComponentScan annotations. Also, this class should create a bean for the view resolver if the view page exists in the project folder location. (create InternalResourceViewresolver bean).

Adding Spring Security -

Before the dispatcher dispatches the request Spring security filter comes in the action to filter the requests. It is just a simple servlet filter.

  1. Add dependencies
  2. Create a custom web security class for your application by extending WebSecurityConfigurerAdapter and mark this class with @EnableWebSecurity, then this custom web security should be attached with Spring security using custom security initializer.

  3. To create custom security initializer extend AbstractSecurityWebApplicationInitializer

  4. With respect to the custom web security class in the step 2 inherit method configure(HttpSecurity http) which contains default behaviour to authenticate form based and rest client. This can be overridden by the custom authentication mechanism. This method is used to configure all the endpoints and authorization.

  5. Override configure(AuthenticationManagerBuilder auth) to authenticate the user based upon requirement, here we can configure different types of authentication such as in-memory/jdbc/ldap etc.

Note -

Spring Security basic architecture

Spring Security Flow

Authentication Manager Flow

AttemptAuthentication implementation in the UsernamePasswordAuthenticationFilter class

Spring Security after successful authentication

Spring security basic flow chart

Spring security flow chart