Spring Security 实战干货:OAuth2登录获取Token的核心逻辑
作者 | 码农小胖哥
来源 | https://mp.weixin.qq.com/s/zdTBdSVunqwVGx-spHjLjw
1. 前言
在上一篇Spring Security 实战干货:OAuth2 授权回调的核心认证流程中,我们讲了当第三方同意授权后会调用redirectUri
发送回执给我们的服务器。我们的服务器拿到一个中间授信凭据会再次进行认证,目的是为了获取Token。而这个逻辑由OAuth2LoginAuthenticationProvider
负责,经过上一文的分析后我们发现获取Token的具体逻辑由OAuth2AuthorizationCodeAuthenticationProvider
来完成,今天就把它的流程搞清楚,来看看Spring Security OAuth2 认证授权获取Token的具体步骤。
❝
注意:本Spring Security干货系列教程的 OAuth2 相关部分是在Spring Security 5.x版本的。
2. OAuth2AuthorizationCodeAuthenticationProvider
该类是AuthenticationProvider
针对OAuth 2.0中Authorization Code Grant模式的实现。关于AuthenticationProvider
有必要简单强调一下,它已经多次在Spring Security 干货系列中出现,十分重要!一定要去看看相关的分析和使用,它是你根据业务扩展认证方式渠道的重要入口。
2.1 OAuth2AccessTokenResponseClient
在该实现中包含了一个OAuth2AccessTokenResponseClient
成员变量,它抽象了通过tokenUri
端点从认证服务器获取Token的细节。你可以根据OAuth 2.0常用的四种模式来进行实现它, 以达到根据不同的策略来获取Token的能力。
OAuth 2.0 四种模式的对应实现
在Spring Security 5中OAuth 2.0登录的配置中默认使用DefaultAuthorizationCodeTokenResponseClient
。如果你想使用自定义实现的话可以通过HttpSecurity
来配置:
@Overrideprotected void configure(HttpSecurity http) throws Exception {http.oauth2Login().tokenEndpoint()// 注入自定义的 OAuth2AccessTokenResponseClient.accessTokenResponseClient(authorizationCodeTokenResponseClient);// 其它省略}
接下来我们看看DefaultAuthorizationCodeTokenResponseClient
实现的获取Token的逻辑:
@Override
public OAuth2AccessTokenResponse getTokenResponse(OAuth2AuthorizationCodeGrantRequest authorizationCodeGrantRequest) {Assert.notNull(authorizationCodeGrantRequest, "authorizationCodeGrantRequest cannot be null");
// 1. 封装调用tokenUri所需要的请求参数RequestEntityRequestEntity<?> request = this.requestEntityConverter.convert(authorizationCodeGrantRequest);ResponseEntity<OAuth2AccessTokenResponse> response;try {// 2. 通过RestTemplate 发起请求获取 OAuth2AccessTokenResponseresponse = this.restOperations.exchange(request, OAuth2AccessTokenResponse.class);} catch (RestClientException ex) {OAuth2Error oauth2Error = new OAuth2Error(INVALID_TOKEN_RESPONSE_ERROR_CODE,"An error occurred while attempting to retrieve the OAuth 2.0 Access Token Response: " + ex.getMessage(), null);throw new OAuth2AuthorizationException(oauth2Error, ex);}// 3. 解析 ResponseEntity 组织返回值 OAuth2AccessTokenResponseOAuth2AccessTokenResponse tokenResponse = response.getBody();if (CollectionUtils.isEmpty(tokenResponse.getAccessToken().getScopes())) {// originally requested by the client in the Token RequesttokenResponse = OAuth2AccessTokenResponse.withResponse(tokenResponse).scopes(authorizationCodeGrantRequest.getClientRegistration().getScopes()).build();}return tokenResponse;
}
这里的方式跟我另一个开源项目Payment Spring Boot的请求方式异曲同工,都是三个步骤:
组织参数
RequestEntity
。RestOperations
发起请求。解析
ResponseEntity
组织返回值。
如果有些的OAuth 2.0认证服务器获取Token的方式比较特殊你可以自行实现OAuth2AccessTokenResponseClient
。
3. 总结
OAuth2AccessTokenResponseClient
是OAuth2AuthorizationCodeAuthenticationProvider
的核心要点。搞清楚它的作用和机制就可以了。这里我们总结一下OAuth2AuthorizationCodeAuthenticationProvider
的认证过程:
检测未授信
OAuth2AuthorizationCodeAuthenticationToken
的状态是否合法。通过
OAuth2AccessTokenResponseClient
请求OAuth 2.0认证服务器获取Token等信息。组装认证过的授信
OAuth2AuthorizationCodeAuthenticationToken
返回。
到此OAuth 2.0的登录流程就搞清楚了,读者可通过系列文章进行学习批判。我是:码农小胖哥,多多关注,获取实用的编程干货。
推荐关注本文作者:码农小胖哥
分享高质量编程知识,探讨IT人生
技术干货,实战技巧,面试技巧,前沿资讯一个都不能少
往期推荐
上云上的差点破产是什么体验?
本科毕业出国率下降,考研or保研?条条大路通「内卷」
一个员工的离职,背后都意味着什么?
彻底解决 gcr、quay、DockerHub 镜像下载难题!
合格的后端Coder都应该写好UT和Mock测试
Spring Boot 2.4.3、2.3.9 版本发布,你准备好了吗?
Spring Security 实战干货:OAuth2登录获取Token的核心逻辑相关推荐
- Spring Security 实战干货:OAuth2授权回调的核心认证流程
1. 前言 我们在上一篇 Spring Security 实战干货:OAuth2 授权回调的处理机制 对 OAuth2 服务端调用客户端回调的流程进行了图解, 今天我们来深入了解 OAuth2 在回调 ...
- Spring Security 实战干货:玩转自定义登录
点击上方蓝色"程序猿DD",选择"设为星标" 回复"资源"获取独家整理的学习资料! 1. 前言 前面的关于 Spring Security ...
- Spring Security 实战干货:实现自定义退出登录
点击上方蓝色"程序猿DD",选择"设为星标" 回复"资源"获取独家整理的学习资料! 1. 前言 上一篇对 Spring Security 所 ...
- Spring Security 实战干货:客户端OAuth2授权请求的入口在哪里
1. 前言 在Spring Security 实战干货:OAuth2 第三方授权初体验一文中我先对 OAuth2.0 涉及的一些常用概念进行介绍,然后直接通过一个 DEMO 来让大家切身感受了 OAu ...
- Spring Security 实战干货: RBAC权限控制概念的理解
点击上方蓝色"程序猿DD",选择"设为星标" 回复"资源"获取独家整理的学习资料! 作者 | 码农小胖哥 来源 | 公众号「码农小胖哥」 1 ...
- Spring Security 实战干货:自定义异常处理
Spring Security 实战干货:自定义异常处理 转自:https://www.cnblogs.com/felordcn/p/12142514.html 文章目录 1. 前言 2. Sprin ...
- Spring Security 实战干货:自定义配置类入口 WebSecurityConfigurerAdapter
点击上方蓝色"程序猿DD",选择"设为星标" 回复"资源"获取独家整理的学习资料! 1. 前言 今天我们要进一步的的学习如何自定义配置 Sp ...
- Spring Security 实战干货:从零手写一个验证码登录
1. 前言 前面关于Spring Security胖哥又写了两篇文章,分别图文并茂地介绍了UsernamePasswordAuthenticationFilter和 AuthenticationMan ...
- Spring Security 实战干货:OAuth2第三方授权初体验
1. 前言 现在很多项目都有第三方登录或者第三方授权的需求,而最成熟的方案就是OAuth2.0授权协议.Spring Security也整合了OAuth2.0,在目前最新的 Spring Securi ...
最新文章
- 平板电脑怎么投屏到电视上_学而思网课怎么投屏到电视上
- 如何优雅的使用fbx sdk
- 大疆等无人机在倾斜摄影作业时 如何进行仿地飞行
- Markdown制作表格
- 075 json和pickle模块
- 华为荣耀立方中播放群晖nas中保存的视频
- js 通过图片URL地址将图片转为可操作的File文件对象
- 机器学习正则化线性回归(调参技巧(matlab))
- 高速公路匝道口事故何时了?
- 网上体育商城的设计与实现
- 【Python】可视化台风路径轨迹图
- html5 gif 只播放一次,使用JS和canvas实现gif动图的停止和播放代码
- 公众号配图在哪里找?快来看看这里
- 兔子数列 - C语言
- mc服务器称号显示插件,[聊天]UDtitle ——称号管理(仓库) 1.1.6 [全版本]
- 为什么使用计算机网络连接,为什么无线网络连接上却不能上网,教您电脑连上无线网却不能上网怎么办...
- 第一条台电子计算机,第一台计算机
- [转]微信终端跨平台组件 mars 系列(二) - 信令传输超时设计
- c语言指针near,C语言中 near指针和far指针
- 【计算】空气质量综合指数+空气质量指数
热门文章
- VC++学习(17):进程间通信
- ubuntu 一个好系统
- Ubuntu下gedit的相关问题
- 签名工具 signtool.exe 参数简介
- golang 数据库 连接与连接池
- linux c 字符串函数 replace indexOf substring 实现
- linux下vim命令详解
- java模拟银行存取_JAVA基础案例 模拟银行存取款业务
- asp服务器推送消息,asp.net实时向客户端推送消息(SignalRWeb)
- mysql bin.000013_mysql运维-二进制日志BINARY LOG清理_ mysql-bin磁盘占用高处理办法