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;
    }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值