ToB企服应用市场:ToB评测及商务社交产业平台
标题:
三、Spring Boot集成Spring Security之securityFilterChain过滤器链详解
[打印本页]
作者:
我可以不吃啊
时间:
2024-10-12 10:33
标题:
三、Spring Boot集成Spring Security之securityFilterChain过滤器链详解
二、默认过滤器链
1、默认设置系统启动日志
2、默认设置的过滤器及顺序如下
org.springframework.security.web.session.DisableEncodeUrlFilter
org.springframework.security.web.context.request.async.WebAsyncManagerIntegrationFilter
org.springframework.security.web.context.SecurityContextPersistenceFilter
org.springframework.security.web.header.HeaderWriterFilter
org.springframework.security.web.csrf.CsrfFilter
org.springframework.security.web.authentication.logout.LogoutFilter
org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter
org.springframework.security.web.authentication.ui.DefaultLoginPageGeneratingFilter
org.springframework.security.web.authentication.ui.DefaultLogoutPageGeneratingFilter
org.springframework.security.web.authentication.www.BasicAuthenticationFilter
org.springframework.security.web.savedrequest.RequestCacheAwareFilter
org.springframework.security.web.servletapi.SecurityContextHolderAwareRequestFilter
org.springframework.security.web.authentication.AnonymousAuthenticationFilter
org.springframework.security.web.session.SessionManagementFilter
org.springframework.security.web.access.ExceptionTranslationFilter
org.springframework.security.web.access.intercept.FilterSecurityInterceptor
3、本文只介绍和登录相干的过滤器
SecurityContextPersistenceFilter
LogoutFilter
UsernamePasswordAuthenticationFilter
DefaultLoginPageGeneratingFilter
DefaultLogoutPageGeneratingFilter
AnonymousAuthenticationFilter
ExceptionTranslationFilter
FilterSecurityInterceptor
三、登录重要概念介绍
安全上下文堆栈(SecurityContextRepository):用于存储安全上下文,默认基于session实现(HttpSessionSecurityContextRepository)
安全上下文持有者(SecurityContextHolder):用于存储本次哀求的安全上下文,默认基于ThreadLocal实现
安全上下文(SecurityContext):用于存储认证信息
认证信息(Authentication):用于存储用户及认证结果信息,重要实现类有
用户名暗码认证Token:UsernamePasswordAuthenticationToken
匿名认证Token:AnonymousAuthenticationToken
登录页面哀求:跳转到登录页面的哀求
登录哀求:在登录页面输入用户名暗码后提交的哀求
登出页面哀求:跳转到登出页面的哀求
登出哀求:在登出页面确认登出提交的哀求
过滤器介绍
过滤器的入口为doFilter(ServletRequest request, ServletResponse response, FilterChain chain)方法
过滤器重要分为三部分:chain.doFilter之前代码,chain.doFilter,chain.doFilter之后的代码
chain.doFilter之前的代码按照过滤器链顺序实行
chain.doFilter之后的代码按照过滤器链倒序实行
调用chain.doFilter表示实行后续的过滤器;不调用chain.doFilter表示不实行后续的过滤器,会按过滤器链倒序实行已经调用的过滤器chain.doFilter之后的代码,这点很重要,下面在介绍每个过滤器时不再赘述。
四、SecurityContextPersistenceFilter
1、实现功能
登录成功之后的身份认证
2、处理哀求类型
所有哀求
3、是否会终止过滤器链
不会
4、实现步骤
从安全上下文堆栈中获取安全上下文,如果为空则创建没有认证信息的安全上下文
将安全上下文设置到安全上下文持有者中供后续业务使用
调用后续过滤器链
从安全上下文持有者中获取最新的认证信息
清除安全上下文持有者中的认证信息
将步骤4中的认证信息添加到安全上下文堆栈中
5、关键源码
五、LogoutFilter
1、实现功能
清除认证信息
重定向登录页面
2、处理哀求类型
登出哀求(默认:POST、/logout哀求)
3、是否会终止过滤器链
登出哀求时会终止
4、实现步骤
匹配哀求地址
清除认证信息(CompositeLogoutHandler中注册的LogoutHandler实现类)
调用登出成功处理器,默认SimpleUrlLogoutSuccessHandler实现重定向登录页面功能,推荐自定义设置,后续介绍
5、关键源码
六、UsernamePasswordAuthenticationFilter
1、实现功能
使用提交的用户名暗码生成用户名暗码认证Token
根据认证结果做不同处理
2、处理哀求类型
登录哀求(默认:POST、/login哀求)
3、是否会终止过滤器链
认证失败时会终止过滤器链,重定向默认登录地址
认证成功时会终止过滤器链,重定向到目标URL地址
4、实现步骤
匹配哀求地址
默认设置:提交的用户名暗码和内存中用户名暗码匹配,并校验用户和暗码的是否有效等信息
认证失败时重定向到登录页面
认证成功时将已认证的安全上下文设置到安全上下文持有者中
重定向到目标URL地址(未认证访问目标地址,会先重定向登录页面,登录成功后再重定向到目标URL地址)
5、关键源码
七、DefaultLoginPageGeneratingFilter
1、实现功能
生成默认登录页面
2、处理哀求类型
登录页面哀求(默认GET、/login哀求)
登录失败
登出成功
3、是否会终止过滤器链
登录页面哀求、登录失败、登出成功时会终止过滤器链
4、关键源码
八、DefaultLogoutPageGeneratingFilter
1、实现功能
生成默认登出页面
2、处理哀求类型
登出页面哀求(默认:GET、/logout哀求)
3、是否会终止过滤器链
登出页面哀求时会终止过滤器链
4、关键源码
九、AnonymousAuthenticationFilter
1、实现功能
当前认证信息为空时生成匿名认证信息
2、处理哀求类型
所有哀求
3、是否会终止过滤器链
不会
4、关键源码
十、ExceptionTranslationFilter
1、实现功能
处理FilterSecurityInterceptor抛出的非常,根据非常做相应处理
2、处理哀求类型
所有哀求
3、是否会终止过滤器链
认证失败时会重定向登录页面
授权失败时会返回错误信息
4、关键源码
十一、FilterSecurityInterceptor
1、实现功能
认证和授权
2、处理哀求类型
所有哀求
3、是否会终止过滤器链
认证或授权失败会抛出非常由ExceptionTranslationFilter处理该非常
4、关键源码
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。
欢迎光临 ToB企服应用市场:ToB评测及商务社交产业平台 (https://dis.qidao123.com/)
Powered by Discuz! X3.4