Home> Java> javaTutorial> body text

What is the execution process of SpringBoot integrating Spring Security filter chain loading?

王林
Release: 2023-05-11 22:55:04
forward
992 people have browsed it

    1. Introduction

    In the Spring Boot project, we introduce the Spring Security dependency and do nothing. When the project Spring Security is started, it will take effect. The access request is intercepted.

    Spring Boot provides an automated configuration solution for Spring Security, which allows you to use Spring Security with less configuration.

    So how does this filter chain load and implement interception?

    2. Spring Security filter chain loading

    1.2. Register a filter named springSecurityFilterChain

    When the Spring Boot project is started, theSecurityFilterAutoConfigurationclass TheDelegatingFilterProxyRegistrationBeanregistration filter will be loaded, with the namespringSecurityFilterChain.

    What is the execution process of SpringBoot integrating Spring Security filter chain loading?

    Note: The name of springSecurityFilterChain is fixed.

    DelegatingFilterProxyRegistrationBeanAfter successful registration, the filter is loaded into the register. Then call the getFilter() method to generate theDelegatingFilterProxyproxy object and register it inIOC.

    What is the execution process of SpringBoot integrating Spring Security filter chain loading?

    3. View the DelegatingFilterProxy class

    When we access the project, we will enter thedoFiltermethod of theDelegatingFilterProxyclass .

    The DelegatingFilterProxy class is essentially a Filter, which indirectly implements the Filter interface, but in doFilter, it actually calls the implementation class of the proxy Filter obtained from the Spring container.

    What is the execution process of SpringBoot integrating Spring Security filter chain loading?

    The returnedFilterChainProxyobject.

    It can be seen that theDelegatingFilterProxyclass gets aFilterChainProxyfilter through the namespringSecurityFilterChain, and this filter is ultimately executed.doFiltermethod.

    Verify that the springSecurityFilterChain noun cannot be modified
    View the initDelegate method.

    What is the execution process of SpringBoot integrating Spring Security filter chain loading?

    4. View the FilterChainProxy class

    FilterChainProxyclass is essentially a Filter, so view thedoFiltermethod. Pay attention to the properties in this class.

    public class FilterChainProxy extends GenericFilterBean { private static final Log logger = LogFactory.getLog(FilterChainProxy.class); private static final String FILTER_APPLIED = FilterChainProxy.class.getName().concat(".APPLIED"); // 过滤器链 private List filterChains; private FilterChainProxy.FilterChainValidator filterChainValidator; private HttpFirewall firewall;
    Copy after login

    4.1 View doFilterInternal method

    Are you surprised? All 15 filters are here!

    What is the execution process of SpringBoot integrating Spring Security filter chain loading?

    4.2 View the getFilters method

    It turns out that these filters are encapsulated into SecurityFilterChain objects.

    What is the execution process of SpringBoot integrating Spring Security filter chain loading?

    ##5 View the SecurityFilterChain interface

    SecurityFilterChainclass is an interface, and there is only one implementation classDefaultSecurityFilterChainclass.
    DefaultSecurityFilterChainThe constructor method of the class initializes the List filters, which are put in by passing parameters.

    What is the execution process of SpringBoot integrating Spring Security filter chain loading?

    When are the filter chain parameters passed in?

    6. View the SpringBootWebSecurityConfiguration class

    Create

    Spring SecurityThe filter chain is handed over toSpring bootfor automatic configuration bySpringBootWebSecurityConfigurationClass creation injection.

    What is the execution process of SpringBoot integrating Spring Security filter chain loading?

    View the

    WebSecurityConfigurerAdapterclass.

    What is the execution process of SpringBoot integrating Spring Security filter chain loading?

    Then the HttpSecurity object will be injected. HttpSecurity can be understood as the http core configuration of Spring Security, which stores the filter chain, request matching path and other related authentication and authorization important information in Spring Security. method.

    Then we started to create the Spring Security filter chain, which was automatically configured by Spring Boot. There are 15 filters in total.

    Use OrderedFilter for proxy and set the order attribute.
    After the addition is completed, encapsulate these filters into DefaultSecurityFilterChain objects.

    Finally load springSecurityFilterChain through WebSecurityConfiguration configuration. The securityFilterChains attribute is maintained in WebSecurityConfiguration and will store all filters in the filter chain.

    The above is the detailed content of What is the execution process of SpringBoot integrating Spring Security filter chain loading?. For more information, please follow other related articles on the PHP Chinese website!

    Related labels:
    source:yisu.com
    Statement of this Website
    The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
    Latest Downloads
    More>
    Web Effects
    Website Source Code
    Website Materials
    Front End Template
    About us Disclaimer Sitemap
    php.cn:Public welfare online PHP training,Help PHP learners grow quickly!