日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > java >内容正文

java

微信公众号Java开发-笔记02【开发接入准备、开发接入】

發布時間:2024/9/30 java 28 豆豆
生活随笔 收集整理的這篇文章主要介紹了 微信公众号Java开发-笔记02【开发接入准备、开发接入】 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

學習視頻網址:嗶哩嗶哩網站?微信公眾號開發-Java版

  • 【P01-P02】微信公眾號Java開發-筆記01【微信公眾號介紹、開發環境搭建】
  • 【P03-P04】微信公眾號Java開發-筆記02【開發接入準備、開發接入】
  • 【P00-P00】微信公眾號Java開發-筆記03【】
  • 【P00-P00】微信公眾號Java開發-筆記04【】
  • 【P00-P00】微信公眾號Java開發-筆記05【】
  • 目錄

    P3 1.3 開發接入準備 19:08

    基本配置

    服務號 開發文檔

    接口測試號申請

    接入指南

    接口配置測試

    servlet代碼

    URL配置

    P4 1.4 開發接入 20:25

    sha1加密

    驗證簽名、接入成功截圖

    WxServlet.java

    WxService.java


    P3 1.3 開發接入準備 19:08

    基本配置

    自己注冊的公眾號,很多權限都沒有!

    服務號 開發文檔

    微信公眾開發平臺:https://mp.weixin.qq.com/

    ??

    接口測試號申請

    接入指南

    接口配置測試

    servlet代碼

    未經修改的servlet代碼:

    package servlet;import java.io.IOException; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse;/*** Servlet implementation class WxServlet*/ @WebServlet("/wx") public class WxServlet extends HttpServlet {private static final long serialVersionUID = 1L;/*** @see HttpServlet#HttpServlet()*/public WxServlet() {super();// TODO Auto-generated constructor stub}/*** @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)*/protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {// TODO Auto-generated method stubresponse.getWriter().append("Served at: ").append(request.getContextPath());}/*** @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)*/protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {// TODO Auto-generated method stubdoGet(request, response);}}

    URL配置

    package servlet;import java.io.IOException; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse;@WebServlet("/wx") public class WxServlet extends HttpServlet {private static final long serialVersionUID = 1L;protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {/*** signature 微信加密簽名,signature結合了開發者填寫的token參數和請求中的timestamp參數、nonce參數。timestamp 時間戳nonce 隨機數echostr 隨機字符串*/String signature = request.getParameter("signature");String timestamp = request.getParameter("timestamp");String nonce = request.getParameter("nonce");String echostr = request.getParameter("echostr");System.out.println(signature);System.out.println(timestamp);System.out.println(nonce);System.out.println(echostr);}protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {System.out.println("post");}}

    P4 1.4 開發接入 20:25

    sha1加密

    SHA-1——百度百科

    SHA-1(英語:Secure Hash Algorithm 1,中文名:安全散列算法1)是一種密碼散列函數,美國國家安全局設計,并由美國國家標準技術研究所(NIST)發布為聯邦數據處理標準(FIPS)。SHA-1可以生成一個被稱為消息摘要的160位(20字節)散列值,散列值通常的呈現形式為40個十六進制數。

    驗證簽名、接入成功截圖

    WxServlet.java

    package servlet;import java.io.IOException; import java.io.PrintWriter;import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse;import service.WxService;@WebServlet("/wx") public class WxServlet extends HttpServlet {private static final long serialVersionUID = 1L;protected void doGet(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {/*** signature 微信加密簽名,signature結合了開發者填寫的token參數和請求中的timestamp參數、nonce參數。 * timestamp 時間戳 * nonce 隨機數 * echostr 隨機字符串*/String signature = request.getParameter("signature");String timestamp = request.getParameter("timestamp");String nonce = request.getParameter("nonce");String echostr = request.getParameter("echostr");System.out.println(signature);System.out.println(timestamp);System.out.println(nonce);System.out.println(echostr);// 校驗簽名if (WxService.check(timestamp, nonce, signature)) {System.out.println("接入成功!");PrintWriter out = response.getWriter();// 原樣返回echostr參數out.print(echostr);out.flush();out.close();} else {System.out.println("接入失敗!");}}protected void doPost(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {System.out.println("post");}}

    WxService.java

    package service;import java.security.MessageDigest; import java.security.NoSuchAlgorithmException; import java.util.Arrays;public class WxService {private static final String TOKEN = "llzs";/*** 驗證簽名* @param timestamp* @param nonce* @param signature* @return*/public static boolean check(String timestamp, String nonce, String signature) {// 1)將token、timestamp、nonce三個參數進行字典序排序String[] strs = new String[] { TOKEN, timestamp, nonce };Arrays.sort(strs);// 2)將三個參數字符串拼接成一個字符串進行sha1加密String str = strs[0] + strs[1] + strs[2];String mysig = sha1(str);// 3)開發者獲得加密后的字符串可與signature對比,標識該請求來源于微信return mysig.equalsIgnoreCase(signature); // 進行字符串比對}/*** 進行sha1加密* * @param str* @return*/private static String sha1(String src) {try {// 獲取一個加密對象MessageDigest md = MessageDigest.getInstance("sha1"); // 獲取加密對象// 加密byte[] digest = md.digest(src.getBytes());char[] chars = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f' };StringBuilder sb = new StringBuilder();// 處理加密結果for (byte b : digest) {sb.append(chars[(b >> 4) & 15]);sb.append(chars[b & 15]);}return sb.toString();} catch (NoSuchAlgorithmException e) {e.printStackTrace();}return null;}}

    將啼饑者比,則得飽自樂;

    將號寒者比,則得暖自樂;

    將勞役者比,則優閑自樂;

    將疾病者比,則康健自樂;

    將禍患者比,則平安自樂;

    將死亡者比,則生存自樂。

    總結

    以上是生活随笔為你收集整理的微信公众号Java开发-笔记02【开发接入准备、开发接入】的全部內容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。