spring-security-oauth2認(rèn)證、資源、客戶端服務(wù)器獨(dú)立搭建教程~Redis存儲(chǔ)(一)

基礎(chǔ)知識(shí):

權(quán)限注解概要:
@EnableGlobalMethodSecurity(prePostEnabled = true,jsr250Enabled = true,securedEnabled = true) 
prePostEnabled :?jiǎn)⒂聾PreAuthorize
jsr250Enabled :?jiǎn)⒂聾RolesAllowed
securedEnabled :?jiǎn)⒂聾Secured
注意事項(xiàng):@Secured對(duì)應(yīng)的角色必須要有ROLE_前綴!
OAuth2個(gè)人理解
OAuth2 :主要用于搭建開(kāi)放平臺(tái),client_id和client_secret相當(dāng)于AppId和AppSecret。比如微信開(kāi)放平臺(tái)、支付寶開(kāi)放平臺(tái),用戶接入后,可得到username、password、appId、appSecret。
OAuth2之客戶端模式:利用appId和appSecret向OAuth2-server認(rèn)證授權(quán)服務(wù)器發(fā)送請(qǐng)求,獲取Access Token,申請(qǐng)獲取訪問(wèn)資源服務(wù)器的權(quán)限。
OAuth2之密碼模式:利用appId和appSecre,再加上用戶名和密碼,向OAuth2-server認(rèn)證授權(quán)服務(wù)器發(fā)送請(qǐng)求,獲取Access Token,申請(qǐng)獲取訪問(wèn)資源服務(wù)器的權(quán)限。
因?yàn)椋艽a模式獲取到的Access Token包含了用戶信息,而客戶端模式,并不包含,所以,資源服務(wù)器僅能粗粒度控制權(quán)限(這里的資源指整個(gè)項(xiàng)目),而密碼模式則能利用用戶信息,認(rèn)證成功后(此處的認(rèn)證指的是對(duì)于Access Token校驗(yàn)是否擁有對(duì)于訪問(wèn)的資源服務(wù)器的權(quán)限)進(jìn)一步利用Spring-Security框架的注解和OAuth2的注解進(jìn)行接口甚至于方法級(jí)別的細(xì)粒度的控制。
spring-oauth-server 數(shù)據(jù)庫(kù)表說(shuō)明,參考鏈接
注意事項(xiàng):本項(xiàng)目只用到了oauth_client_details表和自定義的member表,用戶權(quán)限已寫(xiě)死為ROLE_MEMBER。

oauth_client_details表關(guān)鍵字段說(shuō)明

字段 說(shuō)明
resource_ids 資源ID標(biāo)識(shí)
client_id 相當(dāng)于AppId
client_secret 相當(dāng)于AppSecret
authorities 訪問(wèn)資源所需要的權(quán)限
scope 指定客戶端申請(qǐng)的權(quán)限范圍,可選值包括read,write,trust;若有多個(gè)權(quán)限范圍用逗號(hào)(,)分隔,如: "read,write".

技術(shù)架構(gòu):

Spring Boot + Spring Security Oauth2 + Mysql + Redis
Mysql:存儲(chǔ) ClientDetails 和 UserDetails 信息
Redis:存儲(chǔ) AccessToken、RefreshToken、Authentication等憑證信息。
注意:OAuth2的Access Token的存儲(chǔ)可以多種多樣,比如數(shù)據(jù)庫(kù)、緩存、內(nèi)存甚至是無(wú)存儲(chǔ)(即JWT實(shí)現(xiàn)),本項(xiàng)目使用的是Redis,后續(xù)將繼續(xù)JWT的實(shí)現(xiàn)。

項(xiàng)目結(jié)構(gòu):

OAuth2的認(rèn)證授權(quán)服務(wù)器:oauth2-server,簡(jiǎn)稱OS,提供 /oauth/token 接口獲取Access Token
OAuth2的資源服務(wù)器:oauth2-resource,簡(jiǎn)稱OR, 對(duì)Access Token進(jìn)行權(quán)限檢測(cè)控制
OAuth2的客戶端:oauth2-client,簡(jiǎn)稱OC, 調(diào)用 spring-security-oauth2提供的封裝代碼,向OS獲取AccessToken。

實(shí)踐是檢驗(yàn)真理的唯一標(biāo)準(zhǔn)

1.1、獲取Access Token操作步驟如下:

1、導(dǎo)入初始化數(shù)據(jù),2個(gè)表:member和oauth_client_details表數(shù)據(jù),數(shù)據(jù)庫(kù)和redis服務(wù)請(qǐng)先啟動(dòng)或者改成自己本地的。
2、啟動(dòng)oauth2-server、oauth2-client、oauth2-client服務(wù)
3、輸入oauth2-client服務(wù)的swagger2地址,http://localhost:8052/swagger-ui.html
4、使用案例數(shù)據(jù),發(fā)起調(diào)用,將成功獲取到Access Token。
1.1.1、密碼模式獲取Access Token 的swagger2請(qǐng)求結(jié)果如下:
Curl
curl -X POST --header 'Content-Type: application/json' --header 'Accept: application/json' -d '{ \ 
   "appId": "password_auth_mode", \ 
   "appSecret": "123456", \ 
   "password": "e10adc3949ba59abbe56e057f20f883e", \ 
   "username": "username" \ 
 }' 'http://localhost:8052/oauth2/token/passwordMode'
請(qǐng)求URL
http://localhost:8052/oauth2/token/passwordMode
Request Headers
{
  "Accept": "*/*"
}
響應(yīng)體
{
  "access_token": "cdd3e77e-da0d-4740-8770-867214a381fe",
  "token_type": "bearer",
  "refresh_token": "3adf9259-4c32-4dc6-9a87-332784302dec",
  "expires_in": 2591998,
  "scope": "read"
}
1.1.2、客戶端模式獲取Access Token 的swagger2請(qǐng)求結(jié)果如下:
Curl
curl -X POST --header 'Content-Type: application/json' --header 'Accept: application/json' -d '{ \ 
   "appId": "client_auth_mode", \ 
   "appSecret": "123456" \ 
 }' 'http://localhost:8052/oauth2/token/clientMode'
請(qǐng)求URL
http://localhost:8052/oauth2/token/clientMode
Request Headers
{
  "Accept": "*/*"
}
響應(yīng)體
{
  "access_token": "27533af7-6d72-4ffa-a6ff-c2a0a7c7c8e8",
  "token_type": "bearer",
  "expires_in": 2591998,
  "scope": "read write"
}

2.2、使用Access Token訪問(wèn)資源服務(wù)器的資源


1、打開(kāi)oauth2-resource的swagger2地址,http://localhost:8051/swagger-ui.html
2、點(diǎn)擊右上角的Authorization按鈕,在api_key提示的輸入框的中輸入 Bear字符串,加空格,再加上步驟一中獲取的Access Token的值,點(diǎn)擊Authorize,
則會(huì)在后續(xù)訪問(wèn)每個(gè)接口的時(shí)候,添加頭信息,key=Authorization,value = Bearer+ 空格 +AccessTokenValue (即Bearer a05da964-43b1-4427-a27f-9f2b674e6b6d)
3、點(diǎn)擊要訪問(wèn)的接口,試一下,即可體驗(yàn)效果。
2.2.1、用戶名為username所擁有的權(quán)限為ROLE_MEMBER
A、訪問(wèn)需權(quán)限ROLE_ADMIN的接口/authenticated/authorities/admin,swagger2效果如下:
Curl
curl -X GET --header 'Accept: application/json' --header 'Authorization: Bearer a05da964-43b1-4427-a27f-9f2b674e6b6d' 'http://localhost:8051/authenticated/authorities/admin'
請(qǐng)求URL
http://localhost:8051/authenticated/authorities/admin
Request Headers
{
  "Accept": "*/*"
}
響應(yīng)體
{
  "error": "access_denied",
  "error_description": "不允許訪問(wèn)"
}

B、訪問(wèn)需權(quán)限ROLE_MEMBER的接口/authenticated/authorities/admin,swagger2效果如下:
Curl
curl -X GET --header 'Accept: text/plain' --header 'Authorization: Bearer a05da964-43b1-4427-a27f-9f2b674e6b6d' 'http://localhost:8051/authenticated/authorities/member'
請(qǐng)求URL
http://localhost:8051/authenticated/authorities/member
Request Headers
{
  "Accept": "*/*"
}
響應(yīng)體
success

3、項(xiàng)目oauth2-redis-mysql-example源碼地址


4、拓展:

4.1、資源服務(wù)器、認(rèn)證授權(quán)服務(wù)器、客戶端可以獨(dú)立也可以合而唯一,本項(xiàng)目采用了獨(dú)立的形式(分別為oauth2-client、oauth2-resource、oauth2-server)。
4.2、訪問(wèn)形式一:當(dāng)前文章使用的是前端通過(guò)oauth2-client中轉(zhuǎn)向oauth2-sever獲取Access Token,然后向資源服務(wù)器oauth2-resource發(fā)起訪問(wèn)。
4.3、訪問(wèn)形式二:直接通過(guò)前端構(gòu)造請(qǐng)求,向oauth2-server獲取AccessToken,然后向資源服務(wù)器oauth2-resource發(fā)起訪問(wèn).postman接口測(cè)試文件在此


5、項(xiàng)目不足

1、用戶權(quán)限其實(shí)可以自定義拓展,當(dāng)前代碼寫(xiě)死了,后續(xù)改進(jìn)
2、Redis存儲(chǔ)Token可以使用JWT方式
3、DAO層改用mybatis-plus方式

6、個(gè)人知識(shí)有限,如若有誤,請(qǐng)指出!

嘖嘖嘖
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容