- JSSDK调用,怎么一直提示invalid signature?
调用微信JSSDK一直提示无效签名,用微信提供的工具检查了签名,都是比对得上的,也按照官方文档对url进行截取掉#,前后台的appid也是一样的,还是出现invalid signature,然后同样的前后台代码,用我同事的电脑调用是可以调微信jssdk的摄像头接口的,防火墙也是关闭了,都不行,这是什么问题呢? [图片] [图片] 下面是调用的代码 $(function(){ $(".butt").bind("click",function(){ console.log((location.href.split('#')[0])) var signUrl = encodeURIComponent(window.location.href.split('#')[0]); $.ajax({ url: "http://hzf.free.idcfengye.com/ncgascip-app/weixinsdk/config", method: "post", data: { url: signUrl }, success: function (data) { console.log(data); console.log("wx.config() ---> 接收后台返回的参数"); wx.config({ debug: true, appId: "wx7728a7e3070b5acd", timestamp: data.data.timestamp, nonceStr: data.data.nonceStr, signature: data.data.signature, jsApiList: [ 'chooseImage', 'previewImage', 'uploadImage', 'downloadImage', 'chooseWXPay', 'getLocalImgData' ] }); wx.error(function (res) { console.log(res); }); wx.ready(function(){ wx.chooseImage({ count: 1, // 默认9 sizeType: ['compressed'], // 可以指定是原图还是压缩图,默认二者都有 sourceType: ['camera'], // 可以指定来源是相册还是相机,默认二者都有 success: function (res) { $("#photolist").html(""); //每次选择图片完成后都清空之前已经添加的html节点 let rows = ""; //声明一个空字符串用于保存循环出来的html // var localIds = res.localIds; // 返回选定照片的本地ID列表,localId可以作为img标签的src属性显示图片 images.localId = images.localId.concat(res.localIds); if (window.__wxjs_is_wkwebview) { //判断ios是不是用的 wkwebview 内核 for (var i = 0; i < images.localId.length; i++) { wx.getLocalImgData({ //循环调用 getLocalImgData localId: res.localIds[i], // 图片的localID success: function(res) { var localData = res.localData; // localData是图片的base64数据,可以用img标签显示 localData = localData.replace('jgp', 'jpeg'); //iOS 系统里面得到的数据,类型为 image/jgp,因此需要替换一下 ioslocId.push(localData) //把base64格式的图片添加到ioslocId数组里 这样该数组里的元素都是base64格式的 rows = ""; for (var j = 0; j < ioslocId.length; j++) { rows += `<div class = "z_file" style = "background-image: url(` + ioslocId[j] + `);height:100px;"> <div class = "delete"data - id = "` + j + `" > </div></div> `; } $("#photolist").html(rows); }, fail: function(res) { alert("res"); } }); } alert(rows) } else { //如果不是用的wkwebview 内核 或者是用的安卓系统 执行下面的循环 $.each(images.localId, function(index, el) { rows += `<div class = "z_file" style = "background-image: url(` + el + `); height:100px;" > <div class = "delete" data-id= "` + index + `"> </div></div>`; } ); $("#photolist").html(rows); } } }); }) } }); }); });后台代码 package com.ucap.ncgascip.app.controller.weixin; import com.ucap.ncgascip.app.util.WeiXinUtil; import com.ucap.ncgascip.base.data.RestResultBuilder; import com.ucap.ncgascip.base.data.ResultModel; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.RestController; import java.io.UnsupportedEncodingException; import java.net.URLDecoder; import java.security.MessageDigest; import java.security.NoSuchAlgorithmException; import java.util.Formatter; import java.util.HashMap; import java.util.Map; import java.util.UUID; @RestController @RequestMapping("/weixinsdk") public class WeixinJsSdkController { private static final String jsapi_ticket = ""; @RequestMapping(value = "/config") public ResultModel getConfig(@RequestParam(name = "url") String url) { System.out.println(url); Map<String, String> ret = new HashMap<>(); String jsapi_ticket = WeiXinUtil.getJsapiTicket(); System.out.println(URLDecoder.decode(url)); ret = sign(jsapi_ticket,URLDecoder.decode(url)); return RestResultBuilder.builder().data(ret).build(); } public 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; } private String byteToHex(final byte[] hash) { Formatter formatter = new Formatter(); for (byte b : hash) { formatter.format("%02x", b); } String result = formatter.toString(); formatter.close(); return result; } private String create_nonce_str() { return UUID.randomUUID().toString(); } private String create_timestamp() { return Long.toString(System.currentTimeMillis() / 1000); } } public synchronized static String getJsapiTicket() { //将用户信息写入redis String jsApiTicket = null; String key = WX_JSAPITICKET; // 获取redis的微信token Object jsApiTicketObject = CacheUtil.getCache(CacheCodeEnum.APPWEB.getValue()).get(key); if (jsApiTicketObject != null) { jsApiTicket = String.valueOf(jsApiTicketObject); } else { try { Ticket ticket = getJsSdkComponent().getJsApiTicket(); if (ticket != null) { jsApiTicket = ticket.getTicket(); long overTime = ticket.getExpires_in() / 60; CacheUtil.getCache(CacheCodeEnum.APPWEB.getValue()).add(key, jsApiTicket, overTime); logger.info("catch的jsApiTicket值" + jsApiTicket); } } catch (Exception e) { e.printStackTrace(); } } return jsApiTicket; }
2019-08-16 - 微信测试公众号开发JSSDK接口出现invalid signature?
调用微信JSSDK一直提示无效签名,用微信提供的工具检查了签名,跟我后台生成的是一样的,也按照官方文档对url进行截取掉#,前后台的appid也是一样的,还是出现invalid signature [图片] [图片] 下面是调用的代码 $(function(){ $(".butt").bind("click",function(){ console.log((location.href.split('#')[0])) var signUrl = encodeURIComponent(window.location.href.split('#')[0]); $.ajax({ url: "http://hzf.free.idcfengye.com/ncgascip-app/weixinsdk/config", method: "post", data: { url: signUrl }, success: function (data) { console.log(data); console.log("wx.config() ---> 接收后台返回的参数"); wx.config({ debug: true, appId: "wx7728a7e3070b5acd", timestamp: data.data.timestamp, nonceStr: data.data.nonceStr, signature: data.data.signature, jsApiList: [ 'chooseImage', 'previewImage', 'uploadImage', 'downloadImage', 'chooseWXPay', 'getLocalImgData' ] }); wx.error(function (res) { console.log(res); }); wx.ready(function(){ wx.chooseImage({ count: 1, // 默认9 sizeType: ['compressed'], // 可以指定是原图还是压缩图,默认二者都有 sourceType: ['camera'], // 可以指定来源是相册还是相机,默认二者都有 success: function (res) { $("#photolist").html(""); //每次选择图片完成后都清空之前已经添加的html节点 let rows = ""; //声明一个空字符串用于保存循环出来的html // var localIds = res.localIds; // 返回选定照片的本地ID列表,localId可以作为img标签的src属性显示图片 images.localId = images.localId.concat(res.localIds); if (window.__wxjs_is_wkwebview) { //判断ios是不是用的 wkwebview 内核 for (var i = 0; i < images.localId.length; i++) { wx.getLocalImgData({ //循环调用 getLocalImgData localId: res.localIds[i], // 图片的localID success: function(res) { var localData = res.localData; // localData是图片的base64数据,可以用img标签显示 localData = localData.replace('jgp', 'jpeg'); //iOS 系统里面得到的数据,类型为 image/jgp,因此需要替换一下 ioslocId.push(localData) //把base64格式的图片添加到ioslocId数组里 这样该数组里的元素都是base64格式的 rows = ""; for (var j = 0; j < ioslocId.length; j++) { rows += `<div class = "z_file" style = "background-image: url(` + ioslocId[j] + `);height:100px;"> <div class = "delete"data - id = "` + j + `" > </div></div> `; } $("#photolist").html(rows); }, fail: function(res) { alert("res"); } }); } alert(rows) } else { //如果不是用的wkwebview 内核 或者是用的安卓系统 执行下面的循环 $.each(images.localId, function(index, el) { rows += `<div class = "z_file" style = "background-image: url(` + el + `); height:100px;" > <div class = "delete" data-id= "` + index + `"> </div></div>`; } ); $("#photolist").html(rows); } } }); }) } }); }); });
2019-08-14