JustAuth JustAuth
首页
开始使用🔥
  • 使用State
  • 自定义state缓存
  • 自定义第三方平台的OAuth
  • 自定义Scope
  • JustAuth与现有用户系统整合
  • 异常问题
  • 功能问题
  • 数据看板🔥
  • 贡献指南
  • 行为准则
  • 用户权益
  • 贡献者们
  • 社区配套 (opens new window)
  • 项目荣誉🔥
  • 精选文章
社区活动
  • 关于
  • 友情链接
  • 其他开源
  • 更新记录
收藏
GitHub (opens new window)

JustAuth

开箱即用的整合第三方登录的开源组件
首页
开始使用🔥
  • 使用State
  • 自定义state缓存
  • 自定义第三方平台的OAuth
  • 自定义Scope
  • JustAuth与现有用户系统整合
  • 异常问题
  • 功能问题
  • 数据看板🔥
  • 贡献指南
  • 行为准则
  • 用户权益
  • 贡献者们
  • 社区配套 (opens new window)
  • 项目荣誉🔥
  • 精选文章
社区活动
  • 关于
  • 友情链接
  • 其他开源
  • 更新记录
收藏
GitHub (opens new window)
  • 使用指南
  • 名词解释
  • 快速开始

  • 集成第三方

    • 尝鲜版(快照)

    • 微信生态

    • 常用平台

    • 技术平台

      • Gitee登录
      • Github登录
      • 开源中国登录
        • 1. 申请应用
          • 1.1 登录钉钉开发者中心
          • 1.2 创建第三方授权应用
        • 2. 集成JustAuth
          • 2.1 引入依赖
          • 2.2 创建Request
          • 2.3 生成授权地址
          • 2.4 以上完整代码如下
        • 3. 授权结果
        • 3. 推荐
      • StackOverflow登录
      • Coding(腾讯云)登录
      • 程序员客栈登录
      • CSDN登录
    • 企业平台

    • 国外平台

    • 其他平台

  • 参考文档
  • 指南
  • 集成第三方
  • 技术平台
JustAuth
2021-09-29

开源中国登录

# 1. 申请应用

# 1.1 登录钉钉开发者中心

  1. 登录开源中国:开源中国 (opens new window)
  2. 点击访问应用管理页面:应用管理 (opens new window)

# 1.2 创建第三方授权应用

  1. 在开源中国应用管理页面,点击“创建应用”
  2. 填写基本信息
  3. 创建后即可看到 应用ID 和 应用私钥。

记录以下三个信息:应用ID、应用私钥和回调地址,后面我们会用到。

# 2. 集成JustAuth

# 2.1 引入依赖

<dependency>
  <groupId>me.zhyd.oauth</groupId>
  <artifactId>JustAuth</artifactId>
  <version>${latest.version}</version>
</dependency>
1
2
3
4
5

${latest.version}推荐使用最新版本,快照版:,可以在这儿 (opens new window)获取最新的版本信息。

# 2.2 创建Request

AuthRequest authRequest = new AuthOschinaRequest(AuthConfig.builder()
                .clientId("Client ID")
                .clientSecret("Client Secret")
                .redirectUri("应用回调地址")
                .build());
1
2
3
4
5

# 2.3 生成授权地址

我们可以直接使用以下方式生成第三方平台的授权链接:

String authorizeUrl = authRequest.authorize(AuthStateUtils.createState());
1

这个链接我们可以直接后台重定向跳转,也可以返回到前端后,前端控制跳转。前端控制的好处就是,可以将第三方的授权页嵌入到iframe中,适配网站设计。

# 2.4 以上完整代码如下

import me.zhyd.oauth.config.AuthConfig;
import me.zhyd.oauth.request.AuthOschinaRequest;
import me.zhyd.oauth.model.AuthCallback;
import me.zhyd.oauth.request.AuthRequest;
import me.zhyd.oauth.utils.AuthStateUtils;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;


@RestController
@RequestMapping("/oauth")
public class RestAuthController {

    @RequestMapping("/render")
    public void renderAuth(HttpServletResponse response) throws IOException {
        AuthRequest authRequest = getAuthRequest();
        response.sendRedirect(authRequest.authorize(AuthStateUtils.createState()));
    }

    @RequestMapping("/callback")
    public Object login(AuthCallback callback) {
        AuthRequest authRequest = getAuthRequest();
        return authRequest.login(callback);
    }

    private AuthRequest getAuthRequest() {
        return new AuthOschinaRequest(AuthConfig.builder()
                .clientId("应用ID")
                .clientSecret("应用私钥")
                .redirectUri("回调地址")
                .build());
    }
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37

授权链接访问成功后会看到以下页面内容:

点击“连接”即可完成 OAuth 登录。

# 3. 授权结果

注意

数据已脱敏

{
    "code":2000,
    "data":{
        "avatar":"https://oscimg.oschina.net/oscnet/up-2247e2b7c5c9907f70ba2648f9db112d.jpg!/both/50x50?t=1451008261000",
        "blog":"https://my.oschina.net/yadong0415",
        "email":"yadong.zhang0415@gmail.com",
        "gender":"FEMALE",
        "location":"北京 朝阳",
        "nickname":"HandsomeBoy丶",
        "rawUserInfo":{
            "gender":"female",
            "name":"HandsomeBoy丶",
            "location":"北京 朝阳",
            "id":2564550,
            "avatar":"https://oscimg.oschina.net/oscnet/up-2247e2b7c5c9907f70ba2648f9db112d.jpg!/both/50x50?t=1451008261000",
            "email":"yadong.zhang0415@gmail.com",
            "url":"https://my.oschina.net/yadong0415"
        },
        "source":"OSCHINA",
        "token":{
            "accessToken":"826c0ffaae486e45a657",
            "expireIn":604799,
            "refreshToken":"dc83aa5aaafc786cf",
            "uid":"2aaa0"
        },
        "username":"HandsomeBoy丶",
        "uuid":"2aa0"
    }
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29

# 3. 推荐

官方推荐使用 JustAuth-demo (opens new window) 示例项目进行测试。

使用步骤:

  1. clone: https://github.com/justauth/JustAuth-demo.git (opens new window)
  2. 将上面申请的应用信息填入到RestAuthController#getAuthRequest方法的对应位置中:
  3. 启动项目,访问 http://localhost:8443 (opens new window)
  4. 选择对应的平台进行授权登录
  5. 登录完成后,可以访问http://localhost:8443/users (opens new window)查看已授权的用户

注意

  1. 如果直接使用 JustAuth-demo 项目进行测试,那么在配置测试应用的“回调地址”时要严格按照以下格式配置:http://localhost:8443/oauth/callback/{平台名}
  2. 平台名参考 JustAuthPlatformInfo 枚举类 names
编辑 (opens new window)
#OAuth#开源中国#OSChina
Last Updated: 2024/09/01, 18:42:02
Github登录
StackOverflow登录

← Github登录 StackOverflow登录→

最近更新
01
AppleID登录
09-13
02
关于
09-13
03
企业微信二维码登录(新)
08-04
更多文章>
Theme by Vdoing | Copyright © 2021-2024

友情链接:UniAdmin | 江如意的博客

Written by Yadong.Zhang | 鲁ICP备17054970号-3 |
  • 跟随系统
  • 浅色模式
  • 深色模式
  • 阅读模式