推薦一個小而全的第三方登錄開源組件
我們在企業(yè)開發(fā)中,常常需要實現(xiàn)登錄功能,而有時候為了方便,就需要集成第三方平臺的授權(quán)登錄。如常見的微信登錄、微博登錄等,免去了用戶注冊步驟,提高了用戶體驗。
為了業(yè)務(wù)考慮,我們有時候集成的不僅僅是一兩個第三方平臺,甚至更多。這就會大大的提高了工作量,那么有沒有開源框架來統(tǒng)一來集成這些第三方授權(quán)登錄呢?
答案是有的,今天給大家介紹的項目提供了一個第三方授權(quán)登錄的工具類庫!
項目介紹
JustAuth
—— 一個第三方授權(quán)登錄的工具類庫,可以讓你脫離繁瑣的第三方登錄 SDK,讓登錄變得So easy!
JustAuth
集成了諸如:Github、Gitee、微博、釘釘、百度、Coding、騰訊云開發(fā)者平臺、OSChina、支付寶、QQ、微信、淘寶、Google、Facebook、抖音、領(lǐng)英、小米、微軟、今日頭條、Teambition、StackOverflow、Pinterest、人人、華為、企業(yè)微信、酷家樂、Gitlab、美團、餓了么、推特、飛書、京東、阿里云、喜馬拉雅、Amazon、Slack和 Line 等第三方平臺的授權(quán)登錄。
功能特色:
豐富的 OAuth 平臺:支持國內(nèi)外數(shù)十家知名的第三方平臺的 OAuth 登錄。
自定義 state:支持自定義 State 和緩存方式,開發(fā)者可根據(jù)實際情況選擇任意緩存插件。
自定義 OAuth:提供統(tǒng)一接口,支持接入任意 OAuth 網(wǎng)站,快速實現(xiàn) OAuth 登錄功能。
自定義 Http:接口 HTTP 工具,開發(fā)者可以根據(jù)自己項目的實際情況選擇相對應(yīng)的HTTP工具。
自定義 Scope:支持自定義 scope,以適配更多的業(yè)務(wù)場景,而不僅僅是為了登錄。
代碼規(guī)范·簡單:JustAuth 代碼嚴(yán)格遵守阿里巴巴編碼規(guī)約,結(jié)構(gòu)清晰、邏輯簡單。
安裝使用
回顧 OAuth 授權(quán)流程
參與的角色
Resource Owner
資源所有者,即代表授權(quán)客戶端訪問本身資源信息的用戶(User),也就是應(yīng)用場景中的“開發(fā)者A”Resource Server
資源服務(wù)器,托管受保護的用戶賬號信息,比如 Github
Authorization Server 授權(quán)服務(wù)器,驗證用戶身份然后為客戶端派發(fā)資源訪問令牌,比如 GithubResource Server
和Authorization Server
可以是同一臺服務(wù)器,也可以是不同的服務(wù)器,視具體的授權(quán)平臺而有所差異Client
客戶端,即代表意圖訪問受限資源的第三方應(yīng)用
授權(quán)流程
使用步驟
1、申請注冊第三方平臺的開發(fā)者賬號
2、創(chuàng)建第三方平臺的應(yīng)用,獲取配置信息(accessKey, secretKey, redirectUri)
3、使用 JustAuth
實現(xiàn)授權(quán)登陸
引入依賴
<dependency> <groupId>me.zhyd.oauth</groupId> <artifactId>JustAuth</artifactId> <version>{latest-version}</version> </dependency>
調(diào)用 API
// 創(chuàng)建授權(quán) requestAuthRequest authRequest = new AuthGiteeRequest(AuthConfig.builder() .clientId("clientId") .clientSecret("clientSecret") .redirectUri("redirectUri") .build());// 生成授權(quán)頁面 authRequest.authorize("state"); // 授權(quán)登錄后會返回code(auth_code(僅限支付寶))、state,1.8.0版本后,可以用AuthCallback類作為回調(diào)接口的參數(shù) // 注:JustAuth默認(rèn)保存state的時效為3分鐘,3分鐘內(nèi)未使用則會自動清除過期的 stateauthRequest.login(callback);
說明:
JustAuth
的核心就是一個個的 request,每個平臺都對應(yīng)一個具體的 request 類。
所以在使用之前,需要就具體的授權(quán)平臺創(chuàng)建響應(yīng)的 request.如示例代碼中對應(yīng)的是 Gitee 平臺。
集成國外平臺
國外平臺需要額外配置
httpConfig
AuthRequest authRequest = new AuthGoogleRequest(AuthConfig.builder() .clientId("Client ID"). clientSecret("Client Secret").redirectUri("應(yīng)用回調(diào)地址") // 針對國外平臺配置代理 .httpConfig(HttpConfig.builder() // Http 請求超時時間 .timeout(15000) // host 和 port 請修改為開發(fā)環(huán)境的參數(shù) .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 # 設(shè)備唯一標(biāo)識ID device-id: xxxxxxxxxxxxxx # 客戶端操作系統(tǒng)類型,1-iOS系統(tǒng),2-Android系統(tǒng),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; } }
總結(jié)
JustAuth
集成的第三方授權(quán)登錄平臺,可以說是囊括了業(yè)界中大部分主流的應(yīng)用系統(tǒng)。如國內(nèi)的微信、微博、Gitee 等,還有國外的 Github、Google 等??梢詽M足我們?nèi)粘5拈_發(fā)需求,開箱即用,可快速集成!
最后,貼上項目地址:
https://github.com/justauth/JustAuth
在線文檔:
https://www.justauth.cn/
最后
推薦的開源項目已經(jīng)收錄到 GitHub
項目,歡迎 Star
:
https://github.com/chenyl8848/great-open-source-project
或者訪問網(wǎng)站,進(jìn)行在線瀏覽:
https://chencoding.top:8090/#/