微信公众号token接口springboot

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

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

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

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

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

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

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

WxUrlRequest:

import com.yifan.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;

/**
 * 微信接口Token
 * */
@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 = "xxx";
            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;
@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就可以到微信测试号那里提交域名了

猜你喜欢

转载自blog.csdn.net/ye976142425/article/details/84622368