這篇文章給大家分享的是有關(guān)微信JS接口簽名校驗(yàn)工具的示例分析的內(nèi)容。小編覺得挺實(shí)用的,因此分享給大家做個(gè)參考,一起跟隨小編過來看看吧。
成都創(chuàng)新互聯(lián)成立與2013年,先為青原等服務(wù)建站,青原等地企業(yè),進(jìn)行企業(yè)商務(wù)咨詢服務(wù)。為青原企業(yè)網(wǎng)站制作PC+手機(jī)+微官網(wǎng)三網(wǎng)同步一站式服務(wù)解決您的所有建站問題。
1、微信 JS 接口簽名校驗(yàn)工具
2、具體開發(fā)
2.1 獲取access_token,然后jsapi_ticket
/** * 獲取access_token,然后jsapi_ticket */ private String getAccessToken_ticket(String path) { String access_token = null; // access_token String atime = null;// 獲取時(shí)間 String a_expires_in = null;// 有效時(shí)間(s) String ticket = null;// jsapi_ticket String ttime = null;// 得到時(shí)間 String t_expires_in = null;// 有效時(shí)間(s) String access_tokenStr = TUtils.getAccessToken(APPID, API_KEY); if (access_tokenStr != null && access_tokenStr.indexOf("access_token") != -1) { try { JSONObject jsonObject = new JSONObject(access_tokenStr); access_token = jsonObject.getString("access_token"); a_expires_in = jsonObject.getString("expires_in"); atime = getCurrentDateStr(); } catch (JSONException e) { // e.printStackTrace(); } } if (access_token != null && !access_token.equals("")) { String ticketStr = TicketUtils.getJSAPITicket(access_token); // System.out.println("ticketStr:" + ticketStr); if (ticketStr != null && ticketStr.indexOf("ticket") != -1) { try { JSONObject jsonObject = new JSONObject(ticketStr); ticket = jsonObject.getString("ticket"); t_expires_in = jsonObject.getString("expires_in"); ttime = getCurrentDateStr(); } catch (JSONException e) { // e.printStackTrace(); } } } String result = null; if (ticket != null && !ticket.equals("")) { result = "{\"access_token\":\"" + access_token + "\",\"a_expires_in\":\"" + a_expires_in + "\",\"atime\":\"" + atime + "\",\"ticket\":\"" + ticket + "\",\"t_expires_in\":\"" + t_expires_in + "\",\"ttime\":\"" + ttime + "\"}"; if (MyFileUtils.writeIntoText(path, result)) { // System.out.println("寫入文件成功"); // System.out.println(result); } else { System.out.println("寫入微信簽名文件失敗"); } } return result; }
public static String getAccessToken(String APPID, String APPSECRET) { String url = "https://api.weixin.qq.com/cgi-bin/token"; String params = "grant_type=client_credential&appid=" + APPID + "&secret=" + APPSECRET; String resultStr = HttpRequest.sendGet(url, params); // sendGet:用get方法獲取數(shù)據(jù) ,具體請(qǐng)參考之間的關(guān)于微信的文章 http://www.cnblogs.com/jiduoduo/p/5749363.html return resultStr; } /** * 根據(jù)access_token獲取ticket { "errcode":0, "errmsg":"ok", "ticket": * "bxLdikRXVbTPdHSM05e5u5sUoXNKd8-41ZO3MhKoyN5OfkWITDGgnr2fwJ0m9E8NYzWKVZvdVtaUgWvsdshFKA" * , "expires_in":7200 } * * @param access_token * @return */ public static String getJSAPITicket(String access_token) { String url = "https://api.weixin.qq.com/cgi-bin/ticket/getticket"; String params = "type=jsapi&access_token=" + access_token; String resultStr = HttpRequest.sendGet(url, params); return resultStr; }
2.2具體生成簽名signature
public String Wx_Signature() { String path = ServletActionContext.getServletContext().getRealPath( "/wx/"); // System.out.println(path); try { String tokenJSON = MyFileUtils.readText(path); // String access_token = null; // access_token String atime = null;// 獲取時(shí)間 String a_expires_in = null;// 有效時(shí)間(s) String ticket = null;// jsapi_ticket // String ttime = null;// 得到時(shí)間 // String t_expires_in = null;// 有效時(shí)間(s) String result = tokenJSON; if (result == null || result.equals("")) { tokenJSON = getAccessToken_ticket(path); } // System.out.println(result); if (tokenJSON != null && !tokenJSON.equals("") && tokenJSON.indexOf("access_token") != -1) { try { JSONObject jsonObject = new JSONObject(tokenJSON); // access_token = jsonObject.getString("access_token");// // access_token atime = jsonObject.getString("atime");// 開始時(shí)間 a_expires_in = jsonObject.getString("a_expires_in");// 有效時(shí)間 ticket = jsonObject.getString("ticket");// jsapi_ticket // System.out.println(ticket); // ttime = jsonObject.getString("ttime");// 開始時(shí)間 // t_expires_in = jsonObject.getString("t_expires_in");// // 有效時(shí)間 String t1 = getCurrentDateStr(); String t2 = atime; // System.out.println(atime); // System.out.println(a_expires_in); // System.out.println(TimeInterval.getInterval(t2, t1)); long end_time = Long.parseLong(a_expires_in) - 60; if (TimeInterval.getInterval(t2, t1) > end_time) { ticket = getAccessToken_ticket(path); } } catch (JSONException e) { msg = e.getMessage(); } } else { } // System.out.println(ticket); String url = getParameter("url"); String noncestr = TUtils.getRandomString(16); String timestamp = System.currentTimeMillis() + ""; timestamp = timestamp.substring(0, 10); String data = "jsapi_ticket=" + ticket + "&noncestr=" + noncestr + "×tamp=" + timestamp + "&url=" + url; String digest = new SHA1().getDigestOfString(data.getBytes()); String signature = digest.toLowerCase();// signature result = "{\"noncestr\":\"" + noncestr + "\",\"timestamp\":\"" + timestamp + "\",\"url\":\"" + url + "\",\"signature\":\"" + signature + "\" ,\"ticket\":\"" + ticket + "\"}"; msg = result; } catch (IOException e) { msg = e.getMessage(); } return msg }
說明:簽名是有調(diào)用次數(shù),需要將其cache到服務(wù)器的文件中。
感謝各位的閱讀!關(guān)于“微信JS接口簽名校驗(yàn)工具的示例分析”這篇文章就分享到這里了,希望以上內(nèi)容可以對(duì)大家有一定的幫助,讓大家可以學(xué)到更多知識(shí),如果覺得文章不錯(cuò),可以把它分享出去讓更多的人看到吧!
網(wǎng)頁(yè)題目:微信JS接口簽名校驗(yàn)工具的示例分析
網(wǎng)址分享:http://jinyejixie.com/article44/ppjohe.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供微信小程序、域名注冊(cè)、建站公司、響應(yīng)式網(wǎng)站、App開發(fā)、營(yíng)銷型網(wǎng)站建設(shè)
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)