阅读更多
https://www.cnblogs.com/gygtech/p/9173647.html
@GetMapping("/xxx")
@ResponseBody
public AjaxResult xxx(String url) {
if (StringUtils.isEmpty(url)) {
return AjaxResult.error(601, "连接URL不能为空");
}
Map map = new HashMap();
long timestamp = System.currentTimeMillis() / 1000;
String ticket = getTicket();
String noncestr = randomStr();
map.put("appid", APPID);
map.put("jsapi_ticket", ticket);
map.put("noncestr", noncestr);
map.put("timestamp", timestamp);
String outstring = getSignStr(ticket, noncestr, timestamp, url);;
map.put("outstring", outstring);
map.put("signature", DigestUtils.sha1Hex(outstring));
return AjaxResult.success().put("info", map);
}
/**
* 随机字符串
*
* @return
*/
private String randomStr() {
String[] strs = {
"a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z",
"A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z"
};
Random r = new Random();
StringBuilder sb = new StringBuilder();
int len = strs.length - 1;
for (int i = 0; i < 10; i++) {
sb.append(strs[r.nextInt(len)]);
}
return sb.toString();
}
/**
* 获取签名
*
* @param ticket
* @param noncestr
* @param timestamp
* @param url
* @return
*/
private String getSignStr(String ticket, String noncestr, long timestamp, String url) {
StringBuilder sb = new StringBuilder();
sb.append("jsapi_ticket=").append(ticket).append("&")
.append("noncestr=").append(noncestr).append("&")
.append("timestamp=").append(timestamp).append("&")
.append("url=").append(url.indexOf("#") >= 0 ? url.substring(0, url.indexOf("#")) : url);
return sb.toString();
}