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