HiAuth是一個(gè)開(kāi)源的基于Oauth2協(xié)議的認(rèn)證芭析、授權(quán)系統(tǒng)锚扎,除了標(biāo)準(zhǔn)的Oauth2授權(quán)流程功能外,還提供了應(yīng)用管理馁启、用戶管理驾孔、權(quán)限管理等相關(guān)功能芍秆。
在這個(gè)項(xiàng)目中你能夠了解到如何基于spring-security-oauth2-authorization-server
實(shí)現(xiàn)自己的Authorization Server 認(rèn)證服務(wù)、資源服務(wù)器以及如何第三方集成翠勉,本項(xiàng)目基于SpringBoot 3.0 版本開(kāi)發(fā)妖啥。
項(xiàng)目源碼地址:https://github.com/bestaone/HiAuth
介紹
除了認(rèn)證相關(guān)功能外,還提供了hiauth-mall对碌、hiauth-mgr-svc項(xiàng)目荆虱,供用戶參考如何集成。
- 參考
hiauth-mall
朽们,你可以了解如何在第三方應(yīng)用中集成hiauth授權(quán)服務(wù)怀读; - 參考
hiauth-mgr-svc
項(xiàng)目,你可以快速的啟動(dòng)一個(gè)微服務(wù)項(xiàng)目的框架搭建华坦,亦可以在這里找到一些技術(shù)的最佳實(shí)踐愿吹,為你的項(xiàng)目開(kāi)發(fā)提供參考;
HiAuth 2.0 升級(jí)到 3.0 的升級(jí)內(nèi)容
- SpringBoot 3.0.2
- spring-security-oauth2-authorization-server 1.0.0
- mybatis-plus 3.5.3.1
調(diào)整比較大的地方
-
SpringSecurity
升級(jí)到6.0
版本后惜姐,用法有不少改動(dòng) -
SpringSecurity5
以后已不再支持Authorization Server
犁跪,取而代之的事spring-security-oauth2-authorization-server
項(xiàng)目 - 前端從
vue-element-admin
換成了AntDesignPro
如果你覺(jué)得此項(xiàng)目有價(jià)值,請(qǐng)給我點(diǎn)個(gè)star歹袁,謝謝坷衍!
目錄結(jié)構(gòu)
├─doc 文檔目錄,架構(gòu)設(shè)計(jì)条舔、數(shù)據(jù)庫(kù)設(shè)計(jì)...
├─cicd 持續(xù)集成相關(guān)腳本
├─hiauth-parent 統(tǒng)一管理依賴(必選)
├─hiauth-server hiauth認(rèn)證服務(wù)(必選)
├─hiauth-resource hiauth資源管理服務(wù)枫耳,參考如何集成資源服務(wù)(參考)
├─hiauth-himall himall是一個(gè)demo,參考此項(xiàng)目了解如何集成hiauth(參考)
├─hiauth-mgr-svc hiauth管理端后臺(tái)服務(wù)孟抗,基于SpringBoot(可選)
├─hiauth-mgr-fornt hiauth管理端前端代碼迁杨,基于AntDesignPro(可選)
功能
- 這個(gè)項(xiàng)目可以幫你實(shí)現(xiàn)基于
Oauth2
協(xié)議的統(tǒng)一認(rèn)證、授權(quán)系統(tǒng)凄硼; - 這個(gè)項(xiàng)目可以幫助你快速的啟動(dòng)一個(gè)基于
SpringBoot
技術(shù)棧的微服務(wù)框架搭建铅协; - 如果你僅僅使用到了Oauth2協(xié)議的統(tǒng)一認(rèn)證、授權(quán)系統(tǒng)功能摊沉,那么你用技術(shù)可以是除java外的技術(shù)棧狐史;
- himall演示了
authorization_code
模式,HiAuth支持了用戶名密碼说墨、手機(jī)號(hào)短信兩種認(rèn)證方式骏全; - 發(fā)送短信、登錄接口實(shí)現(xiàn)了圖形驗(yàn)證碼防刷功能尼斧;
- HiAuth是一個(gè)前后端分離項(xiàng)目姜贡,前端使用了
React
、AntDesign
技術(shù)棺棵,參考這個(gè)項(xiàng)目鲁豪,你可以自定義開(kāi)發(fā)管理后端潘悼; - 基于
SpringBoot
項(xiàng)目更容易集成到多個(gè)平臺(tái)(SpringCloud、K8S爬橡、Istio); - 演示了如何統(tǒng)一控制接口規(guī)范棒动;
- 演示了如何規(guī)范異常處理糙申;
- 演示了如何規(guī)范使用
MyBaits-Plus
、分頁(yè)船惨; - 演示了單元測(cè)試柜裸、mock測(cè)試、測(cè)試數(shù)據(jù)回滾粱锐,包括對(duì)controller疙挺、service的測(cè)試;
截圖
-
HiMall
- UMC
前端從
vue-element-admin
換成了AntDesignPro
截圖還沒(méi)來(lái)得及做新的
LIVE DEMO
- Hiauth HiMall:http://himall.hiauth.cn
- Hiauth Manger:http://mgr.hiauth.cn
- Hiauth Server:http://auth.hiauth.cn
快速啟動(dòng)
環(huán)境需求
- JDK17+
- Maven 3.8.6
- MySQL 8
- Redis
- NodeJS v16+(<v18)
- Yarn 1.22.4
下載源碼
>git clone https://github.com/bestaone/HiAuth.git
創(chuàng)建數(shù)據(jù)庫(kù)
在你的mysql
數(shù)據(jù)庫(kù)中創(chuàng)建庫(kù)hiauth怜浅,并執(zhí)行下面腳本:
> HiAuth\doc\hiauth.sql
# 也可以從JAR自己提取相關(guān)表結(jié)構(gòu)
# org.springframework.security.oauth2.server.authorization.client/oauth2-registered-client-schema.sql
# org.springframework.security.oauth2.server.authorization/oauth2-authorization-consent-schema.sql
# org.springframework.security.oauth2.server.authorization/oauth2-authorization-schema.sql
調(diào)整配置
需要調(diào)整的配置有數(shù)據(jù)庫(kù)铐然、redis,默認(rèn)會(huì)使用native.properties
配置恶座,如果和你的環(huán)境不一致搀暑。
請(qǐng)修改:
# 需要將如下兩個(gè)文件中的mysql、redis的配置改成自己的
# HiAuth\hiauth-server\src\main\properties\native.properties
# HiAuth\hiauth-mgr-svc\src\main\properties\native.properties
app.host=http://127.0.0.1:8080
database.url=jdbc:mysql://mysql-server:3306/hiauth3?useUnicode=true&characterEncoding=utf8&zeroDateTimeBehavior=convertToNull&useSSL=true&serverTimezone=GMT%2B8
database.username=dev
database.password=123456
redis.host=redis-server
redis.port=6379
redis.database=8
redis.password=
添加host
# IP 換成你自己的
127.0.0.1 redis-server
127.0.0.1 mysql-server
構(gòu)建跨琳、啟動(dòng)
# 編譯后臺(tái)自点,會(huì)執(zhí)行單元測(cè)試,需要正確配置數(shù)據(jù)庫(kù)和redis
>cd HiAuth
>mvn clean install
# 構(gòu)建前端并啟動(dòng)
>cd HiAuth\hiauth-mgr-front
>yarn install
>yarn start
# 啟動(dòng)hiauth授權(quán)服務(wù)端
>cd HiAuth\hiauth-server
>mvn spring-boot:run
# 啟動(dòng)hiauth資源服務(wù)端
>cd HiAuth\hiauth-resource
>mvn spring-boot:run
# 啟動(dòng)himall
>cd HiAuth\hiauth-himall
>mvn spring-boot:run
# 啟動(dòng)hiauth管理后端
>cd HiAuth\hiauth-mgr-svc
>mvn spring-boot:run
驗(yàn)證
驗(yàn)證登錄
- 訪問(wèn)HiMall:http://127.0.0.1:8081
- 訪問(wèn)Server:http://127.0.0.1:8080
- 訪問(wèn)Manager:http://127.0.0.1:8080 (具體端口看前端項(xiàng)目啟動(dòng)后的控制臺(tái)輸出)
驗(yàn)證Swagger
- 訪問(wèn)HiAuth的
Swagger
地址:http://127.0.0.1:8080/swagger-ui.html - 直接測(cè)試接口脉让,顯示未認(rèn)證
{
"error": "unauthorized",
"error_description": "Full authentication is required to access this resource"
}
- 點(diǎn)擊認(rèn)證按鈕桂敛,會(huì)被重定向到登錄,輸入賬號(hào)登錄進(jìn)行認(rèn)證
- 認(rèn)證成功后被重定向回swagger頁(yè)面
- 再次測(cè)試接口溅潜,獲取正確數(shù)據(jù)
驗(yàn)證Oauth2流程
authorization_code 認(rèn)證流程
- 在瀏覽器中輸入如下地址术唬,會(huì)調(diào)到登錄頁(yè),登錄完成后會(huì)跳轉(zhuǎn)到百度伟恶,在瀏覽器url中把
code
碼復(fù)制出來(lái)
http://127.0.0.1:8080/oauth2/authorize?client_id=demo-client-id&response_type=code&scope=user_info&redirect_uri=http://www.baidu.com
- 使用上面獲取的
code
換取accessToken
curl --location --request POST 'http://127.0.0.1:8080/oauth2/token?grant_type=authorization_code&code=code&redirect_uri=http://www.baidu.com' \
--header 'Authorization: Basic ZGVtby1jbGllbnQtaWQ6ZGVtby1jbGllbnQtc2VjcmV0'
{
"access_token": "xxxxxx",
"refresh_token": "yyyyy",
"scope": "user_info",
"token_type": "Bearer",
"expires_in": 7199
}
Authorization = Basic base64.encode(client_id:client_secret)
可以在網(wǎng)上找個(gè)在線工具生成碴开,或者直接用postman測(cè)試,Authorization
選擇Basic Auth
博秫,填入對(duì)應(yīng)值即可
- 訪問(wèn)受控接口潦牛,不帶
accessToken
,返回401挡育,未授權(quán)
>curl --location --request POST 'http://127.0.0.1:8082/user/info'
{
"error": "unauthorized",
"error_description": "Full authentication is required to access this resource"
}
- 訪問(wèn)受控接口巴碗,攜帶
accessToken
,返回?cái)?shù)據(jù)
curl --location --request POST 'http://127.0.0.1:8082/user/info' --header 'Authorization: Bearer token'
{
"name":"Resource"
}
refresh_token
curl --location --request POST 'http://127.0.0.1:8080/oauth2/token?grant_type=refresh_token&refresh_token=refresh_token' \
--header 'Authorization: Basic ZGVtby1jbGllbnQtaWQ6ZGVtby1jbGllbnQtc2VjcmV0'
{
"access_token": "xxxxxx",
"refresh_token": "yyyyy",
"scope": "user_info",
"token_type": "Bearer",
"expires_in": 6152
}
client_credentials 認(rèn)證流程
- 使用POST訪問(wèn)獲取
access_token
接口即寒,設(shè)置grant_type=client_credentials
curl --location --request POST 'http://127.0.0.1:8080/oauth2/token?grant_type=client_credentials&scope=user_info' \
--header 'Authorization: Basic ZGVtby1jbGllbnQtaWQ6ZGVtby1jbGllbnQtc2VjcmV0'
{
"access_token": "xxxxxx",
"scope": "user_info",
"token_type": "Bearer",
"expires_in": 7199
}
scop權(quán)限范圍驗(yàn)證
- 使用POST訪問(wèn)獲取
access_token
接口,設(shè)置grant_type=client_credentials
橡淆,scope=message.read
> curl --location --request POST 'http://127.0.0.1:8080/oauth2/token?grant_type=client_credentials&scope=message.read' --header 'Authorization: Basic ZGVtby1jbGllbnQtaWQ6ZGVtby1jbGllbnQtc2VjcmV0'
# 返回的 token 的權(quán)限范圍是 message.read
{
"access_token": "xxxxxx",
"scope": "message.read",
"token_type": "Bearer",
"expires_in": 7199
}
- 使用擁有
message.read
權(quán)限的toke訪問(wèn)用戶接口召噩,被拒絕,提示無(wú)權(quán)限
curl --location --request POST 'http://127.0.0.1:8082/user/info' --header 'Authorization: Bearer token'
{
"error": "insufficient_scope",
"error_description": "Insufficient scope for this resource",
"scope": "user_info"
}
所有的127.0.0.1不能使用localhost代替逸爵,因?yàn)閍uth會(huì)檢查域名的合法性具滴,數(shù)據(jù)庫(kù)中登記的是127.0.0.1
集成認(rèn)證、授權(quán)服務(wù)
這里為了演示如何集成HiAuth师倔、提供了一個(gè)Demo項(xiàng)目HiMall
构韵。
HiMall
HiMall是基于SpringBoot
技術(shù)的微服務(wù)項(xiàng)目,其集成了HiAuth
的認(rèn)證趋艘、授權(quán)疲恢。
環(huán)境需求
- JDK17+
- HiAuth3
安裝、啟動(dòng)
#編譯瓷胧、構(gòu)建項(xiàng)目
>cd HiAuth\hiaut-himall
>mvn clean install
>mvn spring-boot:run
驗(yàn)證authorization_code
模式認(rèn)證
- 訪問(wèn)地址:http://127.0.0.1:8081
- 點(diǎn)擊Login显拳,會(huì)被重定向到HiAuth系統(tǒng)進(jìn)行認(rèn)證
- 認(rèn)證通過(guò)后會(huì)被重定向回HiMall,此時(shí)HiMall也將持有登錄狀態(tài)
授權(quán)協(xié)議
本項(xiàng)目執(zhí)行 MIT 協(xié)議