三、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之验证码认证(未完成)

相关推荐
南希夜酒4 分钟前
在Linux系统安装Nginx
java·linux·运维·服务器·centos·云计算
2401_857026235 分钟前
SpringBoot框架下的服装生产管理自动化
数据库·spring boot·自动化
LiQiang335 分钟前
Java 设计模式 构建者模式
java·开发语言·设计模式
2401_857297919 分钟前
招联金融2025秋招倒计时
java·前端·算法·金融·求职招聘
Stack Stone21 分钟前
基于 Spring Boot + Quartz 实现定时任务持久化配置
java·spring boot·后端
苓诣31 分钟前
Java 的数据结构整理(整合版)
java·开发语言·数据结构
weixin_4462608541 分钟前
C++游戏开发指南
java·c
战斗力为542 分钟前
浅谈长轮询及其封装实现
java
qq_335809521 小时前
easyexcel多sheet导出(唯一能用)
java·开发语言
码至终章1 小时前
算法日记-链表翻转
java·数据结构·算法·链表·leecode