大家好,我是 Java陳序員。 我們在企業開發中,常常需要實現登錄功能,而有時候為了方便,就需要集成第三方平臺的授權登錄。如常見的微信登錄、微博登錄等,免去了用戶註冊步驟,提高了用戶體驗。 為了業務考慮,我們有時候集成的不僅僅是一兩個第三方平臺,甚至更多。這就會大大的提高了工作量,那麼有沒有開源框架 ...
大家好,我是 Java陳序員
。
我們在企業開發中,常常需要實現登錄功能,而有時候為了方便,就需要集成第三方平臺的授權登錄。如常見的微信登錄、微博登錄等,免去了用戶註冊步驟,提高了用戶體驗。
為了業務考慮,我們有時候集成的不僅僅是一兩個第三方平臺,甚至更多。這就會大大的提高了工作量,那麼有沒有開源框架來統一來集成這些第三方授權登錄呢?
答案是有的,今天給大家介紹的項目提供了一個第三方授權登錄的工具類庫!
項目介紹
JustAuth
—— 一個第三方授權登錄的工具類庫,可以讓你脫離繁瑣的第三方登錄 SDK,讓登錄變得So easy!
JustAuth
集成了諸如:Github、Gitee、微博、釘釘、百度、Coding、騰訊雲開發者平臺、OSChina、支付寶、QQ、微信、淘寶、Google、Facebook、抖音、領英、小米、微軟、今日頭條、Teambition、StackOverflow、Pinterest、人人、華為、企業微信、酷家樂、Gitlab、美團、餓了麽、推特、飛書、京東、阿裡雲、喜馬拉雅、Amazon、Slack和 Line 等第三方平臺的授權登錄。
功能特色:
- 豐富的 OAuth 平臺:支持國內外數十家知名的第三方平臺的 OAuth 登錄。
- 自定義 state:支持自定義 State 和緩存方式,開發者可根據實際情況選擇任意緩存插件。
- 自定義 OAuth:提供統一介面,支持接入任意 OAuth 網站,快速實現 OAuth 登錄功能。
- 自定義 Http:介面 HTTP 工具,開發者可以根據自己項目的實際情況選擇相對應的HTTP工具。
- 自定義 Scope:支持自定義 scope,以適配更多的業務場景,而不僅僅是為了登錄。
- 代碼規範·簡單:JustAuth 代碼嚴格遵守阿裡巴巴編碼規約,結構清晰、邏輯簡單。
安裝使用
回顧 OAuth 授權流程
參與的角色
Resource Owner
資源所有者,即代表授權客戶端訪問本身資源信息的用戶(User),也就是應用場景中的“開發者A”Resource Server
資源伺服器,托管受保護的用戶賬號信息,比如 Github
Authorization Server 授權伺服器,驗證用戶身份然後為客戶端派發資源訪問令牌,比如 GithubResource Server
和Authorization Server
可以是同一臺伺服器,也可以是不同的伺服器,視具體的授權平臺而有所差異Client
客戶端,即代表意圖訪問受限資源的第三方應用
授權流程
使用步驟
1、申請註冊第三方平臺的開發者賬號
2、創建第三方平臺的應用,獲取配置信息(accessKey, secretKey, redirectUri)
3、使用 JustAuth
實現授權登陸
引入依賴
<dependency>
<groupId>me.zhyd.oauth</groupId>
<artifactId>JustAuth</artifactId>
<version>{latest-version}</version>
</dependency>
調用 API
// 創建授權request
AuthRequest authRequest = new AuthGiteeRequest(AuthConfig.builder()
.clientId("clientId")
.clientSecret("clientSecret")
.redirectUri("redirectUri")
.build());
// 生成授權頁面
authRequest.authorize("state");
// 授權登錄後會返回code(auth_code(僅限支付寶))、state,1.8.0版本後,可以用AuthCallback類作為回調介面的參數
// 註:JustAuth預設保存state的時效為3分鐘,3分鐘內未使用則會自動清除過期的state
authRequest.login(callback);
說明:
JustAuth
的核心就是一個個的 request,每個平臺都對應一個具體的 request 類。
所以在使用之前,需要就具體的授權平臺創建響應的 request.如示例代碼中對應的是 Gitee 平臺。
集成國外平臺
國外平臺需要額外配置
httpConfig
AuthRequest authRequest = new AuthGoogleRequest(AuthConfig.builder()
.clientId("Client ID")
.clientSecret("Client Secret")
.redirectUri("應用回調地址")
// 針對國外平臺配置代理
.httpConfig(HttpConfig.builder()
// Http 請求超時時間
.timeout(15000)
// host 和 port 請修改為開發環境的參數
.proxy(new Proxy(Proxy.Type.HTTP, new InetSocketAddress("127.0.0.1", 10080)))
.build())
.build());
SpringBoot 集成
引入依賴
<dependency>
<groupId>com.xkcoding.justauth</groupId>
<artifactId>justauth-spring-boot-starter</artifactId>
<version>1.4.0</version>
</dependency>
配置文件
justauth:
enabled: true
type:
QQ:
client-id: 10**********6
client-secret: 1f7d08**********5b7**********29e
redirect-uri: http://oauth.xkcoding.com/demo/oauth/qq/callback
union-id: false
WEIBO:
client-id: 10**********6
client-secret: 1f7d08**********5b7**********29e
redirect-uri: http://oauth.xkcoding.com/demo/oauth/weibo/callback
GITEE:
client-id: 10**********6
client-secret: 1f7d08**********5b7**********29e
redirect-uri: http://oauth.xkcoding.com/demo/oauth/gitee/callback
DINGTALK:
client-id: 10**********6
client-secret: 1f7d08**********5b7**********29e
redirect-uri: http://oauth.xkcoding.com/demo/oauth/dingtalk/callback
BAIDU:
client-id: 10**********6
client-secret: 1f7d08**********5b7**********29e
redirect-uri: http://oauth.xkcoding.com/demo/oauth/baidu/callback
CSDN:
client-id: 10**********6
client-secret: 1f7d08**********5b7**********29e
redirect-uri: http://oauth.xkcoding.com/demo/oauth/csdn/callback
CODING:
client-id: 10**********6
client-secret: 1f7d08**********5b7**********29e
redirect-uri: http://oauth.xkcoding.com/demo/oauth/coding/callback
coding-group-name: xx
OSCHINA:
client-id: 10**********6
client-secret: 1f7d08**********5b7**********29e
redirect-uri: http://oauth.xkcoding.com/demo/oauth/oschina/callback
ALIPAY:
client-id: 10**********6
client-secret: 1f7d08**********5b7**********29e
redirect-uri: http://oauth.xkcoding.com/demo/oauth/alipay/callback
alipay-public-key: MIIB**************DAQAB
WECHAT_OPEN:
client-id: 10**********6
client-secret: 1f7d08**********5b7**********29e
redirect-uri: http://oauth.xkcoding.com/demo/oauth/wechat_open/callback
WECHAT_MP:
client-id: 10**********6
client-secret: 1f7d08**********5b7**********29e
redirect-uri: http://oauth.xkcoding.com/demo/oauth/wechat_mp/callback
WECHAT_ENTERPRISE:
client-id: 10**********6
client-secret: 1f7d08**********5b7**********29e
redirect-uri: http://oauth.xkcoding.com/demo/oauth/wechat_enterprise/callback
agent-id: 1000002
TAOBAO:
client-id: 10**********6
client-secret: 1f7d08**********5b7**********29e
redirect-uri: http://oauth.xkcoding.com/demo/oauth/taobao/callback
GOOGLE:
client-id: 10**********6
client-secret: 1f7d08**********5b7**********29e
redirect-uri: http://oauth.xkcoding.com/demo/oauth/google/callback
FACEBOOK:
client-id: 10**********6
client-secret: 1f7d08**********5b7**********29e
redirect-uri: http://oauth.xkcoding.com/demo/oauth/facebook/callback
DOUYIN:
client-id: 10**********6
client-secret: 1f7d08**********5b7**********29e
redirect-uri: http://oauth.xkcoding.com/demo/oauth/douyin/callback
LINKEDIN:
client-id: 10**********6
client-secret: 1f7d08**********5b7**********29e
redirect-uri: http://oauth.xkcoding.com/demo/oauth/linkedin/callback
MICROSOFT:
client-id: 10**********6
client-secret: 1f7d08**********5b7**********29e
redirect-uri: http://oauth.xkcoding.com/demo/oauth/microsoft/callback
MI:
client-id: 10**********6
client-secret: 1f7d08**********5b7**********29e
redirect-uri: http://oauth.xkcoding.com/demo/oauth/mi/callback
TOUTIAO:
client-id: 10**********6
client-secret: 1f7d08**********5b7**********29e
redirect-uri: http://oauth.xkcoding.com/demo/oauth/toutiao/callback
TEAMBITION:
client-id: 10**********6
client-secret: 1f7d08**********5b7**********29e
redirect-uri: http://oauth.xkcoding.com/demo/oauth/teambition/callback
RENREN:
client-id: 10**********6
client-secret: 1f7d08**********5b7**********29e
redirect-uri: http://oauth.xkcoding.com/demo/oauth/renren/callback
PINTEREST:
client-id: 10**********6
client-secret: 1f7d08**********5b7**********29e
redirect-uri: http://oauth.xkcoding.com/demo/oauth/pinterest/callback
STACK_OVERFLOW:
client-id: 10**********6
client-secret: 1f7d08**********5b7**********29e
redirect-uri: http://oauth.xkcoding.com/demo/oauth/stack_overflow/callback
stack-overflow-key: asd*********asd
HUAWEI:
client-id: 10**********6
client-secret: 1f7d08**********5b7**********29e
redirect-uri: http://oauth.xkcoding.com/demo/oauth/huawei/callback
KUJIALE:
client-id: 10**********6
client-secret: 1f7d08**********5b7**********29e
redirect-uri: http://oauth.xkcoding.com/demo/oauth/kujiale/callback
GITLAB:
client-id: 10**********6
client-secret: 1f7d08**********5b7**********29e
redirect-uri: http://oauth.xkcoding.com/demo/oauth/gitlab/callback
MEITUAN:
client-id: 10**********6
client-secret: 1f7d08**********5b7**********29e
redirect-uri: http://oauth.xkcoding.com/demo/oauth/meituan/callback
ELEME:
client-id: 10**********6
client-secret: 1f7d08**********5b7**********29e
redirect-uri: http://oauth.xkcoding.com/demo/oauth/eleme/callback
TWITTER:
client-id: 10**********6
client-secret: 1f7d08**********5b7**********29e
redirect-uri: http://oauth.xkcoding.com/demo/oauth/twitter/callback
XMLY:
client-id: 10**********6
client-secret: 1f7d08**********5b7**********29e
redirect-uri: http://oauth.xkcoding.com/demo/oauth/xmly/callback
# 設備唯一標識ID
device-id: xxxxxxxxxxxxxx
# 客戶端操作系統類型,1-iOS系統,2-Android系統,3-Web
client-os-type: 3
# 客戶端包名,如果 clientOsType 為1或2時必填。對Android客戶端是包名,對IOS客戶端是Bundle ID
#pack-id: xxxx
FEISHU:
client-id: 10**********6
client-secret: 1f7d08**********5b7**********29e
redirect-uri: http://oauth.xkcoding.com/demo/oauth/feishu/callback
JD:
client-id: 10**********6
client-secret: 1f7d08**********5b7**********29e
redirect-uri: http://oauth.xkcoding.com/demo/oauth/jd/callback
cache:
type: default
代碼使用
@Slf4j
@RestController
@RequestMapping("/oauth")
@RequiredArgsConstructor(onConstructor_ = @Autowired)
public class TestController {
private final AuthRequestFactory factory;
@GetMapping
public List<String> list() {
return factory.oauthList();
}
@GetMapping("/login/{type}")
public void login(@PathVariable String type, HttpServletResponse response) throws IOException {
AuthRequest authRequest = factory.get(type);
response.sendRedirect(authRequest.authorize(AuthStateUtils.createState()));
}
@RequestMapping("/{type}/callback")
public AuthResponse login(@PathVariable String type, AuthCallback callback) {
AuthRequest authRequest = factory.get(type);
AuthResponse response = authRequest.login(callback);
log.info("【response】= {}", JSONUtil.toJsonStr(response));
return response;
}
}
總結
JustAuth
集成的第三方授權登錄平臺,可以說是囊括了業界中大部分主流的應用系統。如國內的微信、微博、Gitee 等,還有國外的 Github、Google 等。可以滿足我們日常的開發需求,開箱即用,可快速集成!
最後,貼上項目地址:
https://github.com/justauth/JustAuth
線上文檔:
https://www.justauth.cn/
最後
推薦的開源項目已經收錄到 GitHub
項目,歡迎 Star
:
https://github.com/chenyl8848/great-open-source-project
或者訪問網站,進行線上瀏覽:
https://chencoding.top:8090/#/
大家的點贊、收藏和評論都是對作者的支持,如文章對你有幫助還請點贊轉發支持下,謝謝!