3

Spring Security过滤器链体系

 2 years ago
source link: https://segmentfault.com/a/1190000041384901
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

以下摘自胖哥分享的 2022开工福利教程。

在学习Spring Security的时候你有没有下面这两个疑问:

  • Spring Security的登录是怎么配置的?
  • Spring Security的访问控制是什么机制?

SpringBootWebSecurityConfiguration

上面两个疑问的答案就在配置类SpringBootWebSecurityConfiguration中。你可以按照下面这个思维导图去理解这个自动配置:
SpringBootWebSecurityConfiguration

SpringBootWebSecurityConfigurationSpring Boot应用提供了一套默认的Spring Security配置。

    @Bean
    @Order(SecurityProperties.BASIC_AUTH_ORDER)
    SecurityFilterChain defaultSecurityFilterChain(HttpSecurity http) throws Exception {
        http.authorizeRequests().anyRequest().authenticated().and().formLogin().and().httpBasic();
        return http.build();
    }

这里的配置为:所有的请求都必须是认证用户发起的,同时开启表单登录功能以及Http Basic Authentication认证功能。 我们访问/foo/bar时需要登录认证并且能够进行表单登录就是这个配置起作用了。这个是我们日常开发需要自定义的,在HttpSecurity相关的文章中胖哥也进行了讲解。这个SecurityFilterChain到底是什么呢?

SecurityFilterChain

从上面看得出HttpSecurity就是一个构建类,它的使命就是构建出一个SecurityFilterChain

public interface SecurityFilterChain {
   //  当前请求是否匹配
    boolean matches(HttpServletRequest request);
    // 一揽子过滤器组成的有序过滤器链
    List<Filter> getFilters();
}

当一个请求HttpServletRequest进入SecurityFilterChain时,会通过matches方法来确定是否满足条件进入过滤器链。就好比你是VIP走的是VIP通道,享受的是VIP的一系列待遇;你是普通用户,就走普通用户的通道并享受普通用户的待遇。

不管用户是哪种角色,都走的是一个过滤器链,一个应用中存在1-nSecurityFilterChain。那谁来管理多个SecurityFilterChain呢?

记住这个公式HttpSecurity ->SecurityFilterChain

FilterChainProxy

FilterChainProxy是一个GenericFilterBean(即使Servlet Filter又是Spring Bean),它管理了所有注入Spring IoC容器的SecurityFilterChain。在我刚接触Spring Security的时候是这样配置FilterChainProxy的:

    <bean id="myfilterChainProxy" class="org.springframework.security.web.FilterChainProxy">
        <constructor-arg>
            <util:list>
                <security:filter-chain pattern="/do/not/filter*" filters="none"/>
                <security:filter-chain pattern="/**" filters="filter1,filter2,filter3"/>
            </util:list>
        </constructor-arg>
    </bean>

根据不同的请求路径匹配走不同的SecurityFilterChain。下面是示意图:

后面还会对接触这个类,现在你只需要明白上面这个图就行了。

请注意:在同一过滤器链中不建议有多个FilterChainProxy实例,而且不应将其作为单纯的过滤器使用,它只应该承担管理SecurityFilterChain的功能。

DelegatingFilterProxy

Servlet 容器和Spring IoC容器之间的Filter生命周期并不匹配。为了让Spring IoC容器管理Filter的生命周期,FilterChainProxy便交由Spring Web下的DelegatingFilterProxy来代理。而且FilterChainProxy不会在添加到应用程序上下文的任何过滤器Bean上调用标准Servlet过滤器生命周期方法,FilterChainProxy的生命周期方法会委托给DelegatingFilterProxy来执行。而DelegatingFilterProxy作为Spring IoCServlet的连接器存在。

上面的三个概念非常重要,涉及到Spring Security的整个过滤器链体系。但是作为初学者来说,能看懂多少就看懂多少,不要纠结哪些没有理解,因为目前学习阶段的层次达不到是非常正常的。但是等你学完了Spring Security之后,这几个概念一定要搞明白。

关注公众号:Felordcn 获取更多资讯

个人博客:https://felord.cn


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK