|
@@ -26,22 +26,23 @@ public class WeComController {
|
|
RedisTemplate<String, Object> redisTemplate;
|
|
RedisTemplate<String, Object> redisTemplate;
|
|
|
|
|
|
@GetMapping("/verify")
|
|
@GetMapping("/verify")
|
|
- public void verifyGet(HttpServletRequest request, HttpServletResponse response) throws AesException {
|
|
|
|
- // 微信加密签名
|
|
|
|
- String msgSignature = request.getParameter("msg_signature");
|
|
|
|
- // 时间戳
|
|
|
|
- String timestamp = request.getParameter("timestamp");
|
|
|
|
- // 随机数
|
|
|
|
- String nonce = request.getParameter("nonce");
|
|
|
|
- // 随机字符串
|
|
|
|
- // 如果是刷新,需返回原echostr
|
|
|
|
- String echoStr = request.getParameter("echostr");
|
|
|
|
- // 微信加密签名
|
|
|
|
- WXBizMsgCrypt wxcpt = new WXBizMsgCrypt(WeComServerConstant.TOKEN, WeComServerConstant.EncodingAESKey, WeComServerConstant.CorpID);
|
|
|
|
-
|
|
|
|
- String sEchoStr = ""; //需要返回的明文
|
|
|
|
- PrintWriter out;
|
|
|
|
|
|
+ public void verifyGet(HttpServletRequest request, HttpServletResponse response) {
|
|
try {
|
|
try {
|
|
|
|
+ // 微信加密签名
|
|
|
|
+ String msgSignature = request.getParameter("msg_signature");
|
|
|
|
+ // 时间戳
|
|
|
|
+ String timestamp = request.getParameter("timestamp");
|
|
|
|
+ // 随机数
|
|
|
|
+ String nonce = request.getParameter("nonce");
|
|
|
|
+ // 随机字符串
|
|
|
|
+ // 如果是刷新,需返回原echostr
|
|
|
|
+ String echoStr = request.getParameter("echostr");
|
|
|
|
+ // 微信加密签名
|
|
|
|
+ WXBizMsgCrypt wxcpt = new WXBizMsgCrypt(WeComServerConstant.TOKEN, WeComServerConstant.EncodingAESKey, WeComServerConstant.CorpID);
|
|
|
|
+
|
|
|
|
+ String sEchoStr = ""; //需要返回的明文
|
|
|
|
+ PrintWriter out;
|
|
|
|
+
|
|
sEchoStr = wxcpt.VerifyURL(msgSignature, timestamp,
|
|
sEchoStr = wxcpt.VerifyURL(msgSignature, timestamp,
|
|
nonce, echoStr);
|
|
nonce, echoStr);
|
|
log.info("verifyurl echostr: " + sEchoStr);
|
|
log.info("verifyurl echostr: " + sEchoStr);
|
|
@@ -60,51 +61,55 @@ public class WeComController {
|
|
* 刷新 ticket
|
|
* 刷新 ticket
|
|
*/
|
|
*/
|
|
@PostMapping(value = "/verify")
|
|
@PostMapping(value = "/verify")
|
|
- public String verifyPost(HttpServletRequest request) throws Exception {
|
|
|
|
-
|
|
|
|
- // 微信加密签名
|
|
|
|
- String msg_signature = request.getParameter("msg_signature");
|
|
|
|
- // 时间戳
|
|
|
|
- String timestamp = request.getParameter("timestamp");
|
|
|
|
- // 随机数
|
|
|
|
- String nonce = request.getParameter("nonce");
|
|
|
|
-
|
|
|
|
- String type = request.getParameter("type");
|
|
|
|
-
|
|
|
|
- String corpId = request.getParameter("corpid");
|
|
|
|
-
|
|
|
|
- String id = "";
|
|
|
|
-
|
|
|
|
- // 访问应用和企业回调传不同的ID
|
|
|
|
- if (type.equals("data")) {
|
|
|
|
- id = corpId;
|
|
|
|
- } else {
|
|
|
|
- id = WeComServerConstant.SuiteID;
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
|
|
+ public String verifyPost(HttpServletRequest request) {
|
|
|
|
|
|
- WXBizMsgCrypt wxcpt = new WXBizMsgCrypt(WeComServerConstant.TOKEN, WeComServerConstant.EncodingAESKey, id);
|
|
|
|
-
|
|
|
|
- String postData = ""; // 密文,对应POST请求的数据
|
|
|
|
- //1.获取加密的请求消息:使用输入流获得加密请求消息postData
|
|
|
|
- ServletInputStream in = request.getInputStream();
|
|
|
|
- BufferedReader reader = new BufferedReader(new InputStreamReader(in));
|
|
|
|
-
|
|
|
|
- String tempStr = ""; //作为输出字符串的临时串,用于判断是否读取完毕
|
|
|
|
- while (null != (tempStr = reader.readLine())) {
|
|
|
|
- postData += tempStr;
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- String suiteXml = wxcpt.DecryptMsg(msg_signature, timestamp, nonce, postData);
|
|
|
|
- log.info("suiteXml: " + suiteXml);
|
|
|
|
-
|
|
|
|
- Map suiteMap = WxUtil.transferXmlToMap(suiteXml);
|
|
|
|
- if (suiteMap.get("SuiteTicket") != null) {
|
|
|
|
- String suiteTicket = (String) suiteMap.get("SuiteTicket");
|
|
|
|
- redisTemplate.opsForValue().set("suiteTicket", suiteTicket);
|
|
|
|
- } else if (suiteMap.get("AuthCode") != null) {
|
|
|
|
- String authCode = (String) suiteMap.get("AuthCode");
|
|
|
|
- redisTemplate.opsForValue().set("authCode", authCode);
|
|
|
|
|
|
+ try {
|
|
|
|
+ // 微信加密签名
|
|
|
|
+ String msg_signature = request.getParameter("msg_signature");
|
|
|
|
+ // 时间戳
|
|
|
|
+ String timestamp = request.getParameter("timestamp");
|
|
|
|
+ // 随机数
|
|
|
|
+ String nonce = request.getParameter("nonce");
|
|
|
|
+
|
|
|
|
+ String type = request.getParameter("type");
|
|
|
|
+
|
|
|
|
+ String corpId = request.getParameter("corpid");
|
|
|
|
+
|
|
|
|
+ String id = "";
|
|
|
|
+
|
|
|
|
+ // 访问应用和企业回调传不同的ID
|
|
|
|
+ if (type.equals("data")) {
|
|
|
|
+ id = corpId;
|
|
|
|
+ } else {
|
|
|
|
+ id = WeComServerConstant.SuiteID;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+ WXBizMsgCrypt wxcpt = new WXBizMsgCrypt(WeComServerConstant.TOKEN, WeComServerConstant.EncodingAESKey, id);
|
|
|
|
+
|
|
|
|
+ String postData = ""; // 密文,对应POST请求的数据
|
|
|
|
+ //1.获取加密的请求消息:使用输入流获得加密请求消息postData
|
|
|
|
+ ServletInputStream in = request.getInputStream();
|
|
|
|
+ BufferedReader reader = new BufferedReader(new InputStreamReader(in));
|
|
|
|
+
|
|
|
|
+ String tempStr = ""; //作为输出字符串的临时串,用于判断是否读取完毕
|
|
|
|
+ while (null != (tempStr = reader.readLine())) {
|
|
|
|
+ postData += tempStr;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ String suiteXml = wxcpt.DecryptMsg(msg_signature, timestamp, nonce, postData);
|
|
|
|
+ log.info("suiteXml: " + suiteXml);
|
|
|
|
+
|
|
|
|
+ Map suiteMap = WxUtil.transferXmlToMap(suiteXml);
|
|
|
|
+ if (suiteMap.get("SuiteTicket") != null) {
|
|
|
|
+ String suiteTicket = (String) suiteMap.get("SuiteTicket");
|
|
|
|
+ redisTemplate.opsForValue().set("suiteTicket", suiteTicket);
|
|
|
|
+ } else if (suiteMap.get("AuthCode") != null) {
|
|
|
|
+ String authCode = (String) suiteMap.get("AuthCode");
|
|
|
|
+ redisTemplate.opsForValue().set("authCode", authCode);
|
|
|
|
+ }
|
|
|
|
+ } catch (Exception e) {
|
|
|
|
+ log.error("verifyPost error", e);
|
|
}
|
|
}
|
|
return "success";
|
|
return "success";
|
|
}
|
|
}
|