JustAuth(第三方登录)

疯狼星 2023年11月12日 679 0

一、申请注册第三方平台的开发者账号(企业微信)

1、注册微信开放平台账号

https://open.weixin.9q.com/ (opens new window)。如果已有则忽略该步骤,直接进入第二步.

2、申请开发者资质认证这儿需要重点说明的是, 微信开放平台创建应用,需要申请开发者资质认证


如上图: 账号中心->开发者资质认证
但是微信不支持个人开发者认证,必须要企业信息才能提交认证对于个人开发者来说,我们提供二种解决方案:
(1) 用自己公司的信息做认证 (请确保合法合规)
(2)找朋友帮忙认证
(3)去TB或者其他平台花钱找人认证,这种方式一般支持: 短期租、长期认

3、创建第三方应用

认证通过后切换到“网站应用”标签页,点击“创建网站应用”按钮


创建完成后,就能在“网站应用”列表中看到相关信息

点击“查看”进入应用详情,可以看到 “OAuth三大件”:Client ID、Client Secret和Callback URL。

注意:
授权回调域”填写对应域名即可。比如我想给我的博客 https://www.zhyd.me
(opens new window) 增加微信第三方登录,那么我在配回调地址时,就只需要填写 www.zhyd.me即可,实际我们自己开发程序时, 可以随便配置具体的回调地址,只要确保是在 www.zhyd.me 下的地址就行。本例中, 我在程序中配置的回调地址为:https://www.zhyd.me/oauth/callback/wechat
重要的事情说三遍:一定要确保应用详情页的“接口信息”中的“微信登录”接口的状态为“已获得”! 一定要确保应用详情页的“接口信息”中的“微信登录”接口的状态为“已获得”! **一定要确保应用详情页的“接口信息”中的“微信登录”接口的状态为“已获得”!**否则一定会遇到这个问题:“Scope参数错误或者Scope没有权限”是怎么回事?

二、项目集成JustAuth(企业微信)

1、引入依赖

dependency>
  <groupId>me.zhyd.oauth</groupId>
  <artifactId>JustAuth</artifactId>
  <version>${latest.version}</version>
</dependency>

${latest.version}表示当前最新的版本,可以在这儿 https://mvnrepository.com/ 获取最新的版本信息。

2、完整代码

/**
     * @param source 第三方授权平台
     */
    @RequestMapping("/login/render/{source}")
    public void renderAuth(@PathVariable("source") String source, HttpServletResponse response) throws IOException {
        AuthRequest authRequest = getAuthRequest(source);
        String authorizeUrl = authRequest.authorize(AuthStateUtils.createState());
        response.sendRedirect(authorizeUrl);
    }

    /**
     * @param source oauth平台中配置的授权回调地址
     */
    @RequestMapping("/login/callback/{source}")
    public R loginAuth(@PathVariable("source") String source, AuthCallback callback) {
        AuthRequest authRequest = getAuthRequest(source);
        AuthResponse<AuthUser> response = authRequest.login(callback);
        log.info(JSONUtil.toJsonStr(response));
        if (response.ok()) {
            return R.ok(JSONUtil.toJsonStr(response));
        }
        Map<String, Object> map = new HashMap<>(1);
        map.put("errorMsg", response.getMsg());
        return R.ok(map);
    }

    public AuthRequest getAuthRequest(String source) {
        AuthRequest authRequest = null;
        switch (source.toLowerCase()) {
            case "gitee":
                authRequest = new AuthGiteeRequest(AuthConfig.builder()
                        .clientId(justAuthProperties.getGitee().getClientId())
                        .clientSecret(justAuthProperties.getGitee().getClientSecret())
                        .redirectUri(justAuthProperties.getGitee().getRedirectUri())
                        .build());
                break;
            case "wechat":
                authRequest = new AuthWeChatOpenRequest(AuthConfig.builder()
                        .clientId(justAuthProperties.getWechat().getClientId())
                        .clientSecret(justAuthProperties.getWechat().getClientSecret())
                        .redirectUri(justAuthProperties.getWechat().getRedirectUri())
                        .build());
                break;
            default:
                break;
        }
        return authRequest;
    }

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



Last Updated: 2023/11/12 15:50:32
企业微信结合文言一心(智能客服) 发布Maven中央仓库
您的备案号 粤B2-20****59-1