nutzwx-1.62.jar
包名
package org.nutz.weixin.impl;
以下jssdk生成config的方法
public NutMap genJsSDKConfig(String url, String... jsApiList) {
String jt = this.getJsapiTicket();
long timestamp = System.currentTimeMillis();
String nonceStr = R.UU64();
String str = String.format("jsapi_ticket=%s&noncestr=%s×tamp=%d&url=%s", jt, nonceStr, timestamp, url);
String signature = Lang.sha1(str);
NutMap map = new NutMap();
map.put("appId", appid);
map.put("timestamp", timestamp);
map.put("nonceStr", nonceStr);
map.put("signature", signature);
map.put("jsApiList", jsApiList);
return map;
}
这里timestamp是毫秒级别,这样生成的signature 在页面调用相关接口,会报config:signature invalid
找到jssdk的官方demo其中使用的时间戳是秒,以下是demo
private static String create_timestamp() {
return Long.toString(System.currentTimeMillis() / 1000);
}
public static Map<String, String> sign(String jsapi_ticket, String url) {
Map<String, String> ret = new HashMap<String, String>();
String nonce_str = create_nonce_str();
String timestamp = create_timestamp();
String string1;
String signature = "";
//注意这里参数名必须全部小写,且必须有序
string1 = "jsapi_ticket=" + jsapi_ticket +
"&noncestr=" + nonce_str +
"×tamp=" + timestamp +
"&url=" + url;
System.out.println(string1);
try
{
MessageDigest crypt = MessageDigest.getInstance("SHA-1");
crypt.reset();
crypt.update(string1.getBytes("UTF-8"));
signature = byteToHex(crypt.digest());
}
catch (NoSuchAlgorithmException e)
{
e.printStackTrace();
}
catch (UnsupportedEncodingException e)
{
e.printStackTrace();
}
ret.put("url", url);
ret.put("jsapi_ticket", jsapi_ticket);
ret.put("nonceStr", nonce_str);
ret.put("timestamp", timestamp);
ret.put("signature", signature);
return ret;
}