網(wǎng)站建設(shè)免費書廣東知名seo推廣多少錢
Hi Auth
HiAuth是一個開源的基于Oauth2協(xié)議的認(rèn)證、授權(quán)系統(tǒng),除了標(biāo)準(zhǔn)的Oauth2授權(quán)流程功能外,還提供了應(yīng)用管理、用戶管理、權(quán)限管理等相關(guān)功能。
在這個項目中你能夠了解到如何基于spring-security-oauth2-authorization-server
實現(xiàn)自己的Authorization Server 認(rèn)證服務(wù)、資源服務(wù)器以及如何第三方集成,本項目基于SpringBoot 3.0 版本開發(fā)。
項目源碼地址:https://github.com/bestaone/HiAuth
介紹
除了認(rèn)證相關(guān)功能外,還提供了hiauth-mall、hiauth-mgr-svc項目,供用戶參考如何集成。
- 參考
hiauth-mall
,你可以了解如何在第三方應(yīng)用中集成hiauth授權(quán)服務(wù); - 參考
hiauth-mgr-svc
項目,你可以快速的啟動一個微服務(wù)項目的框架搭建,亦可以在這里找到一些技術(shù)的最佳實踐,為你的項目開發(fā)提供參考;
HiAuth 2.0 升級到 3.0 的升級內(nèi)容
- SpringBoot 3.0.2
- spring-security-oauth2-authorization-server 1.0.0
- mybatis-plus 3.5.3.1
調(diào)整比較大的地方
SpringSecurity
升級到6.0
版本后,用法有不少改動SpringSecurity5
以后已不再支持Authorization Server
,取而代之的事spring-security-oauth2-authorization-server
項目- 前端從
vue-element-admin
換成了AntDesignPro
如果你覺得此項目有價值,請給我點個star,謝謝!
項目地址:https://github.com/bestaone/HiAuth
目錄結(jié)構(gòu)
├─doc 文檔目錄,架構(gòu)設(shè)計、數(shù)據(jù)庫設(shè)計...
├─cicd 持續(xù)集成相關(guān)腳本
├─hiauth-parent 統(tǒng)一管理依賴(必選)
├─hiauth-server hiauth認(rèn)證服務(wù)(必選)
├─hiauth-resource hiauth資源管理服務(wù),參考如何集成資源服務(wù)(參考)
├─hiauth-himall himall是一個demo,參考此項目了解如何集成hiauth(參考)
├─hiauth-mgr-svc hiauth管理端后臺服務(wù),基于SpringBoot(可選)
├─hiauth-mgr-fornt hiauth管理端前端代碼,基于AntDesignPro(可選)
功能
- 這個項目可以幫你實現(xiàn)基于
Oauth2
協(xié)議的統(tǒng)一認(rèn)證、授權(quán)系統(tǒng); - 這個項目可以幫助你快速的啟動一個基于
SpringBoot
技術(shù)棧的微服務(wù)框架搭建; - 如果你僅僅使用到了Oauth2協(xié)議的統(tǒng)一認(rèn)證、授權(quán)系統(tǒng)功能,那么你用技術(shù)可以是除java外的技術(shù)棧;
- himall演示了
authorization_code
模式,HiAuth支持了用戶名密碼、手機號短信兩種認(rèn)證方式; - 發(fā)送短信、登錄接口實現(xiàn)了圖形驗證碼防刷功能;
- HiAuth是一個前后端分離項目,前端使用了
React
、AntDesign
技術(shù),參考這個項目,你可以自定義開發(fā)管理后端; - 基于
SpringBoot
項目更容易集成到多個平臺(SpringCloud、K8S、Istio); - 演示了如何統(tǒng)一控制接口規(guī)范;
- 演示了如何規(guī)范異常處理;
- 演示了如何規(guī)范使用
MyBaits-Plus
、分頁; - 演示了單元測試、mock測試、測試數(shù)據(jù)回滾,包括對controller、service的測試;
截圖
- HiMall
- UMC
前端從
vue-element-admin
換成了AntDesignPro
截圖還沒來得及做新的
LIVE DEMO
- Hiauth HiMall:http://himall.hiauth.cn
- Hiauth Manger:http://mgr.hiauth.cn
- Hiauth Server:http://auth.hiauth.cn
快速啟動
環(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ù)庫
在你的mysql
數(shù)據(jù)庫中創(chuàng)建庫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ù)庫、redis,默認(rèn)會使用native.properties
配置,如果和你的環(huán)境不一致。
請修改:
# 需要將如下兩個文件中的mysql、redis的配置改成自己的
# HiAuth\hiauth-server\src\main\properties\native.properties
# HiAuth\hiauth-mgr-svc\src\main\properties\native.propertiesapp.host=http://127.0.0.1:8080database.url=jdbc:mysql://mysql-server:3306/hiauth3?useUnicode=true&characterEncoding=utf8&zeroDateTimeBehavior=convertToNull&useSSL=true&serverTimezone=GMT%2B8
database.username=dev
database.password=123456redis.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)建、啟動
# 編譯后臺,會執(zhí)行單元測試,需要正確配置數(shù)據(jù)庫和redis
>cd HiAuth
>mvn clean install# 構(gòu)建前端并啟動
>cd HiAuth\hiauth-mgr-front
>yarn install
>yarn start# 啟動hiauth授權(quán)服務(wù)端
>cd HiAuth\hiauth-server
>mvn spring-boot:run# 啟動hiauth資源服務(wù)端
>cd HiAuth\hiauth-resource
>mvn spring-boot:run# 啟動himall
>cd HiAuth\hiauth-himall
>mvn spring-boot:run# 啟動hiauth管理后端
>cd HiAuth\hiauth-mgr-svc
>mvn spring-boot:run
驗證
驗證登錄
- 訪問HiMall:http://127.0.0.1:8081
- 訪問Server:http://127.0.0.1:8080
- 訪問Manager:http://127.0.0.1:8080 (具體端口看前端項目啟動后的控制臺輸出)
驗證Swagger
- 訪問HiAuth的
Swagger
地址:http://127.0.0.1:8080/swagger-ui.html - 直接測試接口,顯示未認(rèn)證
{"error": "unauthorized","error_description": "Full authentication is required to access this resource"
}
- 點擊認(rèn)證按鈕,會被重定向到登錄,輸入賬號登錄進行認(rèn)證
- 認(rèn)證成功后被重定向回swagger頁面
- 再次測試接口,獲取正確數(shù)據(jù)
驗證Oauth2流程
authorization_code 認(rèn)證流程
- 在瀏覽器中輸入如下地址,會調(diào)到登錄頁,登錄完成后會跳轉(zhuǎn)到百度,在瀏覽器url中把
code
碼復(fù)制出來
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)上找個在線工具生成,或者直接用postman測試,Authorization
選擇Basic Auth
,填入對應(yīng)值即可
- 訪問受控接口,不帶
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"
}
- 訪問受控接口,攜帶
accessToken
,返回數(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訪問獲取
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)限范圍驗證
- 使用POST訪問獲取
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訪問用戶接口,被拒絕,提示無權(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代替,因為auth會檢查域名的合法性,數(shù)據(jù)庫中登記的是127.0.0.1
集成認(rèn)證、授權(quán)服務(wù)
這里為了演示如何集成HiAuth、提供了一個Demo項目HiMall
。
HiMall
HiMall是基于SpringBoot
技術(shù)的微服務(wù)項目,其集成了HiAuth
的認(rèn)證、授權(quán)。
環(huán)境需求
- JDK17+
- HiAuth3
安裝、啟動
#編譯、構(gòu)建項目
>cd HiAuth\hiaut-himall
>mvn clean install
>mvn spring-boot:run
驗證authorization_code
模式認(rèn)證
- 訪問地址:http://127.0.0.1:8081
- 點擊Login,會被重定向到HiAuth系統(tǒng)進行認(rèn)證
- 認(rèn)證通過后會被重定向回HiMall,此時HiMall也將持有登錄狀態(tài)
授權(quán)協(xié)議
本項目執(zhí)行 MIT 協(xié)議
社區(qū)與作者
如果群二維碼失效了,請先添加我的微信,然我我拉你入群。