Java 实现企业微信扫码登录

[复制链接]
发表于 2026-3-10 11:17:43 | 显示全部楼层 |阅读模式
一、场景

Web 体系登录页面“企业微信”图标,点击后调取企业微信扫码登录页面,扫码乐成后判定体系账号与企业微光荣户ID绑定环境,有则直接登录,无则跳转到绑定登录页面,用户手动输入个人账号暗码举行绑定,初次绑定后后续无需再绑定。
二、企业微信应用相干设置

1、新建企微应用

进入企微管理背景,应用管理模块,点击创建应用,填写应用名称、先容,设置应用Logo,点击创建应用。

 2、应用 ID 和 Secret

创建好应用后,生存好应用的 AgentId 和 Secret,后续体系调用企微接口会用到。

以及企业ID(在管理背景 - 我的企业中查察)。

 3、设置可信域名、授权回调域、企业可信IP

如果体系所在为 http://www.oa.com:8099,则可信域名、授权回调域填写 www.oa.com:8099。而且确保体系授权流程中的回调接口所在也是 http://www.oa.com:8099/xxx/xxx 格式,有恣意一处对不上,举行扫码登录时会提示:edirect_uri 与设置的授权完成回调域名不同等 标题。
设置可信域名时大概会让举行域名归属认证,下载认证文件,放到域名访问根目次下,确保 http://www.oa.com:8099/WW_verify_xxxxxxx.txt,在欣赏器可访问(会体现WW_verify_xxxxxxx.txt内的内容),具体实现方式根据体系的摆设方式决定,可根据体系的摆设方式扣问AI。

 三、Java 流程实现

常量参数:
  1. // 企微企业 ID
  2. String wxworkCorpId = xxx;
  3. // 企微应用 AgentId
  4. String wxworkAgentId = xxx;
  5. // 企微应用 Secret
  6. String wxworkSecret = xxx;
  7. // 登录回调地址
  8. String wxworkRedirectUri = "http://www.oa.com:8099/wxwork/callback";
复制代码
跳转到企微登录页:
  1.     /**
  2.      * 跳转到企业微信扫码登录页面
  3.      */
  4.     @GetMapping("/wxwork/login")
  5.     public void wxworkLogin(HttpServletResponse response) throws Exception {
  6.         String authUrl = "https://open.work.weixin.qq.com/wwopen/sso/qrConnect?"
  7.                 + "appid=" + wxworkCorpId
  8.                 + "&agentid=" + wxworkAgentId
  9.                 + "&redirect_uri=" + URLEncoder.encode(wxworkRedirectUri, "UTF-8")
  10.                 + "&state=meiqu_erp";
  11.         response.sendRedirect(authUrl);
  12.     }
复制代码
企业微信登录回调:
  1.     @GetMapping("/wxwork/callback")
  2.     public String wxworkCallback(@RequestParam("code") String code,
  3.                                   @RequestParam(value = "state", required = false) String state,
  4.                                   HttpServletRequest request) throws Exception {
  5.         // 1. 获取企业微信 access_token
  6.         String accessToken = getWxworkAccessToken();
  7.         // 2. 用 code 换取用户的 UserId
  8.         String userId = getWxworkUserId(accessToken, code);
  9.         if (StringUtils.isBlank(userId)) {
  10.             return "redirect:/login?error=wxwork_auth_failed";
  11.         }
  12.         // 3. 根据企业微信 userId 查找绑定的系统用户
  13.         // 3.1 userId 没有匹配到系统用户,跳转到绑定页面,让用户首次手动输入系统账户密码进行绑定
  14.         // 3.2 userId 匹配到系统用户,跳转登录。
  15.     }   
复制代码
获取企业微信 access_token:
  1.     /**
  2.      * 获取企业微信 access_token
  3.      * 接口:GET https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid=xxx&corpsecret=xxx
  4.      */
  5.     private String getWxworkAccessToken() throws Exception {
  6.         String url = "https://qyapi.weixin.qq.com/cgi-bin/gettoken?"
  7.                 + "corpid=" + wxworkCorpId
  8.                 + "&corpsecret=" + wxworkSecret;
  9.         String result = getJson(url);
  10.         return JSON.parseObject(result).getString("access_token");
  11.     }
复制代码
用 Code 调换用户的 UserId:
  1. /**
  2.      * 用 code 换取企业微信用户的 UserId
  3.      * 接口:GET https://qyapi.weixin.qq.com/cgi-bin/user/getuserinfo?access_token=xxx&code=xxx
  4.      */
  5.     private String getWxworkUserId(String accessToken, String code) throws Exception {
  6.         String url = "https://qyapi.weixin.qq.com/cgi-bin/user/getuserinfo?"
  7.                 + "access_token=" + accessToken
  8.                 + "&code=" + code;
  9.         String result = getJson(url);
  10.         JSONObject json = JSON.parseObject(result);
  11.         // errcode=0 表示成功,UserId 为企业内用户ID
  12.         if (json.getIntValue("errcode") == 0) {
  13.             return json.getString("UserId");
  14.         }
  15.         return null;
  16.     }
复制代码
 

本帖子中包含更多资源

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

×
回复

使用道具 举报

登录后关闭弹窗

登录参与点评抽奖  加入IT实名职场社区
去登录
快速回复 返回顶部 返回列表