当前位置:网站首页>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协议还不是很理解,推荐你看我之前录制的一个短视频,里面有详细说明。
边栏推荐
- What are the applications and benefits of MES management system
- The so-called consumer Internet only matches and connects industry information, and does not change the industry itself
- MySQL
- Hash table and full comments
- PSINS中19维组合导航模块sinsgps详解(初始赋值部分)
- Wireshark installation
- Django database (SQLite) basic introductory tutorial
- 进程管理基础
- Electrical engineering and automation
- Here comes a white paper to uncover the technology behind Clickhouse, a node with 10000 bytes!
猜你喜欢

用全连接+softmax对图片的feature进行分类

Redis getting started complete tutorial: replication configuration

Unity使用MaskableGraphic画一条带箭头的线

How to write test cases for test coupons?

测试优惠券要怎么写测试用例?

Redis introduction complete tutorial: client case analysis
Django database (SQLite) basic introductory tutorial

Five reasons for clothing enterprises to deploy MES management system

Mmdetection3d loads millimeter wave radar data

Google Earth engine (GEE) -- 1975 dataset of Landsat global land survey
随机推荐
代码调试core-踩内存
QPushButton-》函数精解
巴比特 | 元宇宙每日必读:IP授权是NFT的破圈之路吗?它的难点在哪里?Holder该如何选择合作平台?...
Redis入門完整教程:問題定比特與優化
Detailed explanation of 19 dimensional integrated navigation module sinsgps in psins (initial assignment part)
运维管理系统有哪些特色
MySQL --- 常用函数 - 字符串函数
Redis getting started complete tutorial: replication topology
A complete tutorial for getting started with redis: problem location and optimization
Statistics of radar data in nuscenes data set
Redis入门完整教程:RDB持久化
Andrews - multimedia programming
2022 spring recruitment begins, and a collection of 10000 word interview questions will help you
wireshark安装
CDB PDB 用户权限管理
Unity custom webgl packaging template
导数、偏导数、方向导数
PSINS中19维组合导航模块sinsgps详解(滤波部分)
密码学系列之:在线证书状态协议OCSP详解
[leetcode]Search for a Range