java调⽤js-sdk接⼝
JSSDK使⽤步骤
步骤⼀:绑定域名
注意:前⾯不需要加http
步骤⼆:引⼊JS⽂件
jquery.js⾃⾏下载到⾃⼰的⼯程引⼊即可。
<script src="/js/jquery.js"></script>
<script src="res.wx.qq/open/js/jweixin-1.0.0.js"></script>
前端传的 url : https%3A%2F%2Foftoo.site%2F%3Fcode%3D071N3ldz0A3Ixd1RIXez0fqXcz0N3ldT%2
6state%3D123 在后的代码要decode
@ApiOperation("获取签名")
@RequestMapping(value = "/getSignature",method = RequestMethod.GET)
public Map<String, Object> getSignature(String url) throws Exception {
url = URLDecoder.decode(url, "UTF-8");
//测试
String ticketKey = CacheConfigManagement.WX_SKD_JSAPITICKET + appId;
String ticket = (ticketKey);
if(StringUtils.isEmpty(ticket)){
//获取access_token
String access_token = getAccessToken(appId , appSecret);
/
/获取jspai_ticket
ticket = getTicket(access_token);
redis.set(ticketKey,ticket,7000);
}else{
ticket = (ticketKey);
}
return getSign(url, ticket);
}
// ⽹页授权接⼝
public String getAccessToken(String appId , String appSecret) throws Exception {
// ⽹页授权接⼝
String GetPageAccessTokenUrl = "api.weixin.qq/cgi-bin/token?grant_type=client_credential&appid="+appId+"&secret="+appSecret;
DefaultHttpClient client = null;
String access_token = null;
int expires_in = 0;
try {
client = new DefaultHttpClient();
HttpGet httpget = new HttpGet(GetPageAccessTokenUrl);
ResponseHandler<String> responseHandler = new BasicResponseHandler();
String response = ute(httpget, responseHandler);
JSONObject OpenidJSONO = JSONObject.parseObject(response);
access_token = String.("access_token"));//获取access_token
expires_in = Integer.parseInt(String.("expires_in")));//获取时间
} catch (Exception e) {
throw new Exception("获取AccessToken出错!");
js获取json的key和value} finally {
}
return access_token;
return access_token;
}
public String getTicket(String accessToken) throws Exception {
// ⽹页授权接⼝
String GetPageAccessTokenUrl = "api.weixin.qq/cgi-bin/ticket/getticket?access_token="+accessToken+"&type=jsapi"; DefaultHttpClient client = null;
String ticket = "";
int expires_in = 0;
try {
client = new DefaultHttpClient();
HttpGet httpget = new HttpGet(GetPageAccessTokenUrl);
ResponseHandler<String> responseHandler = new BasicResponseHandler();
String response = ute(httpget, responseHandler);
JSONObject OpenidJSONO = JSONObject.parseObject(response);
ticket = String.("ticket"));//获取ticket
expires_in = Integer.parseInt(String.("expires_in")));//获取时间
} catch (Exception e) {
throw new Exception("获取Ticket出错!");
} finally {
}
return ticket;
}
public String SHA1(String str) {
try {
MessageDigest digest = java.security.MessageDigest
.getInstance("SHA-1"); //如果是SHA加密只需要将"SHA-1"改成"SHA"即可
digest.Bytes());
byte messageDigest[] = digest.digest();
// Create Hex String
StringBuffer hexStr = new StringBuffer();
// 字节数组转换为⼗六进制数
for (int i = 0; i < messageDigest.length; i++) {
String shaHex = HexString(messageDigest[i] & 0xFF);
if (shaHex.length() < 2) {
hexStr.append(0);
}
hexStr.append(shaHex);
}
String();
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
private Map<String,Object> getSign(String url,String jsapi_ticket) {
Map<String,Object> map = new HashMap<>();
//获取noncestr
String noncestr = UUID.randomUUID().toString().replace("-", "");
String timestamp = String.valueOf(System.currentTimeMillis() / 1000);
//将四个数据进⾏组合,传给SHA1进⾏加密
String str = "jsapi_ticket=" + jsapi_ticket +
"&noncestr=" + noncestr +
"×tamp=" + timestamp +
"&url=" + url;
String signature = SHA1(str);
map.put("timestamp",timestamp);
map.put("noncestr",noncestr);
map.put("signature",signature);
map.put("appid",appId);
map.put("url",url);
map.put("url",url);
return map;
}
根据JSAPITICKET⽣成的字符串。与返回的数据在签名校验⼯具⽹址进⾏对⽐加密后的signature是否⼀致
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论