当前位置:网站首页>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协议还不是很理解,推荐你看我之前录制的一个短视频,里面有详细说明。
边栏推荐
- QT常见概念-1
- Unity custom webgl packaging template
- Apifox, is your API interface document rolled up like this?
- MySQL - common functions - string functions
- Es6中Promise的使用
- [secretly kill little partner pytorch20 days] - [Day1] - [example of structured data modeling process]
- CDB PDB 用户权限管理
- 从零安装Redis
- 安德鲁斯—-多媒体编程
- C language exercises_ one
猜你喜欢

Hash table and full comments

The panel floating with the mouse in unity can adapt to the size of text content

What are the characteristics of the operation and maintenance management system

6-6漏洞利用-SSH安全防御

数字滚动增加效果

KYSL 海康摄像头 8247 h9 isapi测试

wireshark安装

C language exercises_ one

Redis introduction complete tutorial: replication principle

Kysl Haikang camera 8247 H9 ISAPI test
随机推荐
Redis入门完整教程:问题定位与优化
A complete tutorial for getting started with redis: AOF persistence
Summary of basic debugging steps of S120 driver
数论 --- 快速幂、快速幂求逆元
Wireshark installation
Digital scrolling increases effect
CDB PDB user rights management
导数、偏导数、方向导数
Remember the problem analysis of oom caused by a Jap query
MySQL is an optimization artifact to improve the efficiency of massive data query
2022 spring recruitment begins, and a collection of 10000 word interview questions will help you
Error in fasterxml tostringserializerbase
代码调试core-踩内存
MySQL
Leetcode 77: combination
一本揭秘字节万台节点ClickHouse背后技术实现的白皮书来了!
【Socket】①Socket技术概述
Redis入门完整教程:AOF持久化
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?
MySQL提升大量数据查询效率的优化神器