一、场景
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 {
- 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;
- }
复制代码
|