基於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認證授權服務的簡單介紹和使用,記錄旨在與學習和回顧思路,分享給大家!

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