public class ShareController extends BaseController {
private static final String appid = "appid";
private static final String secret = "secret ";
// public static void main(String[] args) {
// String url = "http://omstest.vmall.com:23568/thirdparty/wechat/vcode/gotoshare?quantity=1&batchName=MATE7";//分享的H5的链接路径
// Jssdk jssdk = getSign(url);
// System.out.println(jssdk.getSign()+"FFFF"+jssdk.getNoncestr());
//
// }
private String getJsapiTicket(){
String requestUrl = "https://api.weixin.qq.com/cgi-bin/token?";
String params = "grant_type=client_credential&appid=" + appid + "&secret=" + secret + "";
String result = HttpRequestUtils.httpGet(requestUrl+params);
String access_token = JSONObject.parseObject(result).getString("access_token");
requestUrl = "https://api.weixin.qq.com/cgi-bin/ticket/getticket?";
params = "access_token=" + access_token + "&type=jsapi";
result = HttpRequestUtils.httpGet(requestUrl+params);
String jsapi_ticket = JSONObject.parseObject(result).getString("ticket");
int activeTime=Integer.parseInt(JSONObject.parseObject(result).getString("expires_in"));
// Jssdk jssdk = new Jssdk();
// jssdk.setActiveTime(activeTime-600);
// jssdk.setJsapiTicket(jsapi_ticket);
// jssdkDao.update(jssdk);
return jsapi_ticket;
}
@RequestMapping("/getUrl")
private JsonDataVO getSign(String url) {
String jsapi_ticket = getJsapiTicket();
String noncestr = getNonceStr();
String timestamp = getTimeStamp();
String sign = "jsapi_ticket=" + jsapi_ticket + "&noncestr=" + noncestr + "×tamp=" + timestamp + "&url=" + url;
String signature = "";
try {
MessageDigest crypt = MessageDigest.getInstance("SHA-1");
crypt.reset();
crypt.update(sign.getBytes("UTF-8"));
signature = byteToHex(crypt.digest());
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
Jssdk jssdk = new Jssdk();
jssdk.setAppid(appid);
jssdk.setNoncestr(noncestr);
jssdk.setTimestamp(timestamp);
jssdk.setSign(signature);
jssdk.setJsapiTicket(jsapi_ticket);
log.info("signature的值为:"+signature);
return returnJson(jssdk);
}
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 getNonceStr() {
return UUID.randomUUID().toString().replaceAll("-", "");
}
private String getTimeStamp() {
return String.valueOf(System.currentTimeMillis() / 1000);
}
}
/** * git请求的获得类 * @param url 路径 * @return */
@Slf4j
public class HttpRequestUtils {
/**
* 发送get请求
* @param url 路径
* @return
*/
public static String httpGet(String url){
//get请求返回结果
String strResult = null;
try {
DefaultHttpClient client = new DefaultHttpClient();
//发送get请求
HttpGet request = new HttpGet(url);
HttpResponse response = client.execute(request);
/**请求发送成功,并得到响应**/
if (response.getStatusLine().getStatusCode() == org.apache.http.HttpStatus.SC_OK) {
/**读取服务器返回过来的json字符串数据**/
strResult = EntityUtils.toString(response.getEntity());
} else {
log.error("get请求提交失败:" + url);
}
} catch (IOException e) {
log.error("get请求提交失败:" + url, e);
}
return strResult;
}
}
可参考https://www.cnblogs.com/suoking/p/5341108.html
转自 https://blog.csdn.net/suochunlong/article/details/53032497