基于spring-security-oauth2认证授权服务介绍与使用(二)


前言】OAuth: OAuth(开放授权)是一个开放标准,允许用户授权第三方网站访问他们存储在另外的服务提供者上的信息,而不需要将用户名和密码提供给第三方网站或分享他们数据的所有内容。

1、授权流程及示意图

其授权验证流程示意图如下( 图片来源:OAuth2.0协议草案V21的4.1节
在这里插入图片描述
参考链接:QQ互联

2、测试oauth2-server认证授权中心

思路:
授权码模式:先获取到 授权码[Authorization Code] -->通过授权码获取access_token --> 此时已授权带着access_token 去访问接口
密码模式:可直接一个请求获取到access_token --> 此时已授权带着access_token 去访问接口

2.1、采用授权码authorization_code模式

1、 获取Authorization Code

请求方式:get
请求参数[必填]:response_type、client_id、redirect_uri
请求URL:http://localhost:9090/oauth/authorize
完整URL:http://localhost:9090/oauth/authorize?response_type=code&client_id=client_1&redirect_uri=http://www.baidu.com
在这里插入图片描述
访问后跳转到如下图地址登录后–> 回调地址【http://www.baidu.com】
在这里插入图片描述
用户名和密码
选择Approve后 --> 点击按钮即可进入回调地址页面
在这里插入图片描述
_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzM0ODQ2ODc3,size_16,color_FFFFFF,t_70)
即授权码为:yMfKsK 【注】:授权码只能使用一次

2、 通过Authorization Code获取Access Token

请求方式:get
请求参数[必填]:grant_type、client_id、client_secret、code、redirect_uri
请求URL:http://localhost:9090/oauth/token
完整URL:http://localhost:9090/oauth/token?grant_type=authorization_code&client_id=client_1&client_secret=123456&code=6Xt0Dz&redirect_uri=http://www.baidu.com&scope=all
access_token
获取的access_token:1a0eb010-b6a6-4ac7-a3d9-37b0d90517ea

2.2、采用密码模式

密码模式相对简单点,直接使用postman访问了。
请求方式:post
请求参数[必填]:grant_type、username、password、client_id、client_secret
请求路径:http://localhost:9090/oauth/token
在这里插入图片描述
获取到access_token:1a0eb010-b6a6-4ac7-a3d9-37b0d90517ea

3、测试oauth2-resources-order获取资源服务器的数据

如我们要将获取订单信息的简单接口对外开放,提供数据给其他对外服务器访问,就需要获取 access_token才能访问。
http://localhost:9091/api/order/getOrder?Authorization=bearer 1a0eb010-b6a6-4ac7-a3d9-37b0d90517ea
在这里插入图片描述
上一篇:基于spring-security-oauth2认证授权服务介绍与使用(一)
项目地址:https://gitee.com/kuiwang2/spring-security-oauth2.git
参考地址:蚂蚁课堂
以上就是基于spring security-oauth2认证授权服务的简单介绍和使用,记录旨在与学习和回顾思路,分享给大家!

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章