微信公众号接口配置信息详解

微信测试号页面

这里写图片描述

当回调接口没有上面的代码时,接口配置信息修改后保存提示配置失败

需要在项目中运行一下代码接口,才能实现接口配置

回调接口代码:

package com.huawei.test;

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

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

import com.huawei.interfaces.tools.StrUtil;
import com.umpay.api.util.SignUtil;

import edu.emory.mathcs.backport.java.util.Collections;


public class TestAction extends HttpServlet
{
    private static final long serialVersionUID = 1L;

public static final String TOKEN = "123456";  

@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp)
    throws ServletException, IOException
{
    doPost(req, resp);
}

@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException
{
    
    String signature = request.getParameter("signature");  
    // 时间戳  
    String timestamp = request.getParameter("timestamp");  
    // 随机数  
    String nonce = request.getParameter("nonce");  
    // 随机字符串  
    String echostr = request.getParameter("echostr");  

    PrintWriter out = response.getWriter();  
    // 通过检验signature对请求进行校验,若校验成功则原样返回echostr,表示接入成功,否则接入失败  
    if (checkSignature(signature, timestamp, nonce) == signature ) {  
        out.write(echostr);  
        System.out.println("微信服务验证成功!"+echostr);  
    }else {  
         out.print(echostr);  
         System.out.println("微信服务验证失败!"+echostr);  
    }  
    out.flush();  
    out.close();  
}

public static String checkSignature(String signature ,String timestamp, String nonce)
{
    String[] src = {TOKEN,timestamp,nonce};
    List<String> list =Arrays.asList(src);
    Collections.sort(list);
    
    StringBuilder sb = new StringBuilder();
    for (int i = 0; i < list.size(); i++)
    {
        sb.append(list.get(i));
    }
    return StrUtil.SHA1(sb.toString());
    
}

 /**
     * 
     * @param decript
     * @return
     */
    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 "";
}
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

IT界的一只菜鸟

你的鼓励是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值