您的位置 首页 java

SpringSecurity你不得不清楚的常用过滤器

  本文我们来介绍下 SpringSecurity 中常用的过滤器及其加载的过程。

一、常用的过滤器

  常用的过滤器有15个,分别如下:

1.org.springframework.security.web.context.SecurityContextPersistence filter

  首当其冲的一个过滤器,非常重要
主要是使用SecurityContextRepository在session中保存或更新一个SecurityContext,并将SecurityContext给以后的过滤器使用,来为后续filter建立所需的上下文,SecurityContext中存储了当前用户的认证和权限信息。

2.org.springframework.security.web.context. request .async.WebAsyncManagerIntegrationFilter

  此过滤器用于继承SecurityContext到Spring异步执行机制中的WebAsyncManager,和spring整合必须的。

3.org.springframework.security.web. Header .HeaderWriterFilter

  向请求的header中添加响应的信息,可以在http标签内部使用security:headers来控制

4.org.springframework.security.web.csrf. Csrf Filter

  Csrf又称跨域请求伪造,SpringSecurity会对所有post请求验证是否包含系统生成的csrf的token信息,如果不包含则报错,起到防止csrf攻击的效果

5.org.springframework.security.web.authentication.logout.LogoutFilter

  匹配URL为/logout的请求,实现用户退出,清楚认证信息

6.org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter

  认证操作全靠这个过滤器,默认匹配URL为/login且必须为POST请求

7.org.springframework.security.web.authentication.ui.DefaultLoginPageGeneratingFilter

  如果没有在配置文件中指定认证页面,则由该过滤器生成一个默认的认证界面

8.org.springframework.security.web.authentication.ui.DefaultLogoutPageGeneratingFilter

  由此过滤器生成一个默认的退出登录页面

9.org.springframework.security.web.authentication.www.BasicAuthenticationFilter

  此过滤器会自动解析HTTP请求中头部名字为Authentication,且以Basic开头的头部信息

10.org.springframework.security.web.savedrequest.RequestCacheAwareFilter

  通过HttpSessionRequestCache内部维护一个RequestCache,用于缓存HttpServletRequest

11.org.springframework.security.web. Servlet api.SecurityContextHolderAwareRequestFilter

  针对ServletRequest进行一次包装,使得request具有更加丰富的API

12.org.springframework.security.web.authentication.AnonymousAuthenticationFilter

  当SecurityContextHolder中认证信息为空,则会创建一个匿名用户存储到SecurityContextHolder中,SpringSecurity为了兼容未登录的访问,也走了一套认证流程,只不过是一个匿名的身份

13.org.springframework.security.web.session.SessionManagementFilter

  SecurityContextRepository限制同一个用户开启多个会话的数量

14.org.springframework.security.web.access. Exception TranslationFilter

  异常转换过滤器位于整个SpringSecurityFilterChain的后方,用来转换整个链路中出现的异常

15.org.springframework.security.web.access.intercept.FilterSecurityInterceptor

  获取所有配置资源的访问授权信息,根据SecurityContextHolder中存储的用户信息来决定其是否有权限。

相关过滤器的路径

在这里插入图片描述

二、过滤器加载过程

1.DelegatingFilterProxy

  我们在web.xml中配置了一个名称为SpringSecurityFilterChain的过滤器DelegatingFilterProxy,接下来可以看下该源码。

在这里插入图片描述

这个是Filter,所有进入后我们直接看doFilter方法

 public  void  doFilter(ServletRequest request, ServletResponse response, FilterChain filterChain) throws ServletException, IOException {
    Filter delegateToUse = this. Delegate ;
    if (delegateToUse == null) {
         synchronized (this.delegateMonitor) {
            delegateToUse = this.delegate;
            if (delegateToUse == null) {
                WebApplicationContext wac = this.findWebApplicationContext();
                if (wac == null) {
                    throw new Illegal State Exception("No WebApplicationContext found: no ContextLoaderListener or DispatcherServlet registered?");
                }
                // 重要方法 进入
                delegateToUse = this.initDelegate(wac);
            }

            this.delegate = delegateToUse;
        }
    }

    this.invokeDelegate(delegateToUse, request, response, filterChain);
}
  

我们通过 debug 启动查看该方法的运行

SpringSecurity你不得不清楚的常用过滤器

在这里插入图片描述

SpringSecurity你不得不清楚的常用过滤器

在这里插入图片描述

2.FilterChainProxy

  通过上面的源码分析我们发现其实创建的是 FilterChainProxy 这个过滤器,那我们来看下这个过滤器。

SpringSecurity你不得不清楚的常用过滤器

在这里插入图片描述

当然我们需要首先来看下doFilter方法

SpringSecurity你不得不清楚的常用过滤器

在这里插入图片描述

进入doFilterInternal方法,然后debug查看

SpringSecurity你不得不清楚的常用过滤器

在这里插入图片描述

SpringSecurity你不得不清楚的常用过滤器

在这里插入图片描述

我们看到这15个过滤器被保存到了List容器中了。
对应的 this.getFilters方法如下:

在这里插入图片描述

3.SecurityFilterChain

在这里插入图片描述

4.DefaultSecurityFilterChain

具体的实现类

在这里插入图片描述

总结 :通过上面的代码分析,SpringSecurity中要使用到的过滤器最终都保存在了 DefaultSecurityFilterChain 对象的List filter对象中。

文章来源:智云一二三科技

文章标题:SpringSecurity你不得不清楚的常用过滤器

文章地址:https://www.zhihuclub.com/177008.shtml

关于作者: 智云科技

热门文章

网站地图