xiongchao 3 年之前
父节点
当前提交
b9095fbbbf

+ 2 - 1
platform-common/src/main/java/com/platform/common/enums/ResultCode.java

@@ -98,8 +98,9 @@ public enum ResultCode {
     PARTS_NOT_NULL(202054,"复制部件信息不能为空", "copy part info  can not be null"),
 
     /**
-     * 邮箱错误
+     * 微信服务器token验证错误
      */
+    WECHAT_TOKEN_CHECK_ERROR(20201, "微信token验证失败", "token check error"),
     WECHAT_INVALID_REQUEST_ORIGIN(20203, "非法请求预警,请求不是来自微信", "invalid request, only wechat"),
 //    USER_MAIL_EXISTED(20201, "邮箱已存在", "email has existed"),
 //    INVALID_ID_CARD(20202, "无效身份证", "invalid id card"),

+ 48 - 2
platform-common/src/main/java/com/platform/common/util/ShaUtil.java

@@ -1,6 +1,10 @@
 package com.platform.common.util;
 
+import com.platform.common.enums.ResultCode;
+import com.platform.common.exception.BusinessException;
+
 import java.security.MessageDigest;
+import java.util.Arrays;
 
 /**
  * @Description Sha加密
@@ -36,8 +40,50 @@ public class ShaUtil {
             }
             return new String(buf);
         } catch (Exception e) {
-            // TODO: handle exception
-            return null;
+            e.printStackTrace();
+            throw new BusinessException(ResultCode.WECHAT_TOKEN_CHECK_ERROR.getDescription());
         }
     }
+
+    /**
+     * 用SHA1算法验证Token
+     *
+     * @param token     票据
+     * @param timestamp 时间戳
+     * @param nonce     随机字符串
+     * @return 安全签名
+     * @throws BusinessException
+     */
+    public static String getSHA1(String token, String timestamp, String nonce) throws BusinessException {
+        try {
+            String[] array = new String[] { token, timestamp, nonce };
+            StringBuffer sb = new StringBuffer();
+            // 字符串排序
+            Arrays.sort(array);
+            for (int i = 0; i < 3; i++) {
+                sb.append(array[i]);
+            }
+            String str = sb.toString();
+            // SHA1签名生成
+            MessageDigest md = MessageDigest.getInstance("SHA-1");
+            md.update(str.getBytes());
+            byte[] digest = md.digest();
+
+            StringBuffer hexstr = new StringBuffer();
+            String shaHex = "";
+            for (int i = 0; i < digest.length; i++) {
+                shaHex = Integer.toHexString(digest[i] & 0xFF);
+                if (shaHex.length() < 2) {
+                    hexstr.append(0);
+                }
+                hexstr.append(shaHex);
+            }
+            return hexstr.toString();
+        } catch (Exception e) {
+            e.printStackTrace();
+            throw new BusinessException(ResultCode.WECHAT_TOKEN_CHECK_ERROR.getDescription());
+        }
+    }
+
+
 }

+ 3 - 1
platform-service/src/main/java/com/platform/service/wechat/service/WeChatConnectService.java

@@ -82,7 +82,9 @@ public class WeChatConnectService {
      */
     public static boolean isFromWeChat(String signature, String timestamp, String nonce) {
         String sign = ACCESS_TOKEN + timestamp + nonce;
-        String serverSign = ShaUtil.getSha1(sign);
+        String serverSign = ShaUtil.getSHA1(ACCESS_TOKEN, timestamp, nonce);
+        log.info("服务端timestamp:" + timestamp);
+        log.info("服务端nonce:" + nonce);
         log.info("服务端签名:" + serverSign);
         log.info("微信端签名:" + signature);
         // 验证是否来自微信的接口请求,如果不是,要写log