当前位置:网站首页>【OAuth2】十、BearerTokenAuthenticationFilter过滤器介绍
【OAuth2】十、BearerTokenAuthenticationFilter过滤器介绍
2022-08-05 17:17:00 【北城小林】
BearerTokenAuthenticationFilter
一、BearerTokenAuthenticationFilter
1、基本作用
BearerTokenAuthenticationFilter是Spring Security资源服务器的核心过滤器。负责对请求中携带的Token进行校验,提取认证信息,以确定当前请求是否有权限访问对应的资源。
以下是BearerTokenAuthenticationFilter的大致结构:
成员属性都在上一文中进行了说明,这里就不再赘述了。
2、核心逻辑
作为一个OncePerRequestFilter(一次请求的过滤器)核心的逻辑还在doFilterInternal方法中:
具体的逻辑为下:
- 利用BearerTokenResolver从当前请求中提取token字符串,如果提取过程中发生异常就直接交给认证端点接口AuthenticationEntryPoint(默认是BearerTokenAuthenticationEntryPoint)处理。
- 如果该请求解析不出有效的token,就进入过滤器链中的其它过滤器。
- 如果该请求解析出了有效的token,就封装成未授信的BearerTokenAuthenticationToken。
- 利用AuthenticationManagerResolver从当前请求中解析出对应的AuthenticationManager对步骤③中的BearerTokenAuthenticationToken进行认证。
如果使用了JWT,认证则由JwtAuthenticationProvider处理,认证成功后返回JwtAuthenticationToken,并存放到安全上下文SecurityContext中。 - 不管是什么Token风格,认证失败后都会交给AuthenticationFailureHandler处理。
3、JwtAuthenticationProvider
JwtAuthenticationProvider是资源服务器处理JWT风格的AuthenticationProvider。它利用JwtDecoder将token字符串解码为Jwt对象。
然后用默认提供的JwtAuthenticationConverter(可自定义)将Jwt转换为JwtAuthenticationToken,代码逻辑为:
@Override
public final AbstractAuthenticationToken convert(Jwt jwt) {
// 1、默认从Jwt的 scope 或者 scp中提取权限并加以SCOPE_前缀,你可以通过自定义JwtAuthenticationConverter中的jwtGrantedAuthoritiesConverter属性来改变该逻辑。
Collection<GrantedAuthority> authorities = extractAuthorities(jwt);
// 2、 默认从Jwt的sub中提取用户标识作为Token的名称,你可以通过自定义JwtAuthenticationConverter中的principalClaimName属性来改变这一设定。
String principalClaimValue = jwt.getClaimAsString(this.principalClaimName);
// 3、将前两步骤中提取的属性连同Jwt对象一起封装为JwtAuthenticationToken返回,该JwtAuthenticationToken就是保存在安全上下文SecurityContext中的对象。
return new JwtAuthenticationToken(jwt, authorities, principalClaimValue);
}
4、小结
BearerTokenAuthenticationFilter的逻辑非常简单,大多数情况下过滤器都有拦截条件,即有对应的RequestMatcher,而这个过滤器没有,它会拦截处理所有携带token的请求。结合上一篇,可以对OAuth2ResourceServerConfigurer的所有的配置项进行自定义来加深理解,到此整个资源服务器的讲解就结束了,接下来我们该进入授权服务器的相关知识了。
边栏推荐
猜你喜欢

科研实习 | 北京大学万小军老师课题组招收NLP方向实习生和访问学生

详解 SSL(一):网址栏的小绿锁有什么意义?

The Chinese Valentine's Day is coming, it's time to show the romance exclusive to programmers

What is Common Defect Enumeration (CWE)
![[Case] The use of steps in animation animation curve](/img/b0/2831f9318ce28d8732fb5ba87431bd.png)
[Case] The use of steps in animation animation curve

【历史上的今天】8 月 5 日:微信 5.0 发布;百度上市;LinkedIn 创始人诞生

4-Arm PEG-DSPE,MW:2000,四臂-聚乙二醇-磷脂仅供科研实验使用

Good code in the eyes of a compiler engineer: Loop Interchange

Ventoy系统启动盘制作

AIRIOT答疑第7期|如何快速提升物联网项目交付速度?
随机推荐
你也搞Web3 先回答以下13问再找我谈Web3
The Chinese Valentine's Day is coming, it's time to show the romance exclusive to programmers
Install a specific specified version of the r package with a lower version r package downgrade
从零开始Blazor Server(8)--增加菜单以及调整位置
Cholesterol-PEG-Acid,Cholesterol-PEG-COOH,疏水性分子胆固醇-聚乙二醇-羧基
力扣623.在二叉树中增加一行 dfs
程序员表白代码来了!太秀了。。
阿里百万级Redis小册震撼开源,打开源码新世界
8.lnmp architecture
电脑系统安装后桌面图标隔开很宽怎么调?
企业如何用好云原生2.0,释放数字生产力?
2022 第四周全球 AI 模型周报
Why do so many people discourage software testing?Don't click here if the monthly salary is more than 10,000.
App与Hap、Entry与feature,bundleName与packge,务必弄明白
【边缘计算概念】MEC(Mobile Edge Computing边缘计算技术)
左益豪:用代码创造一个新世界|OneFlow U
cpalgotithm安装大坑
AVH 动手实践 (二) | 在 Arm 虚拟硬件上部署 PP-OCR 模型
AI target segmentation capability for fast video cutout without green screen
2022 Week 4 Global AI Model Weekly Report