H5分享提示签名错误,是后端还是前端的问题?
在后端返回的:随机字符串,时间戳,和后端生成的签名,还有url;经过http://mp.weixin.qq.com/debug/cgi-bin/sandbox?t=jsapisign这个验证,后端返回的签名与验证工具生成的签名一致.但是前端用到的时候,报
wx-js-sdk config:fail,Error: invalid signature签名无效错误
想问下大佬,是后端还是前端的问题
3 回复
public Map<String, String> SHA1(String url, String jsapiTicket) { Map<String, String> map = new HashMap<>( 6 ); String nonceStr = getRandomStr( 16 );//生成16位随机字符串 String timestamp = createTimestamp(); String string1; String signature = "" ; //签名 //注意这里参数名必须全部小写,且必须有序 string1 = "jsapi_ticket=" + jsapiTicket + "&noncestr=" + nonceStr + "×tamp=" + timestamp + "&url=" + url; try { MessageDigest digest = MessageDigest .getInstance( "SHA-1" ); digest.update(string1.getBytes()); byte messageDigest[] = digest.digest(); StringBuffer hexStr = new StringBuffer(); for ( int i = 0 ; i < messageDigest.length; i++) { String shaHex = Integer.toHexString(messageDigest[i] & 0xFF ); if (shaHex.length() < 2 ) { hexStr.append( 0 ); } hexStr.append(shaHex); } signature = hexStr.toString(); //签名 map.put( "url" , url); map.put( "jsapi_ticket" , jsapiTicket); map.put( "nonceStr" , nonceStr); map.put( "timestamp" , timestamp); map.put( "signature" , signature); return map; } catch (NoSuchAlgorithmException e) { e.printStackTrace(); } return null ; } |
这个是签名生成方法