微信公众号-开发者模式接入配置(简单介绍)

相关文档:微信公众号开发-实现服务端回复消息为空   

                  xstream简单使用(wx消息体处理)  

                  XStream-bean转换为xml是,携带CDATA

                  controller中直接返回String类型字符串报类型转换异常

 

首先,按照微信公众号开发文档提示,进行公众号申请:

申请完成后,登录该公众号进行相关配置,

微信公众号-开发者模式接入配置(简单介绍)_第1张图片

微信公众号-开发者模式接入配置(简单介绍)_第2张图片

在左侧菜单栏:开发-基本配置 

微信公众号-开发者模式接入配置(简单介绍)_第3张图片

点击修改配置:

微信公众号-开发者模式接入配置(简单介绍)_第4张图片

其中,url代表微信要和我们服务端交互的http接口controller,token可以根据实际情况来设置。

然后需要开发对应的controller,部分代码如下:

package com.wx.mobile.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.google.common.collect.Lists;

import com.thoughtworks.xstream.XStream;
import org.apache.commons.lang3.exception.ExceptionUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Collections;
import java.util.Date;
import java.util.List;
import java.util.Objects;

/**
 * @description: 微信验证
 * @author: feng
 * @create: 2018-10-09 15:39
 **/
@Controller
@RequestMapping(value = "/wx")
public class WXAuthController extends BaseController {



    //wx认证
    @RequestMapping(value = "")
    @ResponseBody()
    @JProfiler(jKey = "AccountController.wxAuth.mobile", mState = {JProEnum.TP, JProEnum.FunctionError, JProEnum.Heartbeat}, jAppName = Constant.JDOS_SHOPSINNER)
    public void wxAuth(String signature, String timestamp, String nonce, String echostr, HttpServletRequest request, HttpServletResponse response) {
        logger.info("wx-param:signature={},timestamp={},nonce={},echostr={}",
                signature, timestamp, nonce, echostr);
        try {
            //=================校验请求方========================
            if (StringUtils.isEmpty(signature) || StringUtils.isEmpty(timestamp)
                    || StringUtils.isEmpty(nonce)) {
                return;
            }
            String token = "test-token";//自己设置的token
            List sort = Lists.newArrayList();
            sort.add(token);
            sort.add(timestamp);
            sort.add(nonce);
            Collections.sort(sort);
            String auth = sort.get(0).concat(sort.get(1)).concat(sort.get(2));
            logger.info("wx-:auth={}", auth);
            String sha1 = MD5Util.sha1(auth);
            logger.info("wx-:sha1={}", sha1);
            if (StringUtils.isEmpty(signature) || !signature.equals(sha1)) {
                return;
            }
            logger.info("wx-auth-success");
            String param = MobileUtil.readStream(request.getInputStream());
            logger.info("wx-param={}", JsonUtils.toJson(param));
            //=================校验请求方========================
            if (!StringUtils.isEmpty(echostr)) {
                try {
                    response.getWriter().print(echostr);
                    response.getWriter().flush();
                } catch (IOException e) {
                    logger.info("wx-response-failure");
                }
                logger.info("wx-success");
                return;
            } else {
                noReply(response);
            }
        } catch (Exception e) {
            noReply(response);
            logger.error("wx-failure, e = {}", e);
        }
    }

    /**
     * 无需回复
     *
     * @param response
     */
    private void noReply(HttpServletResponse response) {
        try {
            logger.info("wx-noReply");
            response.getWriter().print("success");
            response.getWriter().flush();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

}

sha1算法加密:

/*sha1加密*/
    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 "";
    }

 点击提交后,就会有微信侧请求进入该controller方法中,校验通过,返回随机数,否则返回"success"。

 

 

 

你可能感兴趣的:(微信公众号-开发者模式接入配置(简单介绍))