微信的jssdk的config接口注入权限验证配置--signature的生成


生成signature有一下几步:
1、通过 appid + appsecert 获取公众号的 access_token
2、根据1的access_token来获取jsapi_token
3、随便弄一个字符串(长度不太清楚,16位及以内应该都可以)作为nonceStr、
4、生成当前的时间戳(timestamp)
5、使用jssdk的网页url
6、将这几个参数按字典序排列,使用SHA-1生成signnature,在wx.config中配置即可


具体如下:
1.访问 https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=APPID&secret=APPSECRET ,拿到返回值,其中有access-token
其中 grant_type不用改,将 appid和secret修改成自己的就可以了。
访问网络路径,那返回参数,我使用的工具类如下【自行修改一下】:
    /**
     * 向指定url,发送get方式的请求
     */
    public static String sendGet(String url, String param){
        String result = "";
        BufferedReader in = null;
        try {
            String urlNameString = url + "?" + param;
            URL realUrl = new URL(urlNameString);
            //打开和url之间的连接
            URLConnection connection = realUrl.openConnection();
            //设置通用参数
            connection.setRequestProperty("accept", "*/*");
            connection.setRequestProperty("connection", "Keep-Alive");
            connection.setRequestProperty("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)");
            //建立实际连接
            connection.connect();
            //获取所有响应头字段
            Map<String, List<String>> map = connection.getHeaderFields();
            for(String key : map.keySet()){
                System.out.println(key+"----"+map.get(key));
            }
            
            // 定义 BufferedReader输入流来读取URL的响应
            in = new BufferedReader(new InputStreamReader(connection.getInputStream()));
            
            String line;
            while((line = in.readLine()) != null){
                result += line;
            }
            
        } catch (Exception e) {
            System.out.println("发送GET请求出现异常");
            e.printStackTrace();
        } finally{
            try {
                if(in != null){
                    in.close();
                }
            } catch (Exception e2) {
                e2.printStackTrace();
            }
        }
        return result;
    }

2.使用access-token获取ticket   

-将acess_token替换即可,使用上面的方法获取返回值 ticket

3.生成signature

public String getSignature(String jsapiTicket,String rand,String timestamp,String url){
//字符串的变量顺序不能变,然后rand是随机字符串,timestamp=System.currentTimeMillis()/1000
String string1 = "jsapi_ticket=" + jsapiTicket +
"&noncestr=" + rand +
"&timestamp=" + timestamp +
"&url=" + url;
System.out.println(string1);
return encrypt(string1);
}
/**
* 加密
* @param strSrc
* @return
*/
private String encrypt(String strSrc) {
MessageDigest md = null;
String strDes = null;
byte[] bt = strSrc.getBytes();
try {
//使用SHA-1的方式进行加密
md = MessageDigest.getInstance("SHA-1"); //获取加密方式
md.update(bt);//加密
strDes = bytes2Hex(md.digest()); //返回16进制字符串
//加密过程/
} catch (NoSuchAlgorithmException e) {
System.out.println("Invalid algorithm.");
return null;
}
return strDes;
}
/**
* 对加密后的字节数组进行转换
* @param bts
* @return
*/
public String bytes2Hex(byte[] bts) {
String des = "";
String tmp = null;
for (int i = 0; i < bts.length; i++) {
tmp = (Integer.toHexString(bts[i] & 0xFF));
if (tmp.length() == 1) {
des += "0";
}
des += tmp;
}
return des;
}
4.将对应的值填入即可

wx.config({

debug: true, // 开启调试模式,调用的所有api的返回值会在客户端alert出来,若要查看传入的参数,可以在pc端打开,参数信息会通过log打出,仅在pc端时才会打印。 appId: '', // 必填,公众号的唯一标识

timestamp: , // 必填,生成签名的时间戳

nonceStr: '', // 必填,生成签名的随机串

signature: '',// 必填,签名,见附录1

jsApiList: [] // 必填,需要使用的JS接口列表,所有JS接口列表见附录2});

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值