1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129
|
package com.example.springbootdemo.config;
import com.example.springbootdemo.util.CheckUtil; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.io.IOException; import java.io.PrintWriter;
public class extends HttpServlet {
大专栏 用NATAPP进行微信本地开发调试ss="keyword">protected void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException { request.setCharacterEncoding("UTF-8"); response.setCharacterEncoding("UTF-8"); PrintWriter out = response.getWriter();
String echostr = request.getParameter("echostr"); String signature = request.getParameter("signature"); String timestamp = request.getParameter("timestamp"); String nonce = request.getParameter("nonce");
if (CheckUtil.check(timestamp, nonce, signature) == true) out.print(echostr); } } -------------------------------------------------------------------------------- package com.example.springbootdemo.util;
import java.security.MessageDigest; import java.util.Arrays;
public class CheckUtil { public static String token = "weixinAAAA";
public static boolean check(String timestamp, String nonce, String signature) { String[] string = new String[]{timestamp, nonce, token}; String s = new String(); String encrypt = null;
Arrays.sort(string); for (int i = 0; i < string.length; i++) s += string[i];
encrypt = getSha1(s); System.out.println(encrypt); if (encrypt.equals(signature)) return true; else return false; }
public static String getSha1(String decript) { if(decript == null | decript .length() == 0) return null; char hexDigits[] = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'}; try { MessageDigest mdTemp = MessageDigest.getInstance("SHA1"); mdTemp.update(decript.getBytes("UTF-8")); byte[] md = mdTemp.digest(); int j = md.length, k = 0; char buf[] = new char[j*2]; for (int i = 0; i < j; i++) { byte byte0 = md[i]; buf[k++] = hexDigits[byte0 >>> 4 & 0xf]; buf[k++] = hexDigits[byte0 & 0xf]; } return new String(buf); } catch (Exception e) { return null; } } } -------------------------------------------------------------------------------- package com.example.springbootdemo.servlet;
import com.example.springbootdemo.config.WeixinServlet; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.boot.builder.SpringApplicationBuilder; import org.springframework.boot.web.servlet.ServletRegistrationBean; import org.springframework.boot.web.servlet.support.SpringBootServletInitializer; import org.springframework.context.annotation.Bean;
@SpringBootApplication public class ServletConfig extends SpringBootServletInitializer {
public static void main(String[] args) { SpringApplication.run(ServletConfig.class, args); }
protected SpringApplicationBuilder configure(SpringApplicationBuilder application) { return application.sources(ServletConfig.class); }
@Bean public ServletRegistrationBean wexinA() { return new ServletRegistrationBean(new WeixinServlet(), "/weixinA"); } }
|