jsapi_ticket是公众号用于调用微信JS接口的临时票据。正常状况下,jsapi_ticket的有效期为7200秒,经过access_token来获取。因为获取jsapi_ticket的api调用次数很是有限,频繁刷新jsapi_ticket会致使api调用受限,影响自身业务,开发者必须在本身的服务全局缓存jsapi_ticket 。html
微信JS接口: 受权登陆, 分享网页等java
因为jsapi_ticket有效期为2小时,而且天天有实时调用量上限次数,因此最好开发的时候最好把jsapi_ticket存储起来,判断上次获取的jsapi_ticket是否有效,若是有效,就须要再次发起请求去获取jsapi_ticket了。 web
三, 获取spring
安全中心 > 白名单 (填写服务器IP地址)json
package org.xxx.xxx.wx.util; import lombok.extern.slf4j.Slf4j; import com.alibaba.fastjson.JSONObject; import java.io.InputStream; import java.net.HttpURLConnection; import java.net.URL; import java.security.MessageDigest; import java.security.NoSuchAlgorithmException; /** * @Description: * @Auther: wuxw * @Date: 2019/11/20 17:38 */ @Slf4j public class WxSignUtil2 { public static String getAccessToken() { String access_token = ""; String grant_type = "client_credential";//获取access_token填写client_credential String AppId="APPID";//第三方用户惟一凭证 String secret="秘钥";//第三方用户惟一凭证密钥,即appsecret //这个url连接地址和参数皆不能变 String url = "https://api.weixin.qq.com/cgi-bin/token?grant_type="+grant_type+"&appid="+AppId+"&secret="+secret; try { URL urlGet = new URL(url); HttpURLConnection http = (HttpURLConnection) urlGet.openConnection(); http.setRequestMethod("GET"); // 必须是get方式请求 http.setRequestProperty("Content-Type","application/x-www-form-urlencoded"); http.setDoOutput(true); http.setDoInput(true); System.setProperty("sun.net.client.defaultConnectTimeout", "30000");// 链接超时30秒 System.setProperty("sun.net.client.defaultReadTimeout", "30000"); // 读取超时30秒 http.connect(); InputStream is = http.getInputStream(); int size = is.available(); byte[] jsonBytes = new byte[size]; is.read(jsonBytes); is.close(); String message = new String(jsonBytes, "UTF-8"); log.info("受权返回消息结构体: "+message); JSONObject demoJson = JSONObject.parseObject(message); System.out.println("受权返回消息结构体: JSON字符串:"+demoJson); access_token = demoJson.getString("access_token"); } catch (Exception e) { e.printStackTrace(); } return access_token; } public static String getTicket(String access_token) { String ticket = null; String url = "https://api.weixin.qq.com/cgi-bin/ticket/getticket?access_token="+ access_token +"&type=jsapi";//这个url连接和参数不能变 try { URL urlGet = new URL(url); HttpURLConnection http = (HttpURLConnection) urlGet.openConnection(); http.setRequestMethod("GET"); // 必须是get方式请求 http.setRequestProperty("Content-Type","application/x-www-form-urlencoded"); http.setDoOutput(true); http.setDoInput(true); System.setProperty("sun.net.client.defaultConnectTimeout", "30000");// 链接超时30秒 System.setProperty("sun.net.client.defaultReadTimeout", "30000"); // 读取超时30秒 http.connect(); InputStream is = http.getInputStream(); int size = is.available(); byte[] jsonBytes = new byte[size]; is.read(jsonBytes); String message = new String(jsonBytes, "UTF-8"); JSONObject demoJson = JSONObject.parseObject(message); System.out.println("JSON字符串:"+demoJson); ticket = demoJson.getString("ticket"); is.close(); } catch (Exception e) { e.printStackTrace(); } return ticket; } public static String SHA1(String decript) { try { MessageDigest digest = java.security.MessageDigest.getInstance("SHA-1"); digest.update(decript.getBytes()); byte messageDigest[] = digest.digest(); // Create Hex String StringBuffer hexString = new StringBuffer(); // 字节数组转换为 十六进制 数 for (int i = 0; i < messageDigest.length; i++) { String shaHex = Integer.toHexString(messageDigest[i] & 0xFF); if (shaHex.length() < 2) { hexString.append(0); } hexString.append(shaHex); } return hexString.toString(); } catch (NoSuchAlgorithmException e) { e.printStackTrace(); } return ""; } }
package org.linlinjava.litemall.wx.web; import lombok.extern.slf4j.Slf4j; import com.alibaba.fastjson.JSONObject; import org.linlinjava.litemall.wx.util.WxSignUtil2; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.ResponseBody; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.io.IOException; import java.util.UUID; /** * @Description: * @Auther: wuxw * @Date: 2019/11/20 16:28 */ @Slf4j @Controller @RequestMapping("/wx/sign/config") public class WxSignConfigController { @ResponseBody @RequestMapping(value="/getJsapiTicket", method = RequestMethod.GET) public Object t2(HttpServletRequest request, HttpServletResponse response) throws IOException { log.info("-----------------------event2----------------------"); String url = request.getParameter("url"); //一、获取AccessToken String accessToken = WxSignUtil2.getAccessToken(); //二、获取Ticket String jsapi_ticket = WxSignUtil2.getTicket(accessToken); //三、时间戳和随机字符串 String noncestr = UUID.randomUUID().toString().replace("-", "").substring(0, 16);//随机字符串 String timestamp = String.valueOf(System.currentTimeMillis() / 1000);//时间戳 System.out.println("accessToken:"+accessToken+"\njsapi_ticket:"+jsapi_ticket+"\n时间戳:"+timestamp+"\n随机字符串:"+noncestr); //四、获取url if(url == null){ url="www.xxx.vip/index.html"; } /*根据JSSDK上面的规则进行计算,这里比较简单,我就手动写啦 String[] ArrTmp = {"jsapi_ticket","timestamp","nonce","url"}; Arrays.sort(ArrTmp); StringBuffer sf = new StringBuffer(); for(int i=0;i<ArrTmp.length;i++){ sf.append(ArrTmp[i]); } */ //五、将参数排序并拼接字符串 String str = "jsapi_ticket="+jsapi_ticket+"&noncestr="+noncestr+"×tamp="+timestamp+"&url="+url; //六、将字符串进行sha1加密 String signature =WxSignUtil2.SHA1(str); System.out.println("参数:"+str+"\n签名:"+signature); JSONObject obj = new JSONObject(); obj.put("jsapi_ticket",jsapi_ticket); obj.put("noncestr",noncestr); obj.put("timestamp",timestamp); obj.put("url",url); return obj; } }