微信平台开发1--开发者模式基本配置
生活随笔
收集整理的這篇文章主要介紹了
微信平台开发1--开发者模式基本配置
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
網上有很多教程,這里作為本人學習筆記記錄。網上教程大多是PHP版本,由于我比較喜歡Java,這里用Java進行配置。
基本步驟參考官網
填寫好token和URL之后下面要求驗證服務器地址有效性
下面是代碼
import java.io.IOException; import java.io.PrintWriter;import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse;import com.xinshidaisudi.util.SignUtil;public class CoreServlet extends HttpServlet {/*** Constructor of the object.*/public static final String token = "Token";//token 這里要跟你剛才填寫的Token一致public CoreServlet() {super();}/*** Destruction of the servlet. <br>*/public void destroy() {super.destroy(); // Just puts "destroy" string in log// Put your code here}/*** The doGet method of the servlet. <br>** This method is called when a form has its tag value method equals to get.* * @param request the request send by the client to the server* @param response the response send by the server to the client* @throws ServletException if an error occurred* @throws IOException if an error occurred*/public void doGet(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {//簽名String singature = request.getParameter("signature");//時間戳String timestamp = request.getParameter("timestamp");//隨機數String nonce = request.getParameter("nonce");//隨機字符串String echostr = request.getParameter("echostr");//System.out.println("echostr: " + singature);//System.out.println("timestamp: " + timestamp);//System.out.println("nonce: " + nonce);//System.out.println("echostr: " + echostr);// 開發者通過檢驗signature對請求進行校驗(下面有校驗方式)。 // 若確認此次GET請求來自微信服務器,請原樣返回echostr參數內容,則接入生效,成為開發者成功,否則接入失敗。PrintWriter out = response.getWriter();if(SignUtil.checkSingature(token, singature, timestamp, nonce)){out.write(echostr);}out.close();out = null;}/*** The doPost method of the servlet. <br>** This method is called when a form has its tag value method equals to post.* * @param request the request send by the client to the server* @param response the response send by the server to the client* @throws ServletException if an error occurred* @throws IOException if an error occurred*/public void doPost(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {}/*** Initialization of the servlet. <br>** @throws ServletException if an error occurs*/public void init() throws ServletException {// Put your code here}}SignUtil.checkSingature(token, singature, timestamp, nonce)方法:
public static boolean checkSingature(String token,String singature ,String timestamp,String nonce){String[] arr = new String[]{token,timestamp,nonce};Arrays.sort(arr);//進行字典排序//將字符串拼接成一個字符串StringBuilder strBUilder = new StringBuilder();for(String s : arr){strBUilder.append(s);}String str = strBUilder.toString();//sha1加密StringBuilder builder = new StringBuilder();try {MessageDigest md = MessageDigest.getInstance("SHA-1");md.update(str.getBytes());byte[] b = md.digest();//轉換成16進制字符串for(byte bb : b){String s = Integer.toHexString(bb & 0xff);if(s.length() == 1)builder.append("0");builder.append(s);}} catch (NoSuchAlgorithmException e) {// TODO Auto-generated catch blocke.printStackTrace();}String result = builder.toString();//得到SHA加密后結果//System.out.println("result: " + result);return result != null? result.equals(singature.toLowerCase()) :false;}這樣就完成了基本的接入。
?
?
轉載于:https://www.cnblogs.com/daniel-lee/p/4249126.html
總結
以上是生活随笔為你收集整理的微信平台开发1--开发者模式基本配置的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: ios统计流量代码
- 下一篇: 算法题:找出一个数组中相加值最大的连续序