Spring Security - Understanding Spring Security Configurations and components

[Updated: Jul 16, 2017, Created: Jul 16, 2017]

In this tutorial will walk-through the Spring Security configurations and at the same time we will get familiar with different high level security components.


This class implements Spring's WebApplicationInitializer. That means the concrete implementations of this abstract class will be initialized by SpringServletContainerInitializer given that the spring-web module is on classpath.

The onStartup() method of this class, creates an instance of AnnotationConfigWebApplicationContext which registers client side @Configuration classes with it and bootstraps Spring container.

Following implementation of this class (in its basic form) should be included in the Spring application:

public class AppSecurityInitializer extends AbstractSecurityWebApplicationInitializer {
  public AppSecurityInitializer() {

Note that, it's not necessary to call super(MyAppConfig.class) from the constructor, if our configuration class is already registered via DispatcherServlet (if it's Spring MVC application)

AbstractSecurityWebApplicationInitializer also registers an instance of DelegatingFilterProxy. The DelegatingFilterProxy#targetBeanName property is set with bean name "springSecurityFilterChain".

The bean named "springSecurityFilterChain" is registered in the configuration imported by @EnableWebSecurity (next section).


This annotation is used on our configuration class to important necessary Spring security configuration. Following is the typical usage of the annotation:

public class MyAppConfig{

Let's see how the annotation @EnableWebSecurity is defined:

@Import({ WebSecurityConfiguration.class, SpringWebMvcImportSelector.class })
public @interface EnableWebSecurity {

Let's understand what WebSecurityConfiguration and SpringWebMvcImportSelector configuration classes do.

Configurations imported by WebSecurityConfiguration

WebSecurityConfiguration creates a Filter and registers it as a bean by name "springSecurityFilterChain" (same bean name we saw above in section 1). Following is the bean registration snippet.

public class WebSecurityConfiguration implements .... {
    @Bean(name = AbstractSecurityWebApplicationInitializer.DEFAULT_FILTER_NAME)
    public Filter springSecurityFilterChain() throws Exception {
        boolean hasConfigurers = webSecurityConfigurers != null
                && !webSecurityConfigurers.isEmpty();
        if (!hasConfigurers) {
            WebSecurityConfigurerAdapter adapter = objectObjectPostProcessor
                    .postProcess(new WebSecurityConfigurerAdapter() {
        return webSecurity.build();

In above snippet, following things are important to know:

  • The returned Filter is an instance of FilterChainProxy.
  • WebSecurityConfigurerAdapter(s) (injected as 'webSecurityConfigurers' collection in the above class) is extended by our @Configuration class for customization. But if we don't extend it then a default one is used with no methods implemented.
  • webSecurity.build() builds an instance of FilterChainProxy and during building process it eventually calls our WebSecurityConfigurerAdapter implemented methods (in our @Configuration class) allowing for customization. WebSecurity instance (webSecurity), is basically a builder for FilterChainProxy.
  • The returned Filter is also registered as bean by the name: AbstractSecurityWebApplicationInitializer.DEFAULT_FILTER_NAME = "springSecurityFilterChain". As seen in section 1 above, DelegatingFilterProxy#targetBeanName is set with the same name.

Now we will understand above components quickly.


As the name suggests this Filter bean is another proxy (within DelegatingFilterProxy) which delegates to a list of Spring-managed filter beans (they also implement Filter interface). Here's a list of those filters in a FilterChainProxy instance (spring security version 4.2.3.RELEASE):

Above screenshot is taken by setting a debug break point in doFilter() method of FilterChainProxy source code. We don't have to understand each filters in above list right now. We will need to understand one of them if we will do some advance configurations or customizations, which we will be exploring in future spring security tutorials.

WebSecurity and HttpSecurity

As we saw in section 3, WebSecurity is nothing but builder to create an instance of FilterChainProxy. Its another sibling-class HttpSecurity allows configuring web based security for specific http requests. We will see in the next section how it is used.


As we discussed a little about this class in section 3, it is implemented by the client application (usually by @Configuration class) to customize WebSecurity and HttpSecurity. It is an adapter implementation of WebSecurityConfigurer. Its init() method creates an instance of HttpSecurity which is responsible to add all the Filters we saw in the screenshot in section 4. We will be gradually learning what method can be overridden for customizations in these series of tutorials.


EnableWebSecurity also imports SpringWebMvcImportSelector (as seen in section 2), which conditionally imports WebMvcSecurityConfiguration when the DispatcherServlet is present on the classpath.

This configuration class registers RequestDataValueProcessor bean which is used to provide data integrity, e.g. by protected against CSRF attacks.


The annotation @EnableWebSecurity (section 2) is also meta-annotated with @EnableGlobalAuthentication which imports AuthenticationConfiguration. This configuration register beans for authentication process.

With this import we can configure a global instance of AuthenticationManagerBuilder. For example:

public class MyAppConfig extends WebSecurityConfigurerAdapter {

  public void configure(AuthenticationManagerBuilder builder)
          throws Exception {

See Also