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; } }