diff --git a/hai-service/src/main/java/com/hai/common/utils/WxUtils.java b/hai-service/src/main/java/com/hai/common/utils/WxUtils.java index 4995413a..78a5eddf 100644 --- a/hai-service/src/main/java/com/hai/common/utils/WxUtils.java +++ b/hai-service/src/main/java/com/hai/common/utils/WxUtils.java @@ -543,19 +543,24 @@ public class WxUtils { /** * 用SHA1算法生成安全签名 * @param data 数据 - * @param key key * @return 安全签名 * @throws Exception */ - public static String getSHA1(final Map data, String key) throws Exception + public static String getSHA1(final Map data) throws Exception { 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]); + Set keySet = data.keySet(); + String[] keyArray = keySet.toArray(new String[keySet.size()]); + Arrays.sort(keyArray); + StringBuilder sb = new StringBuilder(); + for (String k : keyArray) { + if (k.equals(WXPayConstants.FIELD_SIGN) || k.equals("apiKey")) { + continue; + } + if (data.get(k) != null) // 参数值为空,则不参与签名 + { + sb.append(k).append("=").append(data.get(k)).append("&"); + } } String str = sb.toString(); // SHA1签名生成 diff --git a/v1/src/main/java/com/v1/controller/WxMsgController.java b/v1/src/main/java/com/v1/controller/WxMsgController.java index bc53bdab..ecb91f91 100644 --- a/v1/src/main/java/com/v1/controller/WxMsgController.java +++ b/v1/src/main/java/com/v1/controller/WxMsgController.java @@ -55,32 +55,32 @@ public class WxMsgController { @Resource private HighMerchantStoreService merchantStoreService; - @RequestMapping(value = "/verifyWxToken", method = RequestMethod.GET) - @ResponseBody - @ApiOperation(value = "验证servlet") - public String verifyWxToken( - @RequestParam(name = "signature", required = false) String signature, - @RequestParam(name = "timestamp", required = false) String timestamp, - @RequestParam(name = "nonce", required = false) String nonce, - @RequestParam(name = "echostr", required = false) String echostr - ) { - - try { - - String signatureStr = WxUtils.getSHA1("RgAWdnR5oEOLBdyEjfr4" , timestamp , nonce); - log.info("signatureStr!!!!!" + signatureStr); - log.info("signature!!!!!" + signature); - if (signature.equals(signatureStr)) { - log.info("验证通过!!!!!"); - return echostr; - } - log.info("验证失败!!!!!"); - return null; - } catch (Exception e) { - return null; - } - - } +// @RequestMapping(value = "/verifyWxToken", method = RequestMethod.GET) +// @ResponseBody +// @ApiOperation(value = "验证servlet") +// public String verifyWxToken( +// @RequestParam(name = "signature", required = false) String signature, +// @RequestParam(name = "timestamp", required = false) String timestamp, +// @RequestParam(name = "nonce", required = false) String nonce, +// @RequestParam(name = "echostr", required = false) String echostr +// ) { +// +// try { +// +// String signatureStr = WxUtils.getSHA1("RgAWdnR5oEOLBdyEjfr4" , timestamp , nonce); +// log.info("signatureStr!!!!!" + signatureStr); +// log.info("signature!!!!!" + signature); +// if (signature.equals(signatureStr)) { +// log.info("验证通过!!!!!"); +// return echostr; +// } +// log.info("验证失败!!!!!"); +// return null; +// } catch (Exception e) { +// return null; +// } +// +// } @RequestMapping(value = "/verifyWxToken", method = RequestMethod.POST) @ResponseBody @ApiOperation(value = "验证servlet") @@ -251,4 +251,32 @@ public class WxMsgController { } + @RequestMapping(value = "/verifyWx", method = RequestMethod.GET) + @ResponseBody + @ApiOperation(value = "微信签名") + public String verifyWx() { + + try { + + Map params = new HashMap<>(); + params.put("appid", SysConst.getSysConfig().getWxH5AppId()); + params.put("secret", SysConst.getSysConfig().getWxH5AppSecret()); + params.put("grant_type", "client_credential"); + + JSONObject accessTokenObject = HttpsUtils.doGet("https://api.weixin.qq.com/cgi-bin/token", params); + + Map map = new HashMap<>(); + map.put("noncestr","Wm3WZYTPz0wzccnW"); + map.put("jsapi_ticket",accessTokenObject.getString("ticket")); + map.put("timestamp","1414587457"); + map.put("url","http://mp.weixin.qq.com?params=value"); + + return WxUtils.getSHA1(map); + } catch (Exception e) { + return null; + } + + } + + }