微信公众号Java开发-笔记02【开发接入准备、开发接入】
學(xué)習(xí)視頻網(wǎng)址:嗶哩嗶哩網(wǎng)站?微信公眾號(hào)開發(fā)-Java版
目錄
P3 1.3 開發(fā)接入準(zhǔn)備 19:08
基本配置
服務(wù)號(hào) 開發(fā)文檔
接口測(cè)試號(hào)申請(qǐng)
接入指南
接口配置測(cè)試
servlet代碼
URL配置
P4 1.4 開發(fā)接入 20:25
sha1加密
驗(yàn)證簽名、接入成功截圖
WxServlet.java
WxService.java
P3 1.3 開發(fā)接入準(zhǔn)備 19:08
基本配置
自己注冊(cè)的公眾號(hào),很多權(quán)限都沒有!
服務(wù)號(hào) 開發(fā)文檔
微信公眾開發(fā)平臺(tái):https://mp.weixin.qq.com/
??
接口測(cè)試號(hào)申請(qǐng)
接入指南
接口配置測(cè)試
servlet代碼
未經(jīng)修改的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結(jié)合了開發(fā)者填寫的token參數(shù)和請(qǐng)求中的timestamp參數(shù)、nonce參數(shù)。timestamp 時(shí)間戳nonce 隨機(jī)數(shù)echostr 隨機(jī)字符串*/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 開發(fā)接入 20:25
sha1加密
SHA-1——百度百科
SHA-1(英語(yǔ):Secure Hash Algorithm 1,中文名:安全散列算法1)是一種密碼散列函數(shù),美國(guó)國(guó)家安全局設(shè)計(jì),并由美國(guó)國(guó)家標(biāo)準(zhǔn)技術(shù)研究所(NIST)發(fā)布為聯(lián)邦數(shù)據(jù)處理標(biāo)準(zhǔn)(FIPS)。SHA-1可以生成一個(gè)被稱為消息摘要的160位(20字節(jié))散列值,散列值通常的呈現(xiàn)形式為40個(gè)十六進(jìn)制數(shù)。
驗(yàn)證簽名、接入成功截圖
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結(jié)合了開發(fā)者填寫的token參數(shù)和請(qǐng)求中的timestamp參數(shù)、nonce參數(shù)。 * timestamp 時(shí)間戳 * nonce 隨機(jī)數(shù) * echostr 隨機(jī)字符串*/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);// 校驗(yàn)簽名if (WxService.check(timestamp, nonce, signature)) {System.out.println("接入成功!");PrintWriter out = response.getWriter();// 原樣返回echostr參數(shù)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";/*** 驗(yàn)證簽名* @param timestamp* @param nonce* @param signature* @return*/public static boolean check(String timestamp, String nonce, String signature) {// 1)將token、timestamp、nonce三個(gè)參數(shù)進(jìn)行字典序排序String[] strs = new String[] { TOKEN, timestamp, nonce };Arrays.sort(strs);// 2)將三個(gè)參數(shù)字符串拼接成一個(gè)字符串進(jìn)行sha1加密String str = strs[0] + strs[1] + strs[2];String mysig = sha1(str);// 3)開發(fā)者獲得加密后的字符串可與signature對(duì)比,標(biāo)識(shí)該請(qǐng)求來(lái)源于微信return mysig.equalsIgnoreCase(signature); // 進(jìn)行字符串比對(duì)}/*** 進(jìn)行sha1加密* * @param str* @return*/private static String sha1(String src) {try {// 獲取一個(gè)加密對(duì)象MessageDigest md = MessageDigest.getInstance("sha1"); // 獲取加密對(duì)象// 加密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();// 處理加密結(jié)果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;}}將啼饑者比,則得飽自樂;
將號(hào)寒者比,則得暖自樂;
將勞役者比,則優(yōu)閑自樂;
將疾病者比,則康健自樂;
將禍患者比,則平安自樂;
將死亡者比,則生存自樂。
總結(jié)
以上是生活随笔為你收集整理的微信公众号Java开发-笔记02【开发接入准备、开发接入】的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 微信公众号Java开发-笔记01【微信公
- 下一篇: JavaWeb黑马旅游网-学习笔记03【