微信公众号开发环境搭建(一)

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/zl_momomo/article/details/81874981

公网映射配置

需要将本地地址映射到公网,这里我们使用natapp

注意:本地端口必须是要填8080的(这个购买后也是可以再修改的),因为微信公众平台接口的调用仅支持80

客户端下载

1.配置config.ini文件

2.获取authtoken

3.双击运行natapp.exe,出现以下信息映射成功

微信公众平台接口测试

测试号管理

登录测试号管理,修改接口配置信息

使用springmvc配置后台

/**
 * 基本配置类,接收微信消息推送类
 */
@Controller
@RequestMapping("/weixin")
public class WeiXinBaseController {
    //消息处理服务类
    @Resource(name = "messageService")
    private MessageService messageService;

    // 自定义 token(开通服务时使用)
    private String TOKEN = "7A43B6D26D7A422BA575175B0368EAF8";
    /**
     * 微信消息验证服务器方法
     * @param request
     * @param response
     */
    @RequestMapping(value = {"/notice"}, method = RequestMethod.GET)
    public void getNotice(HttpServletRequest request, HttpServletResponse response) {
        try {
            logger.info("notice 连通..");
            // 微信加密签名
            String signature = request.getParameter("signature");
            // 随机字符串
            String echostr = request.getParameter("echostr");
            // 时间戳
            String timestamp = request.getParameter("timestamp");
            // 随机数
            String nonce = request.getParameter("nonce");

            String[] str = {TOKEN, timestamp, nonce};
            Arrays.sort(str); // 字典序排序
            String bigStr = str[0] + str[1] + str[2];
            // SHA1加密
            String digest = Sha1Util.getSha1(bigStr);

            // 确认请求来至微信
            if (digest.equals(signature)) {
                response.getWriter().print(echostr);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     * 微信消息处理
     * @param request
     * @param response
     */
    @RequestMapping(value = {"/notice"}, method = RequestMethod.POST)
    public void postNotice(HttpServletRequest request, HttpServletResponse response) {
        logger.info("进入 notice 方法");
        String respXml = messageService.processRequest(request);

        try {
            response.getWriter().print(respXml);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

 更多测试开发参考 微信公众平台技术文档   微信公众平台接口调试工具 

猜你喜欢

转载自blog.csdn.net/zl_momomo/article/details/81874981
今日推荐