開發微信公衆號在沒有正式的公衆平臺帳號時,咱們可使用測試平臺帳號———
測試平臺申請地址:https://mp.weixin.qq.com/debug/cgi-bin/sandbox?t=sandbox/loginhtml
進入以後咱們會看見
此時
appID、appsecret都有了,url是咱們成爲開發者與微信進行的一次握手配置(url其實就是咱們項目中你controller的訪問地址,token是咱們本身填寫的,可在後臺進行判斷的),這裏的url能夠用ngrok來作映射,這樣開發起來比較方便,ngrok配置(點擊查看)ngrok下載地址(點擊下載)
咱們能夠在開發者文檔中看見java
這裏查看詳細配置;期間咱們要說道微信會給咱們的url經過get方式傳遞4個參數web
下面咱們來看一下controller怎麼編寫spring
- package com.website.wechat.controller;
-
- import java.io.IOException;
- import java.security.MessageDigest;
- import java.util.ArrayList;
- import java.util.Collections;
- import java.util.List;
-
- import javax.servlet.http.HttpServletRequest;
- import javax.servlet.http.HttpServletResponse;
-
- 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;
-
- @Controller
- @RequestMapping(value="weixin")
- public class WeiXinController {
-
- private static final char[] HEX_DIGITS = { '0', '1', '2', '3', '4', '5',
- '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f' };
-
- @RequestMapping(value="getWeiXinMethod",method=RequestMethod.GET)
- @ResponseBody
- public void getWeiXinMethod(HttpServletRequest request, HttpServletResponse response) throws IOException{
- boolean validate = validate(request);
- if (validate) {
- response.getWriter().write(request.getParameter("echostr"));
- response.getWriter().close();
- }
-
- }
-
- private boolean validate(HttpServletRequest req) throws IOException {
- String signature = req.getParameter("signature");
- String timestamp = req.getParameter("timestamp");
- String nonce = req.getParameter("nonce");
- List<String> list = new ArrayList<String>();
- list.add("chenchen");
- list.add(timestamp);
- list.add(nonce);
- Collections.sort(list);
- String s = "";
- for (int i = 0; i < list.size(); i++) {
- s += (String) list.get(i);
- }
- if (encode("SHA1", s).equalsIgnoreCase(signature)) {
- return true;
- } else {
- return false;
- }
- }
-
- public static String encode(String algorithm, String str) {
- if (str == null) {
- return null;
- }
- try {
-
- MessageDigest messageDigest = MessageDigest.getInstance(algorithm);
-
- messageDigest.update(str.getBytes());
- return getFormattedText(messageDigest.digest());
- } catch (Exception e) {
- throw new RuntimeException(e);
- }
- }
-
- private static String getFormattedText(byte[] bytes) {
- int len = bytes.length;
- StringBuilder buf = new StringBuilder(len * 2);
-
- for (int j = 0; j < len; j++) {
- buf.append(HEX_DIGITS[(bytes[j] >> 4) & 0x0f]);
- buf.append(HEX_DIGITS[bytes[j] & 0x0f]);
- }
- return buf.toString();
- }
- }
package com.website.wechat.controller;
import java.io.IOException;
import java.security.MessageDigest;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
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;
@Controller
@RequestMapping(value="weixin")
public class WeiXinController {
private static final char[] HEX_DIGITS = { '0', '1', '2', '3', '4', '5',
'6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f' };
@RequestMapping(value="getWeiXinMethod",method=RequestMethod.GET)
@ResponseBody
public void getWeiXinMethod(HttpServletRequest request, HttpServletResponse response) throws IOException{
boolean validate = validate(request);
if (validate) {
response.getWriter().write(request.getParameter("echostr"));
response.getWriter().close();
}
}
private boolean validate(HttpServletRequest req) throws IOException {
String signature = req.getParameter("signature");//微信加密簽名
String timestamp = req.getParameter("timestamp");//時間戳
String nonce = req.getParameter("nonce");//隨機數
List<String> list = new ArrayList<String>();
list.add("chenchen");
list.add(timestamp);
list.add(nonce);
Collections.sort(list);//字典排序
String s = "";
for (int i = 0; i < list.size(); i++) {
s += (String) list.get(i);
}
if (encode("SHA1", s).equalsIgnoreCase(signature)) {
return true;
} else {
return false;
}
}
public static String encode(String algorithm, String str) {
if (str == null) {
return null;
}
try {
//Java自帶的加密類
MessageDigest messageDigest = MessageDigest.getInstance(algorithm);
//轉爲byte
messageDigest.update(str.getBytes());
return getFormattedText(messageDigest.digest());
} catch (Exception e) {
throw new RuntimeException(e);
}
}
private static String getFormattedText(byte[] bytes) {
int len = bytes.length;
StringBuilder buf = new StringBuilder(len * 2);
// 把密文轉換成十六進制的字符串形式
for (int j = 0; j < len; j++) {
buf.append(HEX_DIGITS[(bytes[j] >> 4) & 0x0f]);
buf.append(HEX_DIGITS[bytes[j] & 0x0f]);
}
return buf.toString();
}
}
此時驗證和調用已經沒問題,成功介入微信,成爲一名開發者