news 2025/12/30 9:32:56

企业微信扫码登录案例-springboot版

作者头像

张小明

前端开发工程师

1.2k 24
文章封面图
企业微信扫码登录案例-springboot版

企业微信扫码登录案例-springboot版

思路:创建应用,拿到copid、agenid、密钥、再企业微信授权登录配置回调地址即可;(扫码后将结果回调你本地)

效果如下:

一、maven坐标

<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-thymeleaf</artifactId> </dependency> <dependency> <groupId>com.alibaba.fastjson2</groupId> <artifactId>fastjson2</artifactId> <version>2.0.40</version> </dependency> <dependency> <groupId>cn.hutool</groupId> <artifactId>hutool-all</artifactId> <version>5.8.21</version> </dependency> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <optional>true</optional> </dependency>

二、config、controller

yml配置:

server: port: 8080 wecom: corp-id: xxxxx agent-id: xxxx secret: xxxx-AOfBJ66c # 必须与你在企微后台配置的可信域名一致,且要在该域名下 redirect-uri: http://xxx.cn:28088/api/wecom/callback
import com.alibaba.fastjson2.JSONObject; import cn.hutool.http.HttpUtil; import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.ResponseBody; import java.io.UnsupportedEncodingException; import java.net.URLEncoder; import java.nio.charset.StandardCharsets; @Controller public class WeComLoginController { @Value("${wecom.corp-id}") private String corpId; @Value("${wecom.agent-id}") private String agentId; @Value("${wecom.secret}") private String secret; @Value("${wecom.redirect-uri}") private String redirectUri; /** * 1. 访问登录页 * URL: http://yaoqinqin.cn:28088/login */ @GetMapping("/login") public String loginPage(Model model) throws UnsupportedEncodingException { // 将配置传给前端,生成二维码使用 model.addAttribute("corpId", corpId); model.addAttribute("agentId", agentId); // 回调地址必须 URL Encode model.addAttribute("redirectUri", URLEncoder.encode(redirectUri, "UTF-8")); return "login.html"; // 对应 resources/templates/login.html } /** * 2. 扫码后的回调接口 * URL: http://yaoqinqin.cn:28088/api/wecom/callback */ @GetMapping("/api/wecom/callback") @ResponseBody public String callback(@RequestParam("code") String code) { // 第一步:获取 AccessToken // 注意:生产环境 AccessToken 必须缓存(有效期2小时),不能每次请求都去获取,会触发频率限制 String tokenUrl = String.format("https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid=%s&corpsecret=%s", corpId, secret); String tokenResp = HttpUtil.get(tokenUrl); JSONObject tokenJson = JSONObject.parseObject(tokenResp); if (tokenJson.getIntValue("errcode") != 0) { return "获取 Token 失败: " + tokenResp; } String accessToken = tokenJson.getString("access_token"); // 第二步:获取访问用户身份 (UserId) String userInfoUrl = String.format("https://qyapi.weixin.qq.com/cgi-bin/user/getuserinfo?access_token=%s&code=%s", accessToken, code); String userInfoResp = HttpUtil.get(userInfoUrl); JSONObject userInfoJson = JSONObject.parseObject(userInfoResp); if (userInfoJson.getIntValue("errcode") != 0) { return "获取用户信息失败: " + userInfoResp; } String userId = userInfoJson.getString("UserId"); return "<h1>登录成功!</h1> <p>企业微信 UserId: " + userId + "</p>"; } }

三、thmeleaf页面

<!DOCTYPE html> <html xmlns:th="http://www.thymeleaf.org"> <head> <meta charset="UTF-8"> <title>企业微信扫码登录</title> <script src="https://rescdn.qqmail.com/node/ww/wwopenmng/js/sso/wwLogin-1.0.0.js"></script> <style> body { text-align: center; padding-top: 50px; } #wx_reg { margin: 0 auto; } </style> </head> <body> <h2>欢迎使用扫码登录</h2> <div id="wx_reg"></div> <script th:inline="javascript"> window.onload = function() { // 获取后端传来的参数 var corpId = [[${corpId}]]; var agentId = [[${agentId}]]; var redirectUri = [[${redirectUri}]]; // 已经是 encode 过的 // 实例化二维码 window.WwLogin({ "id": "wx_reg", "appid": corpId, "agentid": agentId, "redirect_uri": decodeURIComponent(redirectUri), // JS SDK内部可能也会处理,通常传入原样字符串或decode一下保险 "state": "STATE_LOGIN", "href": "", }); }; </script> </body> </html>
版权声明: 本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若内容造成侵权/违法违规/事实不符,请联系邮箱:809451989@qq.com进行投诉反馈,一经查实,立即删除!
网站建设 2025/12/28 11:54:55

【Java毕设全套源码+文档】基于springboot的某炼油厂盲板管理系统设计与实现(丰富项目+远程调试+讲解+定制)

博主介绍&#xff1a;✌️码农一枚 &#xff0c;专注于大学生项目实战开发、讲解和毕业&#x1f6a2;文撰写修改等。全栈领域优质创作者&#xff0c;博客之星、掘金/华为云/阿里云/InfoQ等平台优质作者、专注于Java、小程序技术领域和毕业项目实战 ✌️技术范围&#xff1a;&am…

作者头像 李华
网站建设 2025/12/28 11:50:23

YOLO模型镜像支持NVIDIA Triton推理服务器集成

YOLO模型镜像支持NVIDIA Triton推理服务器集成 在智能制造工厂的视觉质检线上&#xff0c;一台GPU服务器正同时处理来自20个高清摄像头的实时视频流——每秒上百帧图像被精准识别出微米级划痕与装配偏差&#xff0c;而系统平均响应延迟却不到30毫秒。这种看似矛盾的高性能表现&…

作者头像 李华
网站建设 2025/12/28 11:49:31

YOLO目标检测镜像已通过三级等保测评

YOLO目标检测镜像通过三级等保测评的技术实践与工程启示 在智能制造、智慧安防和自动驾驶快速发展的今天&#xff0c;AI模型早已不再是实验室里的“技术玩具”&#xff0c;而是需要真正扛起生产责任的工业级组件。一个模型能否上线&#xff0c;不再只看它的mAP或FPS&#xff0…

作者头像 李华
网站建设 2025/12/28 11:44:24

ffmpeg命令之查看音频参数

您可以使用PowerShell或直接使用ffprobe命令。以下是几种方法: 方法1:使用PowerShell(推荐) ffprobe -show_streams 1.mp4 | Select-String "codec_name|sample_rate|channels|profile"方法2:使用findstr(Windows命令行) ffprobe -show_streams 1.mp4 | fi…

作者头像 李华
网站建设 2025/12/28 11:43:20

ArduPilot入门配置核心要点解析

ArduPilot入门配置&#xff1a;从零到飞的实战指南 你是不是也曾在深夜对着一块Pixhawk发呆&#xff0c;看着Mission Planner里密密麻麻的参数无从下手&#xff1f; 刷完固件、接上USB&#xff0c;却发现“未连接”三个字像幽灵一样挥之不去&#xff1b; 遥控器推满油门&…

作者头像 李华