前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >SpringSecurity常用过滤器介绍

SpringSecurity常用过滤器介绍

作者头像
用户4919348
发布2019-12-11 16:04:28
1.5K0
发布2019-12-11 16:04:28
举报
文章被收录于专栏:波波烤鸭波波烤鸭

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

一、常用的过滤器

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

1.org.springframework.security.web.context.SecurityContextPersistenceFilter

  首当其冲的一个过滤器,非常重要 主要是使用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.CsrfFilter

  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.servletapi.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.ExceptionTranslationFilter

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

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

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

相关过滤器的路径

在这里插入图片描述
在这里插入图片描述

二、过滤器加载过程

1.DelegatingFilterProxy

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

在这里插入图片描述
在这里插入图片描述

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

代码语言:javascript
复制
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 IllegalStateException("No WebApplicationContext found: no ContextLoaderListener or DispatcherServlet registered?");
                }
                // 重要方法 进入
                delegateToUse = this.initDelegate(wac);
            }

            this.delegate = delegateToUse;
        }
    }

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

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

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

2.FilterChainProxy

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

在这里插入图片描述
在这里插入图片描述

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

在这里插入图片描述
在这里插入图片描述

进入doFilterInternal方法,然后debug查看

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

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

在这里插入图片描述
在这里插入图片描述

3.SecurityFilterChain

在这里插入图片描述
在这里插入图片描述

4.DefaultSecurityFilterChain

具体的实现类

在这里插入图片描述
在这里插入图片描述

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

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2019-12-05 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 一、常用的过滤器
    • 1.org.springframework.security.web.context.SecurityContextPersistenceFilter
      • 2.org.springframework.security.web.context.request.async.WebAsyncManagerIntegrationFilter
        • 3.org.springframework.security.web.header.HeaderWriterFilter
          • 4.org.springframework.security.web.csrf.CsrfFilter
            • 5.org.springframework.security.web.authentication.logout.LogoutFilter
              • 6.org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter
                • 7.org.springframework.security.web.authentication.ui.DefaultLoginPageGeneratingFilter
                  • 8.org.springframework.security.web.authentication.ui.DefaultLogoutPageGeneratingFilter
                    • 9.org.springframework.security.web.authentication.www.BasicAuthenticationFilter
                      • 10.org.springframework.security.web.savedrequest.RequestCacheAwareFilter
                        • 11.org.springframework.security.web.servletapi.SecurityContextHolderAwareRequestFilter
                          • 12.org.springframework.security.web.authentication.AnonymousAuthenticationFilter
                            • 13.org.springframework.security.web.session.SessionManagementFilter
                              • 14.org.springframework.security.web.access.ExceptionTranslationFilter
                                • 15.org.springframework.security.web.access.intercept.FilterSecurityInterceptor
                                • 二、过滤器加载过程
                                  • 1.DelegatingFilterProxy
                                    • 2.FilterChainProxy
                                      • 3.SecurityFilterChain
                                        • 4.DefaultSecurityFilterChain
                                        相关产品与服务
                                        容器服务
                                        腾讯云容器服务(Tencent Kubernetes Engine, TKE)基于原生 kubernetes 提供以容器为核心的、高度可扩展的高性能容器管理服务,覆盖 Serverless、边缘计算、分布式云等多种业务部署场景,业内首创单个集群兼容多种计算节点的容器资源管理模式。同时产品作为云原生 Finops 领先布道者,主导开源项目Crane,全面助力客户实现资源优化、成本控制。
                                        领券
                                        问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档