SpringSecurity过滤器

本文详细解读Spring Security中的DelegatingFilterProxy加载过程,重点介绍默认的15个关键过滤器,包括SecurityContextPersistenceFilter、CsrfFilter等,帮助理解Spring Security的认证与权限管理机制。

一、过滤器加载过程

1.DelegatingFilterProxy

在web.xml中配置过滤器
在这里插入图片描述
进入doFilter方法
在这里插入图片描述
invokeDelegate方法执行FilterChainProxy的doFilter
在这里插入图片描述
最终采用遍历的方式执行十五个默认的过滤器
在这里插入图片描述

二、默认的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中存储的用户信息来决定其是否有权限。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值