Spring Security Overview

Spring Security is a certification authorization framework that supports authentication modes such as HTTP BASIC authentication (based on IETF rfc-based standard),HTTP Digest authentication (based on IETF rfc-based standard), form-based authentication (for simple user interface),OpenID authentication, and so on. Spring Security enables the current system to quickly integrate these authentication mechanisms or implement its own set of authentication mechanisms.

1. Spring Security Usage.

Spring Security3 and later provide Java Config configuration, but I found the XML configuration is more easily to understand, so this article is based on XML configuration.

1.1 Pom Dependencies.



Since Spring Boot is used, you need to enable Spring Security with the @EnableWebSecurity annotation and specify that its configuration file is spring-security.xml in the classpath directory.

@ImportResource(locations = "classpath:spring-security.xml")
public class SecurityConfig {

1.2 XML Configuration.

The officially provided namespace security is introduced in spring-security.xml and then simply configured to intercept all requested urls, which must be accessed by users with User privileges.

<beans xmlns="http://www.springframework.org/schema/beans"

    <security:http >
        <security:intercept-url pattern="/**" access="hasRole('ROLE_USER')"/>

                <security:user name="user" password="123456" authorities="ROLE_USER"/>
                <security:user name="admin" password="123456" authorities="ROLE_USER, ROLE_ADMIN"/>


1.3 Access Test.

When you access any url in this web application, spring security will generate a login page and redirect your access to this login page. Only after you enter user:123456 in the login form then you can access it.

2. How Does Spring Security Intercept Requests.

2.1 Traditional Filter.

The traditional filter configuration in web.xml is as following.


From above configuration we can see that every request ( /* ) will be filtered by the org.springframework.web.filter.DelegatingFilterProxy class.

2.2 Spring Security Filter.

The breaking point shows that the DelegatingFilterProxy actually represents the FilterChainProxy class, which has private List<SecurityFilterChain> filterChains Global variable, so what’s the SecurityFilterChain?

public interface SecurityFilterChain {
    boolean matches(HttpServletRequest request);
    List<Filter> getFilters();

From the source code, it can be judged that the SecurityFilterChain is a set of Filter chain corresponding to a set of rules. Then, look at the getFilters source code, it will obtain a Filter chain corresponding to a set of rules configured by security:http xml tag in the configuration file.

private List<Filter> getFilters(HttpServletRequest request) {
    for (SecurityFilterChain chain : filterChains) {
        if (chain.matches(request)) {
           return chain.getFilters();
    return null;    

Leave a Comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.