三、Spring Boot集成Spring Security之securityFilterChain过滤器链详解

Spring Boot集成Spring Security之securityFilterChain过滤器链详解

一、默认过滤器

1、默认配置系统启动日志

2、默认配置的过滤器及顺序如下

  1. org.springframework.security.web.session.DisableEncodeUrlFilter
  2. org.springframework.security.web.context.request.async.WebAsyncManagerIntegrationFilter
  3. org.springframework.security.web.context.SecurityContextPersistenceFilter
  4. org.springframework.security.web.header.HeaderWriterFilter
  5. org.springframework.security.web.csrf.CsrfFilter
  6. org.springframework.security.web.authentication.logout.LogoutFilter
  7. org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter
  8. org.springframework.security.web.authentication.ui.DefaultLoginPageGeneratingFilter
  9. org.springframework.security.web.authentication.ui.DefaultLogoutPageGeneratingFilter
  10. org.springframework.security.web.authentication.www.BasicAuthenticationFilter
  11. org.springframework.security.web.savedrequest.RequestCacheAwareFilter
  12. org.springframework.security.web.servletapi.SecurityContextHolderAwareRequestFilter
  13. org.springframework.security.web.authentication.AnonymousAuthenticationFilter
  14. org.springframework.security.web.session.SessionManagementFilter
  15. org.springframework.security.web.access.ExceptionTranslationFilter
  16. org.springframework.security.web.access.intercept.FilterSecurityInterceptor

3、本文只介绍和登录相关的过滤器

  1. SecurityContextPersistenceFilter
  2. LogoutFilter
  3. UsernamePasswordAuthenticationFilter
  4. DefaultLoginPageGeneratingFilter
  5. DefaultLogoutPageGeneratingFilter
  6. AnonymousAuthenticationFilter
  7. ExceptionTranslationFilter
  8. FilterSecurityInterceptor

二、SecurityContextPersistenceFilter

1、实现功能

  1. 登陆成功之后的身份认证

2、处理请求类型

  • 所有请求

3、是否会终止过滤器链

  • 不会

4、实现步骤

  1. 从认证仓库中(SecurityContextRepository)获取认证信息(SecurityContext,基于session实现),如果为空则创建未认证的认证信息
  2. 将认证信息设置到认证上下文中(SecurityContextHolder,线程绑定的)中供后续业务使用
  3. 调用后续过滤器链
  4. 从认证上下文中获取最新的认证信息
  5. 清除认证上下文中的认证信息
  6. 将步骤4中的认证信息添加到认证仓库中

5、关键源码

三、LogoutFilter

1、实现功能

  1. 清除认证信息
  2. 重定向登录页面

2、处理请求类型

  • 登出提交请求(默认:POST、/logout请求)

3、是否会终止过滤器链

  • 登出请求时会终止

4、实现步骤

  1. 匹配请求地址
  2. 清除认证信息(CompositeLogoutHandler中注册的LogoutHandler实现类)
  3. 调用登出成功处理器,默认SimpleUrlLogoutSuccessHandler实现重定向登录页面功能,推荐自定义配置,后续介绍

5、关键源码

四、UsernamePasswordAuthenticationFilter

1、实现功能

  1. 使用提交的用户名密码生成认证信息
  2. 根据认证结果做不同处理

2、处理请求类型

  • 登录提交请求(默认:POST、/login请求)

3、是否会终止过滤器链

  • 认证失败时会终止过滤器链,重定向默认登录地址
  • 认证成功时会终止过滤器链,重定向到目标URL地址

4、实现步骤

  1. 匹配请求地址
  2. 默认配置:提交的用户名密码和内存中用户名密码匹配,并校验用户和密码的是否有效等信息
  3. 认证失败时重定向到登录页面
  4. 认证成功时securityContextRepository中保存SecurityContext
  5. 重定向到目标URL地址(未认证访问目标地址,会先重定向登录页面,登录成功后再重定向到目标URL地址)

5、关键源码

五、DefaultLoginPageGeneratingFilter

1、实现功能

  1. 生成默认登录页面

2、处理请求类型

  • 登录页面请求(默认GET、/login请求)
  • 登录失败
  • 登出成功

3、是否会终止过滤器链

  • 登录页面请求、登录失败、登出成功时会终止过滤器链

4、关键源码

六、DefaultLogoutPageGeneratingFilter

1、实现功能

  1. 生成默认登录页面

2、处理请求类型

  • 登出页面请求(默认:GET、/logout请求)

3、是否会终止过滤器链

  • 登出页面请求时会终止过滤器链

4、关键源码

七、AnonymousAuthenticationFilter

1、实现功能

  1. 当前认证信息为空时生成匿名认证信息

2、处理请求类型

  • 所有请求

3、是否会终止过滤器链

  • 不会

4、关键源码

八、ExceptionTranslationFilter

1、实现功能

  1. 处理FilterSecurityInterceptor抛出的异常,根据异常做相应处理

2、处理请求类型

  • 所有请求

3、是否会终止过滤器链

  • 认证失败时会重定向登录页面
  • 授权失败时会返回错误信息

4、关键源码

九、FilterSecurityInterceptor

1、实现功能

  1. 认证和授权

2、处理请求类型

  • 所有请求

3、是否会终止过滤器链

  • 认证或授权失败会抛出异常由ExceptionTranslationFilter处理该异常

4、关键源码

十、Spring Boot集成Spring Security专栏

一、Spring Boot集成Spring Security之自动装配
二、Spring Boot集成Spring Security之实现原理
三、Spring Boot集成Spring Security之securityFilterChain过滤器链详解
四、Spring Boot集成Spring Security之登录登出业务实现逻辑(未完成)
五、Spring Boot集成Spring Security之登录成功后自动认证业务实现逻辑(未完成)
六、Spring Boot集成Spring Security之自定义securityFilterChain过滤器链(未完成)
七、Spring Boot集成Spring Security之自定义基于JWT的用户名密码认证(未完成)
八、Spring Boot集成Spring Security之登录成功后JWT自动认证(未完成)
九、Spring Boot集成Spring Security之验证码认证(未完成)

相关推荐
Fuyo_111937 分钟前
C++ AVL树保姆级详解:从平衡因子到四大旋转机制
java·c++·算法
Sarvartha1 小时前
Date、LocalDate、LocalTime、LocalDateTime、DateTimeFormatter类
java
阿俊-全栈开发1 小时前
LikeShop 多商户 Java 版商城微服务模块拆分与部署架构演进实践
java·微服务·架构·likeshop开源商城·多商户源码
Wang's Blog2 小时前
Java 项目实战:交友项目 - 联系人列表
java·开发语言·交友
我命由我123452 小时前
访客系统形式(有用户概念、无用户概念)
java·开发语言·前端·javascript·python·产品运营·产品经理
香蕉大香蕉(06敬畏市场)2 小时前
MySQL 单点详解(01)
java·数据库·mysql·面试·博客·实习
LaughingZhu3 小时前
Product Hunt 热榜 2026-10-30:Empacer、Scape、Floot in ChatGPT
java·前端·数据库·人工智能·chatgpt·notion
niucloud-admin3 小时前
NIUCLOUD PHP SAAS上门家政使用文档:服务保障
java·开发语言·php
IT枫斗者枫哥3 小时前
CompletableFuture失败后返回空列表:异常没了,业务含义也变了
java·后端
知识搬运临时工3 小时前
一个模板把 AEM 拖进 Full GC 死亡螺旋:300% CPU、7 GB 堆、310 万行堆栈
java·aws