什么是oauth协议

本篇文章让我带你认识一下什么是oauth协议
什么是 oauth协议 ?百度百科上解释:允许用户提供一个令牌,而不是用户名和密码来访问他们存放在特定服务提供者的数据。每一个令牌授权一个特定的网站(例如,视频编辑网站)在特定的时段(例如,接下来的2小时内)内访问特定的资源(例如仅仅是某一相册中的视频)。这样,oauth允许用户授权第三方网站访问他们存储在另外的服务提供者上的信息,而不需要分享他们的访问许可或他们数据的所有内容。
简单的来讲就是一个令牌,这个令牌可以有一定的权限,在不知道用户密码的情况下也可以进行部分的功功能操作。用一个例子帮助理解:一个甲方公司,公司里面使用一卡通,这张卡可以门禁,食堂,等等,公司招了外包进来,外包只能开通门禁就行,其他的权限是没有的。这时候公司就不会给一张正式员工的卡,而是外包的卡,正编员工卡就是用户的账号密码,这个外包卡就相当于是一个令牌,并且可能公司会给你的外包卡设置一个有效期,等有效期快到的时候有需要申请延期。这个就相当于是oauth协议模式。用户不会给你用户名和密码,那样相当于放开了所有的权限,而会给你提供一个令牌,在有效期类可以访问特定的功能服务 使用令牌的优点:
令牌是短期的,到期会自动失效,用户自己无法修改。密码一般长期有效,用户不修改,就不会发生变化。令牌可以被数据所有者撤销,会立即失效。令牌有权限范围(scope),对于网络服务来说,只读令牌就比读写令牌更安全。密码一般是完整权限oauth协议有四种模式;1、授权码模式 2、隐藏式 3、密码式 4、客户端凭证 其中授权码方式是最常用的流程,安全性也最高,今天我们就来着重讲一下授权码模式。 授权码模式: 指的是第三方应用先申请一个授权码,然后再用该码获取令牌,授权码通过前端传送,令牌则是储存在后端,而且所有与资源服务器的通信都在后端完成。这样的前后端分离,可以避免令牌泄漏。
授权码模式主要分为几个步骤以甲方公司员工卡系统(selfresearch.com)和外包公司员工卡系统(outsource.com)为例,下面是授权码模式的流程
请求授权码
首先,甲方公司给外包员工方提供一个授权链接,外包员工点击连接,申请授权码,连接参数如下所示
https://selfresearch.com/oauth/authorize?response_type=code&client_id=client_id&redirect_uri=callback_url&scope=read
response_type参数表示要求返回授权码(code), client_id参数让 甲方 知道是谁在请求, redirect_uri参数是 甲方系统 接受或拒绝请求后的跳转网址, scope参数表示要求的授权范围(这里是只读)。
返回授权码
用户跳转到这个之后,甲方公司会先要求用户登录,然后请求授权,如果同意就跳转到redirect_uri这个参数的地址,并返回授权码
https://selfresearch.com/callback?code=authorization_code
code就是表示授权码
请求令牌
外包员工拿着授权码去请求令牌
https://selfresearch.com/oauth/token?client_id=client_id&client_secret=client_secret&grant_type=authorization_code&code=authorization_code&redirect_uri=callback_url
client_id参数和client_secret参数用来让 甲方公司 确认 外包员工 的身份(client_secret参数是保密的,因此只能在后端发请求)。grant_type参数的值是authorization_code,表示采用的授权方式是授权码。code参数是上一步拿到的授权码。redirect_uri参数是令牌颁发后的回调网址。返回令牌 外包员工访问redirect_uri会得到一串json数据:
{ access_token: 67c4051b-36c8-11ec-af33-00163e0808bf, token_type: bearer, refresh_token: 71975ccc-36c8-11ec-af33-cfd2826915e5, expires_in: 3249, scope: read}access_token就是令牌了,用户需要访问其他接口就需要带上这个token去访问了 refresh_token这个是刷新令牌,如果需要使用新的token,就需要通过这个刷新令牌来获取最新的access_token。
实现 oauth2,可以分为三个步骤认证服务资源服务第三方服务现在第三方用户(test9527)想去访问资源中的部分功能(接口),但是改功能只有user角色才能访问,需要先通过认证服务器获取user的授权码,然后拿着这个code和自己账号信息去获取token,并校验通过之后才能访问到资源服务器,也就是第三方用户通过 test9527 可以访问本来需要user权限才能访问的资源功能(接口)
实现步骤:
创建三个服务oauth-server(认证服务)、resource-server(资源服务)、third-server(第三方服务
maven依赖
org.springframework.security.oauth spring-security-oauth2 2.1.3.release org.springframework.boot spring-boot-starter-securityoauth-server 服务配置文件
/** * 模拟第三方授权配置 */@enableauthorizationserver@configurationpublic class authconfig extends authorizationserverconfigureradapter { @resource clientdetailsservice clientdetailsservice; /** * 资源服务器校验token */ @override public void configure(authorizationserversecurityconfigurer security) { security.checktokenaccess(permitall()).allowformauthenticationforclients(); } /** * 第三方客户端请求配置,和资源服务访问的配置,不设置默认都可以访问,提供默认回调地址 */ @override public void configure(clientdetailsserviceconfigurer clients) throws exception { clients.inmemory() //第三方用户 .withclient(test9527) .secret(new bcryptpasswordencoder().encode(test9527)) .resourceids(resource) //认证模式 .authorizedgranttypes(authorization_code,refresh_token) .scopes(all) //回调地址 .redirecturis(http://localhost:8082/notify.html); } /** * 配置访问端点 */ @override public void configure(authorizationserverendpointsconfigurer endpoints) { endpoints.authorizationcodeservices(authorizationcodeservices()).tokenservices(tokenservices()); } /** * 内存管理 */ @bean authorizationcodeservices authorizationcodeservices() { return new inmemoryauthorizationcodeservices(); } /** * token管理规则 */ @bean authorizationservertokenservices tokenservices() { defaulttokenservices services = new defaulttokenservices(); services.setclientdetailsservice(clientdetailsservice); services.setsupportrefreshtoken(true); services.settokenstore(tokenstore()); services.setaccesstokenvalidityseconds(3600); services.setrefreshtokenvalidityseconds(3600*7); return services; } @bean tokenstore tokenstore() { return new inmemorytokenstore(); }配置spring security
/** * 模拟本地用户配置 */@configurationpublic class securityconfig extends websecurityconfigureradapter { /** * 密码加密方式 */ @bean public passwordencoder passwordencoder(){ return new bcryptpasswordencoder(); } /** * 内存中虚拟用户和角色 */ @override protected void configure(authenticationmanagerbuilder auth) throws exception { auth.inmemoryauthentication() .withuser(user) .password(new bcryptpasswordencoder().encode(123456)) .roles(user); } /** * 表单登录 */ @override protected void configure(httpsecurity http) throws exception { http.csrf().disable().formlogin(); }}resource-server配置
/** * 资源服务管理配置 */@configuration@enableresourceserverpublic class resourceserverconfig extends resourceserverconfigureradapter { /** * token令牌校验 */ @bean remotetokenservices tokenservices() { remotetokenservices services = new remotetokenservices(); services.setchecktokenendpointurl(http://localhost:8080/oauth/check_token); services.setclientid(test9527); services.setclientsecret(test9527); return services; } /** * 服务资源id配置 */ @override public void configure(resourceserversecurityconfigurer resources) throws exception { resources.resourceid(resource).tokenservices(tokenservices()); } /** * 模拟用户权限规则 */ @override public void configure(httpsecurity http) throws exception { http.authorizerequests() //访问user下的路径需要user角色 .antmatchers(/user/**).hasrole(user) .anyrequest().authenticated(); }resource下的功能(接口)
第三方服务首先认证,获取授权码code http://localhost:8080/oauth/authorize?client_id=test9527&response_type=code&scope=all&redirect_uri=http://localhost:8082/notify.html
此时会到认证服务器中的user认证
此时需要user用户同意授权,当认证服务中的user用户同意之后,到如下页面,此时看接口可以知道已经拿到code授权码,并跳转到我们需要跳转的地址
redirect_uri=http://localhost:8082/notify.html
拿到授权码之后访问回调地址 http://localhost:8082/notify.html?code=rs067l 然后通过code,client_id,client_secret 等参数访问获取令牌地址 https://selfresearch.com/oauth/token?client_id=client_id&client_secret=client_secret&grant_type=authorization_code&code=smd5nj&redirect_uri=redirect_uri=http://localhost:8082/notify.html 成功之后会返回token,刷新token,以及token有效时间,如下
拿到token之后,我们再去访问资源服务器,此时就能顺利访问到功能(接口)了
最终效果,最终第三方用户test9527访问当了资源服务器功

基于Dragonboard 410c开发板的USB摄像头实现移动侦测
BlueField DPUs和DOCA软件栈提供零信任安全
中国200架固定翼无人机集群试飞成功,无人机技术进一步提升
C语言驱动实现
热电偶传感器的特点及测温原理解析
什么是oauth协议
PCB制造商为何选择飞针来测试刚性柔性电路板
如何看懂汽车的“身份证”——车辆识别码
荣耀两款新机获得美国FCC认证
中兴迎来转机 盘点中兴危局的始末
NSA不是假5G 高通骁龙X50真5G手机选择多
5G时代的媒体离得开4K吗
Widora烧写系统的方法
日光灯长度一般有哪些_日光灯管长度尺寸标准
新唐科技M032 系列简介
大招科技:人工智能AI行业的解决方案
选择MAXQ2000微控制器和MAX1132 ADC接口的SPI时钟模式
5纳米芯片集体成了笑话?
消防应急灯的作用
5G商用网络推出 沟通便捷加快了时代的进程