SpringBootフレームワークは微信公衆番号Tokenサーバーの検証を実現する


私は自分でテストして、有効で、だからコードを貼ります
TokenControl.java( )
package com.example.demo.control;
import com.example.demo.wxtools.SignUtil;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
@RestController
@RequestMapping("/weixin")
public class TokenControl {
    /**
     *  token 
     */
    @RequestMapping(value = "/token", method = RequestMethod.GET)
    public String get(HttpServletRequest request, HttpServletResponse response) throws Exception {
        System.out.println("---------------- ----------");
        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");

            //  signature , echostr, , 
            if (SignUtil.checkSignature(signature, timestamp, nonce)) {
                System.out.println("---- .........");
                return echostr;
        }else{
                return null;
            }
    }//public
}
SignUtil.java( )
package com.example.demo.wxtools;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.Arrays;

public class SignUtil {
    private static String token = "token";

    /**
     *  
     * @param signature  
     * @param timestamp  
     * @param nonce  
     * @return  
     */
    public static boolean checkSignature(String signature,String timestamp,String nonce){
        String checktext = null;
        if (null != signature) {
            // ToKen,timestamp,nonce  
            String[] paramArr = new String[]{token,timestamp,nonce};
            Arrays.sort(paramArr);
            // 
            String content = paramArr[0].concat(paramArr[1]).concat(paramArr[2]);

            try {
                MessageDigest md = MessageDigest.getInstance("SHA-1");
                // sha1 
                byte[] digest = md.digest(content.toString().getBytes());
                checktext = byteToStr(digest);
            } catch (NoSuchAlgorithmException e){
                e.printStackTrace();
            }
        }
        // signature 
        return checktext !=null ? checktext.equals(signature.toUpperCase()) : false;
    }

    /**
     *  16 
     * @param byteArrays  
     * @return  
     */
    private static String byteToStr(byte[] byteArrays){
        String str = "";
        for (int i = 0; i < byteArrays.length; i++) {
            str += byteToHexStr(byteArrays[i]);
        }
        return str;
    }

    /**
     *   
     * @param myByte  
     * @return  
     */
    private static String byteToHexStr(byte myByte) {
        char[] Digit = {'0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F'};
        char[] tampArr = new char[2];
        tampArr[0] = Digit[(myByte >>> 4) & 0X0F];
        tampArr[1] = Digit[myByte & 0X0F];
        String str = new String(tampArr);
        return str;
    }

}