推荐一个小而全的第三方登录开源组件

打印 上一主题 下一主题

主题 876|帖子 876|积分 2630

大家好,我是 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 授权服务器,验证用户身份然后为客户端派发资源访问令牌,比如 Github
  • Resource Server 和 Authorization Server 可以是同一台服务器,也可以是不同的服务器,视具体的授权平台而有所差异
  • Client 客户端,即代表意图访问受限资源的第三方应用
授权流程

使用步骤

1、申请注册第三方平台的开发者账号
2、创建第三方平台的应用,获取配置信息(accessKey, secretKey, redirectUri)
3、使用 JustAuth 实现授权登陆
引入依赖
  1. <dependency>
  2.     <groupId>me.zhyd.oauth</groupId>
  3.     <artifactId>JustAuth</artifactId>
  4.     <version>{latest-version}</version>
  5. </dependency>
复制代码
调用 API
  1. // 创建授权request
  2. AuthRequest authRequest = new AuthGiteeRequest(AuthConfig.builder()
  3.         .clientId("clientId")
  4.         .clientSecret("clientSecret")
  5.         .redirectUri("redirectUri")
  6.         .build());
  7. // 生成授权页面
  8. authRequest.authorize("state");
  9. // 授权登录后会返回code(auth_code(仅限支付宝))、state,1.8.0版本后,可以用AuthCallback类作为回调接口的参数
  10. // 注:JustAuth默认保存state的时效为3分钟,3分钟内未使用则会自动清除过期的state
  11. authRequest.login(callback);
复制代码
说明:
JustAuth 的核心就是一个个的 request,每个平台都对应一个具体的  request 类。
所以在使用之前,需要就具体的授权平台创建响应的 request.如示例代码中对应的是 Gitee 平台。
集成国外平台

国外平台需要额外配置 httpConfig
  1. AuthRequest authRequest = new AuthGoogleRequest(AuthConfig.builder()
  2.                 .clientId("Client ID")
  3.                 .clientSecret("Client Secret")
  4.                 .redirectUri("应用回调地址")
  5.                 // 针对国外平台配置代理
  6.                 .httpConfig(HttpConfig.builder()
  7.                         // Http 请求超时时间
  8.                         .timeout(15000)
  9.                         // host 和 port 请修改为开发环境的参数
  10.                         .proxy(new Proxy(Proxy.Type.HTTP, new InetSocketAddress("127.0.0.1", 10080)))
  11.                         .build())
  12.                 .build());
复制代码
SpringBoot 集成

引入依赖
  1. <dependency>
  2.   <groupId>com.xkcoding.justauth</groupId>
  3.   <artifactId>justauth-spring-boot-starter</artifactId>
  4.   <version>1.4.0</version>
  5. </dependency>
复制代码
配置文件
  1. justauth:
  2.   enabled: true
  3.   type:
  4.     QQ:
  5.       client-id: 10**********6
  6.       client-secret: 1f7d08**********5b7**********29e
  7.       redirect-uri: http://oauth.xkcoding.com/demo/oauth/qq/callback
  8.       union-id: false
  9.     WEIBO:
  10.       client-id: 10**********6
  11.       client-secret: 1f7d08**********5b7**********29e
  12.       redirect-uri: http://oauth.xkcoding.com/demo/oauth/weibo/callback
  13.     GITEE:
  14.       client-id: 10**********6
  15.       client-secret: 1f7d08**********5b7**********29e
  16.       redirect-uri: http://oauth.xkcoding.com/demo/oauth/gitee/callback
  17.     DINGTALK:
  18.       client-id: 10**********6
  19.       client-secret: 1f7d08**********5b7**********29e
  20.       redirect-uri: http://oauth.xkcoding.com/demo/oauth/dingtalk/callback
  21.     BAIDU:
  22.       client-id: 10**********6
  23.       client-secret: 1f7d08**********5b7**********29e
  24.       redirect-uri: http://oauth.xkcoding.com/demo/oauth/baidu/callback
  25.     CSDN:
  26.       client-id: 10**********6
  27.       client-secret: 1f7d08**********5b7**********29e
  28.       redirect-uri: http://oauth.xkcoding.com/demo/oauth/csdn/callback
  29.     CODING:
  30.       client-id: 10**********6
  31.       client-secret: 1f7d08**********5b7**********29e
  32.       redirect-uri: http://oauth.xkcoding.com/demo/oauth/coding/callback
  33.       coding-group-name: xx
  34.     OSCHINA:
  35.       client-id: 10**********6
  36.       client-secret: 1f7d08**********5b7**********29e
  37.       redirect-uri: http://oauth.xkcoding.com/demo/oauth/oschina/callback
  38.     ALIPAY:
  39.       client-id: 10**********6
  40.       client-secret: 1f7d08**********5b7**********29e
  41.       redirect-uri: http://oauth.xkcoding.com/demo/oauth/alipay/callback
  42.       alipay-public-key: MIIB**************DAQAB
  43.     WECHAT_OPEN:
  44.       client-id: 10**********6
  45.       client-secret: 1f7d08**********5b7**********29e
  46.       redirect-uri: http://oauth.xkcoding.com/demo/oauth/wechat_open/callback
  47.     WECHAT_MP:
  48.       client-id: 10**********6
  49.       client-secret: 1f7d08**********5b7**********29e
  50.       redirect-uri: http://oauth.xkcoding.com/demo/oauth/wechat_mp/callback
  51.     WECHAT_ENTERPRISE:
  52.       client-id: 10**********6
  53.       client-secret: 1f7d08**********5b7**********29e
  54.       redirect-uri: http://oauth.xkcoding.com/demo/oauth/wechat_enterprise/callback
  55.       agent-id: 1000002
  56.     TAOBAO:
  57.       client-id: 10**********6
  58.       client-secret: 1f7d08**********5b7**********29e
  59.       redirect-uri: http://oauth.xkcoding.com/demo/oauth/taobao/callback
  60.     GOOGLE:
  61.       client-id: 10**********6
  62.       client-secret: 1f7d08**********5b7**********29e
  63.       redirect-uri: http://oauth.xkcoding.com/demo/oauth/google/callback
  64.     FACEBOOK:
  65.       client-id: 10**********6
  66.       client-secret: 1f7d08**********5b7**********29e
  67.       redirect-uri: http://oauth.xkcoding.com/demo/oauth/facebook/callback
  68.     DOUYIN:
  69.       client-id: 10**********6
  70.       client-secret: 1f7d08**********5b7**********29e
  71.       redirect-uri: http://oauth.xkcoding.com/demo/oauth/douyin/callback
  72.     LINKEDIN:
  73.       client-id: 10**********6
  74.       client-secret: 1f7d08**********5b7**********29e
  75.       redirect-uri: http://oauth.xkcoding.com/demo/oauth/linkedin/callback
  76.     MICROSOFT:
  77.       client-id: 10**********6
  78.       client-secret: 1f7d08**********5b7**********29e
  79.       redirect-uri: http://oauth.xkcoding.com/demo/oauth/microsoft/callback
  80.     MI:
  81.       client-id: 10**********6
  82.       client-secret: 1f7d08**********5b7**********29e
  83.       redirect-uri: http://oauth.xkcoding.com/demo/oauth/mi/callback
  84.     TOUTIAO:
  85.       client-id: 10**********6
  86.       client-secret: 1f7d08**********5b7**********29e
  87.       redirect-uri: http://oauth.xkcoding.com/demo/oauth/toutiao/callback
  88.     TEAMBITION:
  89.       client-id: 10**********6
  90.       client-secret: 1f7d08**********5b7**********29e
  91.       redirect-uri: http://oauth.xkcoding.com/demo/oauth/teambition/callback
  92.     RENREN:
  93.       client-id: 10**********6
  94.       client-secret: 1f7d08**********5b7**********29e
  95.       redirect-uri: http://oauth.xkcoding.com/demo/oauth/renren/callback
  96.     PINTEREST:
  97.       client-id: 10**********6
  98.       client-secret: 1f7d08**********5b7**********29e
  99.       redirect-uri: http://oauth.xkcoding.com/demo/oauth/pinterest/callback
  100.     STACK_OVERFLOW:
  101.       client-id: 10**********6
  102.       client-secret: 1f7d08**********5b7**********29e
  103.       redirect-uri: http://oauth.xkcoding.com/demo/oauth/stack_overflow/callback
  104.       stack-overflow-key: asd*********asd
  105.     HUAWEI:
  106.       client-id: 10**********6
  107.       client-secret: 1f7d08**********5b7**********29e
  108.       redirect-uri: http://oauth.xkcoding.com/demo/oauth/huawei/callback
  109.     KUJIALE:
  110.       client-id: 10**********6
  111.       client-secret: 1f7d08**********5b7**********29e
  112.       redirect-uri: http://oauth.xkcoding.com/demo/oauth/kujiale/callback
  113.     GITLAB:
  114.       client-id: 10**********6
  115.       client-secret: 1f7d08**********5b7**********29e
  116.       redirect-uri: http://oauth.xkcoding.com/demo/oauth/gitlab/callback
  117.     MEITUAN:
  118.       client-id: 10**********6
  119.       client-secret: 1f7d08**********5b7**********29e
  120.       redirect-uri: http://oauth.xkcoding.com/demo/oauth/meituan/callback
  121.     ELEME:
  122.       client-id: 10**********6
  123.       client-secret: 1f7d08**********5b7**********29e
  124.       redirect-uri: http://oauth.xkcoding.com/demo/oauth/eleme/callback
  125.     TWITTER:
  126.       client-id: 10**********6
  127.       client-secret: 1f7d08**********5b7**********29e
  128.       redirect-uri: http://oauth.xkcoding.com/demo/oauth/twitter/callback
  129.     XMLY:
  130.       client-id: 10**********6
  131.       client-secret: 1f7d08**********5b7**********29e
  132.       redirect-uri: http://oauth.xkcoding.com/demo/oauth/xmly/callback
  133.       # 设备唯一标识ID
  134.       device-id: xxxxxxxxxxxxxx
  135.       # 客户端操作系统类型,1-iOS系统,2-Android系统,3-Web
  136.       client-os-type: 3
  137.       # 客户端包名,如果 clientOsType 为1或2时必填。对Android客户端是包名,对IOS客户端是Bundle ID
  138.       #pack-id: xxxx
  139.     FEISHU:
  140.       client-id: 10**********6
  141.       client-secret: 1f7d08**********5b7**********29e
  142.       redirect-uri: http://oauth.xkcoding.com/demo/oauth/feishu/callback
  143.     JD:
  144.       client-id: 10**********6
  145.       client-secret: 1f7d08**********5b7**********29e
  146.       redirect-uri: http://oauth.xkcoding.com/demo/oauth/jd/callback
  147.   cache:
  148.     type: default
复制代码
代码使用
  1. @Slf4j
  2. @RestController
  3. @RequestMapping("/oauth")
  4. @RequiredArgsConstructor(onConstructor_ = @Autowired)
  5. public class TestController {
  6.     private final AuthRequestFactory factory;
  7.     @GetMapping
  8.     public List<String> list() {
  9.         return factory.oauthList();
  10.     }
  11.     @GetMapping("/login/{type}")
  12.     public void login(@PathVariable String type, HttpServletResponse response) throws IOException {
  13.         AuthRequest authRequest = factory.get(type);
  14.         response.sendRedirect(authRequest.authorize(AuthStateUtils.createState()));
  15.     }
  16.     @RequestMapping("/{type}/callback")
  17.     public AuthResponse login(@PathVariable String type, AuthCallback callback) {
  18.         AuthRequest authRequest = factory.get(type);
  19.         AuthResponse response = authRequest.login(callback);
  20.         log.info("【response】= {}", JSONUtil.toJsonStr(response));
  21.         return response;
  22.     }
  23. }
复制代码
总结

JustAuth 集成的第三方授权登录平台,可以说是囊括了业界中大部分主流的应用系统。如国内的微信、微博、Gitee 等,还有国外的 Github、Google 等。可以满足我们日常的开发需求,开箱即用,可快速集成!
最后,贴上项目地址:
  1. https://github.com/justauth/JustAuth
复制代码
在线文档:
  1. https://www.justauth.cn/
复制代码
最后

推荐的开源项目已经收录到 GitHub 项目,欢迎 Star:
  1. https://github.com/chenyl8848/great-open-source-project
复制代码
或者访问网站,进行在线浏览:
  1. https://chencoding.top:8090/#/
复制代码
大家的点赞、收藏和评论都是对作者的支持,如文章对你有帮助还请点赞转发支持下,谢谢!

免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?立即注册

x
回复

使用道具 举报

0 个回复

倒序浏览

快速回复

您需要登录后才可以回帖 登录 or 立即注册

本版积分规则

用多少眼泪才能让你相信

金牌会员
这个人很懒什么都没写!

标签云

快速回复 返回顶部 返回列表