微信开发-接入

1 创建一个web工程

微信开发-接入_第1张图片

1.1 创建一个servlet类:CheckSignature

package com.weixin;

import java.io.IOException;
import java.util.Arrays;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class CheckSignature extends HttpServlet {

    private final String token = "wosiguwozai15427";

    public CheckSignature() {
        super();
    }


    public void destroy() {
        super.destroy(); // Just puts "destroy" string in log
        // Put your code here
    }


    public void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        request.setCharacterEncoding("UTF-8");
        response.setCharacterEncoding("UTF-8");
        String signature = request.getParameter("signature");
        String timestamp = request.getParameter("timestamp");
        String nonce = request.getParameter("nonce");
        String echostr = request.getParameter("echostr");

        //排序
        String sortString = sort(token, timestamp, nonce);
        //加密
        String mytoken = Decript.SHA1(sortString);
        //校验签名
        if (mytoken != null && mytoken != "" && mytoken.equals(signature)) {
// System.out.println("签名校验通过。");
            response.getWriter().println(echostr);

        } else {

            System.out.println("签名校验失败。");
        }
    }


    public static String sort(String token, String timestamp, String nonce) {
        String[] strArray = { token, timestamp, nonce };
        Arrays.sort(strArray);

        StringBuilder sbuilder = new StringBuilder();
        for (String str : strArray) {
            sbuilder.append(str);
        }

        return sbuilder.toString();
    }

    public void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {

    }

    public void init() throws ServletException {
        // Put your code here
    }

}

1.2 创建功能类:Decript

package com.weixin;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

public class Decript {
    public static String SHA1(String decript) {
        try {
            MessageDigest digest = MessageDigest
                    .getInstance("SHA-1");
            digest.update(decript.getBytes());
            byte messageDigest[] = digest.digest();
            // Create Hex String
            StringBuffer hexString = new StringBuffer();
            // 字节数组转换为 十六进制 数
            for (int i = 0; i < messageDigest.length; i++) {
                String shaHex = Integer.toHexString(messageDigest[i] & 0xFF);
                if (shaHex.length() < 2) {
                    hexString.append(0);
                }
                hexString.append(shaHex);
            }
            return hexString.toString();

        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        }
        return "";
    }
}

1.3 配置web.xml文件

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.0" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">

  <welcome-file-list>
        <welcome-file>index.jsp</welcome-file>
</welcome-file-list>

  <servlet>
    <servlet-name>CheckSignature</servlet-name>
    <servlet-class>com.weixin.CheckSignature</servlet-class>
  </servlet>
  <servlet-mapping>
    <servlet-name>CheckSignature</servlet-name>
    <url-pattern>/CheckSignature</url-pattern>
  </servlet-mapping>

</web-app>

1.4 把项目上传至服务器

2 填写微信的配置信息

主要填写的有URL,token和EncodingAESKey,然后点提交即可,注意url路径要填写访问的是哪一个servlet。

你可能感兴趣的:(Web,servlet,微信)