JavaWeb接入微信公众号

Posted 张文琪2022

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了JavaWeb接入微信公众号相关的知识,希望对你有一定的参考价值。

不啰嗦,直接上代码:

//这里注意要确保微信公众号开发配置时的网址可以访问到这个方法
@RequestMapping(value = "/setwechat")
    public void setWechat(HttpServletRequest request,HttpServletResponse response) throws Exception

        response.setCharacterEncoding("utf-8");
        request.setCharacterEncoding("utf-8");
        String result = "";
        /* 不管在微信上进行什么操作,都会有xml报文传到绑定URL上 */
        Map res = showParams(request);
        Set<Map.Entry<Integer,String>> entrys = res.entrySet();
        System.out.println("setwechat微信获取数据如下:");
        for(Map.Entry entry:entrys)
            String key = (String)entry.getKey();
            String value = (String)entry.getValue();
            System.out.println("key:"+key+" value:"+value);
        
        if(request.getParameter("openid")==null)//接入校验
            String timestamp = "", nonce = "", signature = "", echostr = "";
            timestamp = request.getParameter("timestamp");
            nonce = request.getParameter("nonce");
            signature = request.getParameter("signature");
            echostr = request.getParameter("echostr");
            boolean b = checkSignature(token,signature,timestamp,nonce);
            if(b)
                result = echostr;
            
        else//非接入

        

        PrintWriter pw = response.getWriter();
        pw.print(result);
        pw.close();
    
//检查是否合法
public static boolean checkSignature(String token,String signature, String timestamp,
                                         String nonce) 
       String[] arr = new String[]  token, timestamp, nonce ;
        Arrays.sort(arr);
        StringBuilder content = new StringBuilder();
        for (int i = 0; i < arr.length; i++) 
            content.append(arr[i]);
        
        MessageDigest md = null;
        String tmpStr = null;
        try 
            md = MessageDigest.getInstance("SHA-1");
            byte[] digest = md.digest(content.toString().getBytes());
            tmpStr = byteToStr(digest);
         catch (NoSuchAlgorithmException e) 
            e.printStackTrace();
        

        return tmpStr != null ? tmpStr.equals(signature.toUpperCase()) : false;

    


    private static String byteToStr(byte[] digest) 
        String strDigest = "";
        for (int i = 0; i < digest.length; i++) 
            strDigest += byteToHexStr(digest[i]);
        
        return strDigest;
    


    public static String byteToHexStr(byte b) 
        char[] Digit =  '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A',
                'B', 'C', 'D', 'E', 'F' ;

        char[] tempArr = new char[2];
        tempArr[0] = Digit[(b >>> 4) & 0X0F];
        tempArr[1] = Digit[b & 0X0F];
        String s = new String(tempArr);
        return s;
    
    //获取所有request里的数据,便于测试
    public static Map<String,Object> showParams(HttpServletRequest request) 
        Map<String,Object> map = new HashMap<String,Object>();
        Enumeration paramNames = request.getParameterNames();
        while (paramNames.hasMoreElements()) 
            String paramName = (String) paramNames.nextElement();

            String[] paramValues = request.getParameterValues(paramName);
            if (paramValues.length >0) 
                String paramValue = paramValues[0];
                if (paramValue.length() != 0) 
                    map.put(paramName, paramValue);
                
            
        
        return map;
    

以上是关于JavaWeb接入微信公众号的主要内容,如果未能解决你的问题,请参考以下文章

SpringCloud : 接入 微信公众号平台接入微信请求(支持多公众号)

微信公众号开发纪要-微信公众号接入

微信公众号接入

微信公众号开发--开发服务器接入微信服务器

微信公众号接入之排序问题小记

微信公众号开发--接入