@ApiOperation(value="微信公众号分享签名", notes="url")
@PostMapping(value="/getTicket")
public Result<Map<String,String>> getTicket(String url){
String urlss = "https://api.weixin.qq.com/cgi-bin/ticket/getticket";
String urls = "https://api.weixin.qq.com/cgi-bin/token";
String appid = "";
String secret = "";
String accessToken = WxTkenUtil.getAccessToken(urls, "client_credential", appid, secret);
if(accessToken == null){
return Result.error("access_token获取失败");
}
String tokenUrl = urlss +"?access_token="+accessToken+"&type=jsapi";
JSONObject json = RestUtil.get(tokenUrl);
String jsapi_ticket = json.getString("ticket");
//生成随机字符串 noncestr
String nonceStr = UUID.randomUUID().toString();
//时间戳 timestamp,精确到秒
String timestamp = Long.toString(System.currentTimeMillis() / 1000);
HashMap<String,String> map = new HashMap();
String Stitching = "jsapi_ticket="+jsapi_ticket+"&noncestr="+nonceStr+"×tamp="+timestamp+"&url="+url;
System.out.println("Stitching:"+Stitching);
//SHA1 加密
java.security.MessageDigest digest = null;
try {
digest = java.security.MessageDigest.getInstance("SHA-1");
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
}
digest.update(Stitching.getBytes());
byte messageDigest[] = digest.digest();
StringBuffer hexStr = new StringBuffer();
// 字节数组转换为 十六进制 数
for (int i = 0; i < messageDigest.length; i++) {
String shaHex = Integer.toHexString(messageDigest[i] & 0xFF);
if (shaHex.length() < 2) {
hexStr.append(0);
}
hexStr.append(shaHex);
}
map.put("appid", appid);
map.put("timestamp", timestamp);
map.put("nonceStr", nonceStr);
map.put("signature", hexStr.toString());
return Result.OK("请求成功",map);
}
package org.jeecg.modules.util;
import com.alibaba.fastjson.JSONObject;
import org.apache.commons.lang3.StringUtils;
import org.jeecg.common.util.RestUtil;
import org.jeecg.config.sign.util.HttpUtils;
import javax.xml.transform.Result;
public class WxTkenUtil {
public static String getAccessToken(String url,String grantType,String appid,String secret){
String access_token ="";
String tokenUrl = url +"?grant_type="+grantType+"&appid="+appid +"&secret="+ secret;
Object result = RestUtil.get(tokenUrl);
JSONObject jsons = JSONObject.parseObject(result.toString());
String expires_in = jsons.getString("expires_in");
if(StringUtils.isNotEmpty(expires_in) && Integer.parseInt(expires_in) == 7200){
access_token = jsons.getString("access_token");
}
return access_token;
}
}