springboot配置微信公众号token接口

需要微信公众号测试号、natapp(这个是可以让外网访问你本地的东西,免费的)、springboot.

首先是测试号要配置服务器信息:

在没配置好之前提交是失败的!!!为什么会失败,是因为提交相当于发起一次请求,你没配置好发的请求就不懂去哪里了。

所有先配置好其它东西才可以提交。

natapp(这个是可以让外网访问你本地的东西,免费的),可以看相关教程很简单的几步,natapp官网:https://natapp.cn/

配置好用80端口,不用80的话就用nginx反向代理80端口(测试就不用nginx)

下面是controller和一个接口类,String token = "xxx";这里的xxx要和微信公众号测试的Token一样

WxUrlRequest:

import com.weixin.wxdevelop.service.WeChatService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
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;
import java.io.IOException;

/**
 * @ClassName WxConfigController
 * @Author liming
 * @Description //TODO
 * @Date 2020/6/7 10:54
 **/
@RestController
@RequestMapping("/wx")
public class WxUrlRequest {
    private final static Logger LOG = LoggerFactory.getLogger(WxUrlRequest.class);
    /**
     * 接入微信服务
     * @param request
     * @param response
     */
    @Autowired
    private WeChatService wechatservice;

    @RequestMapping(value="/urlR",method= RequestMethod.GET)
    public void index(HttpServletRequest request, HttpServletResponse response){
        LOG.info("微信接入服务器");
        String signature = request.getParameter("signature");
        String timestamp = request.getParameter("timestamp");
        String nonce = request.getParameter("nonce");
        String token = "你的token";
        String echostr = request.getParameter("echostr");
        if (wechatservice.verifyInfo(signature, timestamp, nonce, token)) {
            LOG.info("echostr为:{}", echostr);
            if (echostr != null) {
                try {
                    response.getWriter().write(echostr);
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        } else {
            LOG.info("signature为:{}", signature);
            LOG.info("timestamp为:{}", timestamp);
            LOG.info("nonce为:{}", nonce);
            LOG.info("token为:{}", token);
        }
    }

}
WeChatService:
import org.apache.commons.codec.digest.DigestUtils;
import org.springframework.stereotype.Service;
import java.util.TreeSet;
/**
 * @ClassName WeChatService
 * @Author liming
 * @Description //TODO
 * @Date 2020/6/7 10:58
 **/
@Service
public class WeChatService {
    public Boolean verifyInfo(String signature, String timestamp, String nonce,
                              String token) {
        TreeSet<String> set = new TreeSet<String>();
        set.add(token);
        set.add(timestamp);
        set.add(nonce);
        StringBuilder sBuilder = new StringBuilder();
        for (String item : set) {
            sBuilder.append(item);
        }
        String sign = DigestUtils.sha1Hex(sBuilder.toString());
        return signature.equalsIgnoreCase(sign);
    }
}

别忘了springboot的tomcat端口改为80;

启动好natapp和springboot就可以到微信测试号那里提交域名了

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

江湖行骗老中医

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

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

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

打赏作者

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

抵扣说明:

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

余额充值