<acronym id="indot"><dfn id="indot"></dfn></acronym>
<span id="indot"></span>

<bdo id="indot"><meter id="indot"></meter></bdo>
<label id="indot"><samp id="indot"></samp></label>
<label id="indot"><xmp id="indot">
  • <span id="indot"><table id="indot"></table></span>
    <center id="indot"><optgroup id="indot"></optgroup></center>
  • <bdo id="indot"><meter id="indot"></meter></bdo>
      當(dāng)前位置:首頁 > 后端 > java > 正文內(nèi)容

      推薦一個小而全的第三方登錄開源組件

      hxing6412年前 (2023-12-18)java6840

      我們在企業(yè)開發(fā)中,常常需要實現(xiàn)登錄功能,而有時候為了方便,就需要集成第三方平臺的授權(quán)登錄。如常見的微信登錄、微博登錄等,免去了用戶注冊步驟,提高了用戶體驗。

      為了業(yè)務(wù)考慮,我們有時候集成的不僅僅是一兩個第三方平臺,甚至更多。這就會大大的提高了工作量,那么有沒有開源框架來統(tǒng)一來集成這些第三方授權(quán)登錄呢?

      答案是有的,今天給大家介紹的項目提供了一個第三方授權(quán)登錄的工具類庫!

      項目介紹

      JustAuth —— 一個第三方授權(quán)登錄的工具類庫,可以讓你脫離繁瑣的第三方登錄 SDK,讓登錄變得So easy!

      img-20231213092627.png

      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ā)資源訪問令牌,比如 Github

      • Resource Server 和 Authorization Server 可以是同一臺服務(wù)器,也可以是不同的服務(wù)器,視具體的授權(quán)平臺而有所差異

      • Client 客戶端,即代表意圖訪問受限資源的第三方應(yīng)用

      授權(quán)流程

      img-20231213092954.png

      使用步驟

      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/#/


      掃描二維碼推送至手機訪問。

      版權(quán)聲明:本文由星星博客發(fā)布,如需轉(zhuǎn)載請注明出處。

      本文鏈接:http://www.7811333.com/?id=483

      “推薦一個小而全的第三方登錄開源組件” 的相關(guān)文章

      Java泛型:理解、應(yīng)用與實踐

      在Java編程中,泛型是一項非常重要的特性,它提供了強大的類型參數(shù)化能力,使得代碼可以更加靈活、安全地處理不同類型的數(shù)據(jù)。本文將介紹Java泛型的基本概念、常見應(yīng)用場景以及相關(guān)的代碼示例,并總結(jié)如何在實際項目中合理地應(yīng)用和實踐泛型特性。一、泛型的基本概念泛型是Java語言中的一項重要特性,它允許我們...

      在Eclipse環(huán)境中查看JDK類庫的源碼

      在Java編程中,JDK(Java Development Kit)類庫是我們?nèi)粘i_發(fā)的基礎(chǔ)。它包含了Java虛擬機、Java類庫和一些命令行工具,為我們提供了豐富強大的功能。然而,很多Java開發(fā)者可能并未深入了解過JDK類庫的源代碼。實際上,通過查看JDK類庫的源代碼,我們可以更深入地理解Jav...

      Java數(shù)據(jù)類型:了解數(shù)據(jù)類型的基礎(chǔ)知識

      在Java編程中,數(shù)據(jù)類型是一項非常重要的概念。了解和正確使用數(shù)據(jù)類型可以幫助我們更好地處理數(shù)據(jù),并提高程序的性能和可讀性。本文將介紹Java中常見的數(shù)據(jù)類型,包括基本數(shù)據(jù)類型和引用數(shù)據(jù)類型,并提供一些示例代碼來演示它們的使用。 一、基本數(shù)據(jù)類型...

      Java中SimpleDateFormat、LocalDateTime和DateTimeFormatter的使用

      Java是廣泛應(yīng)用的一種編程語言,用于開發(fā)各種類型的應(yīng)用程序。在Java中,有許多日期和時間處理的類和方法可供使用,其中包括SimpleDateFormat、LocalDateTime和DateTimeFormatter。本文將介紹并演示這三個類和方法的使用。1. SimpleDateFo...

      發(fā)表評論

      訪客

      ◎歡迎參與討論,請在這里發(fā)表您的看法和觀點。
      主站蜘蛛池模板: 伊人久久婷婷五月综合97色| 色综合久久久久网| 色综合综合色综合色综合| 色综合AV综合无码综合网站| 色综合久久久无码网中文| 一本综合久久国产二区| 日日狠狠久久偷偷色综合0| 狠狠色丁香婷婷综合久久来来去| 婷婷四房综合激情五月在线| 成人综合在线视频| 91亚洲精品第一综合不卡播放| 天天综合天天添夜夜添狠狠添| 久久一本色系列综合色| 国产精品亚洲综合一区在线观看| 日本伊人色综合网| 91久久婷婷国产综合精品青草 | 久久综合九色综合欧洲| 色综合久久天天综合绕观看| 伊人久久综合影院| 国产激情电影综合在线看 | 精品久久人人做人人爽综合| 婷婷国产天堂久久综合五月| 久久婷婷综合中文字幕| 伊人久久亚洲综合影院| 国产在线视频色综合| 天天综合色天天综合| 婷婷久久香蕉五月综合| 色综久久天天综合绕视看| 色综合视频一区中文字幕| 综合欧美五月丁香五月| 久久婷婷是五月综合色狠狠| 97se亚洲综合在线| 久久综合丝袜长腿丝袜| 中文字幕亚洲综合久久综合| 国产成人综合亚洲绿色| 色综合天天综合网看在线影院| 免费国产综合视频在线看| 97久久综合精品久久久综合| 99久久综合狠狠综合久久aⅴ| 亚洲国产精品综合福利专区| 久久综合九色综合久99|