首当其冲的一个过滤器,非常重要

主要是使用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中存储的用户信息来决定其是否有权限。

相关过滤器的路径

springsecurity过滤所有的链接 springsecurity过滤器_后端

二、过滤器加载过程

========================================================================

1.DelegatingFilterProxy


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

springsecurity过滤所有的链接 springsecurity过滤器_面试_02

这个是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 IllegalStateException(“No WebApplicationContext found: no ContextLoaderListener or DispatcherServlet registered?”);
}
// 重要方法 进入
delegateToUse = this.initDelegate(wac);
}
this.delegate = delegateToUse;
}