当前位置:网站首页>Oauth2协议中如何对accessToken进行校验
Oauth2协议中如何对accessToken进行校验
2022-07-06 19:26:00 【飘渺Jam】
大家好,我是飘渺。今天我们来聊聊oauth2.0的accesstoken校验逻辑。
概述
本文来自球友Never Sett*
的提问

看完这个问题,我感觉读者对于accesstoken的校验逻辑不太清楚,所以特意写了这篇文章解释一下。
首先我们要知道Oauth2是一个授权协议,客户端访问某个被保护的资源之前,需要先通过认证服务器获取accesstoken,而后通过在请求头上带上accesstoken访问资源服务器。
其次,Oauth2认证服务器颁发的令牌有两种:不透明令牌(opaque tokens) 和 透明令牌(not opaque tokens) 说白了其实就是 uuid 和 jwt 的区别。
好了,现在问题来了,客户端带上 accesstoken 访问资源服务器,那资源服务器如何知道你这accesstoken是合法的呢?
当你是获取到的令牌是 uuid时,资源服务器自己肯定无法判断你令牌的有效性。
此时常见的一般有两种校验逻辑:
远程校验
授权服务器暴露一个端点,对于有效令牌,它会返回先前向其颁发该令牌的用户所授予的权限,这个端口称为 check_token端点(在很多地方也叫令牌自省端点)。我们可以直接使用认证服务器提供的默认接口/oauth/check_toen 也可以自定义一个接口。
资源服务器为每个请求调用check_token端点,这样,它就会验证从客户端接受的令牌,并获得授予客户端的权限。资源服务器可以通过在yaml中配置security.oauth2.resource.user-info-uri 指定认证服务器check_token端点地址。
黑板模式
资源服务器和授权服务器使用共享存储,常见的如数据库和redis。
授权服务器生成accesstoken后会将其持久化存储起来,这样资源服务器也可以通过访问此共享存储来校验accesstoken的有效性。
JWT
如果认证服务器给你颁发的是jwt,这时候客户端再带上jwt访问资源服务器,资源服务器自己就可以直接解析令牌了,无需调用认证服务器。不过需要注意的是使用jwt的时候一定要配置密钥,推荐生产环境使用非对称密钥。
小结
oauth2 中 accesstoken校验逻辑大体就上面三种
直接调用授权服务器
使用共享数据库(黑板模式)
直接使用JWT,资源服务器自己校验
现在实际开发中大部分情况下都是使用jwt,这样可以减少资源服务器与认证服务器的交互请求,提高访问效率。
当然,如果你对oauth2协议还不是很理解,推荐你看我之前录制的一个短视频,里面有详细说明。
边栏推荐
- Number theory --- fast power, fast power inverse element
- 导数、偏导数、方向导数
- Redis入门完整教程:复制拓扑
- Code line breaking problem of untiy text box
- A complete tutorial for getting started with redis: RDB persistence
- Here comes a white paper to uncover the technology behind Clickhouse, a node with 10000 bytes!
- QT common Concepts-1
- Redis introduction complete tutorial: replication principle
- 简单冒泡排序
- 如何设计好接口测试用例?教你几个小技巧,轻松稿定
猜你喜欢
Leetcode 77: combination
MySQL is an optimization artifact to improve the efficiency of massive data query
LeetCode 77:组合
KYSL 海康摄像头 8247 h9 isapi测试
Redis getting started complete tutorial: replication topology
哈希表及完整注释
牛客编程题--必刷101之双指针篇
如何设计好接口测试用例?教你几个小技巧,轻松稿定
NuScenes数据集关于Radar数据的统计
[socket] ① overview of socket technology
随机推荐
Classify the features of pictures with full connection +softmax
wzoi 1~200
Common fitting models and application methods of PCL
安德鲁斯—-多媒体编程
Redis入门完整教程:复制配置
新标杆!智慧化社会治理
The 8 element positioning methods of selenium that you have to know are simple and practical
Utilisation de la promesse dans es6
The annual salary of general test is 15W, and the annual salary of test and development is 30w+. What is the difference between the two?
Redis introduction complete tutorial: client case analysis
Apifox, is your API interface document rolled up like this?
MySQL
进程管理基础
ERROR: Could not find a version that satisfies the requirement xxxxx (from versions: none)解决办法
Unity webgl adaptive web page size
Statistics of radar data in nuscenes data set
Cglib agent in agent mode
差异与阵列和阵列结构和链表的区别
What are the characteristics of the operation and maintenance management system
巴比特 | 元宇宙每日必读:IP授权是NFT的破圈之路吗?它的难点在哪里?Holder该如何选择合作平台?...