【微信二次开发】微信开发者模式接入接口JAVA大法

微信官方文档:

https://developers.weixin.qq.com/doc/offiaccount/Basic_Information/Access_Overview.html

 

Demo:

import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.io.UnsupportedEncodingException;

@RestController
@RequestMapping(value = "/weChat/callback")
public class WeChatCallBackController {

    private static final Logger LOG = LoggerFactory.getLogger(WeChatCallBackController.class);

    /**
     * 校验信息是否是从微信服务器发出
     *
     * @param request
     * @param response
     */
    @GetMapping
    public void get(HttpServletRequest request, HttpServletResponse response) {
        try {
            request.setCharacterEncoding("UTF-8");
            response.setCharacterEncoding("UTF-8");
        } catch (UnsupportedEncodingException e) {
            LOG.error("UnsupportedEncodingException", e);
        }

        // 微信加密签名
        String signature = request.getParameter("signature");
        // 时间戳
        String timestamp = request.getParameter("timestamp");
        // 随机数
        String nonce = request.getParameter("nonce");
        // 随机字符串
        String echostr = request.getParameter("echostr");
        // 微信开发者模式配置Token
        String token = "Token";

        try (PrintWriter writer = response.getWriter()) {
            // 通过检验signature对请求进行校验,若校验成功则原样返回echostr,表示接入成功,否则接入失败
            if (WeChatPayUtil.checkSignature(token, signature, timestamp, nonce)) {
                writer.write(echostr);
            }else {
                // 回复消息为空,必须做出下述回复,这样微信服务器才不会对此作任何处理,
                // 并且不会发起重试,否则,将出现严重的错误提示。直接回复success(推荐方式)
                writer.write("success");
            }
        } catch (AesException e) {
            LOG.error("AesException", e);
        } catch (IOException e) {
            LOG.error("IOException", e);
        }
    }

    /**
     * 微信服务器处理消息
     *
     * @param request
     * @param response
     */
    @PostMapping
    public void post(HttpServletRequest request, HttpServletResponse response) {
        try {
            request.setCharacterEncoding("UTF-8");
            response.setCharacterEncoding("UTF-8");
        } catch (UnsupportedEncodingException e) {
            LOG.error("UnsupportedEncodingException", e);
        }

        // 加密 response
        String respMessage = "加密response";
        try (PrintWriter writer = response.getWriter()) {
            if (StringUtils.isNotBlank(respMessage)) {
                writer.write(respMessage);
            } else {
                writer.write("success");
            }
        } catch (Exception e) {
            LOG.error("Exception", e);
        }
    }

}

 

   /**
    * 验证微信开发者模式回调Token
    *
    * @param token        Token(令牌)
    * @param msgSignature 签名串,对应URL参数的signature
    * @param timeStamp    时间戳,对应URL参数的timestamp
    * @param nonce        随机串,对应URL参数的nonce
    * @return 是否为安全签名
    * @throws AesException 执行失败,请查看该异常的错误码和具体的错误信息
    */
   public static boolean checkSignature(String token, String msgSignature, String timeStamp, String nonce) 
           throws AesException {
      String signature = SHA1.getSHA1(token, timeStamp, nonce);
      if (!signature.equals(msgSignature)) {
         throw new AesException(AesException.ValidateSignatureError);
      }
      return true;

微信后台配置接口URL,若提交成功即OK啦!

 

微信公众号和微信小程序通用

5.1 注册账号 ......................................................................................................................... 7 SDK 使用说明 .................................................................................................................... 8 5.2 获取企业实名认证地址 ................................................................................................. 9 SDK 使用说明 .................................................................................................................. 12 5.3 获取个人实名认证地址 ............................................................................................... 13 SDK 使用说明 .................................................................................................................. 16 5.4 实名证书申请 ............................................................................................................... 16 SDK 使用说明 .................................................................................................................. 17 5.5 印章上传 ....................................................................................................................... 17 SDK 使用说明 .................................................................................................................. 18 5.6 自定义印章 ................................................................................................................... 19 SDK 使用说明 .................................................................................................................. 20 5.7 合同上传 ....................................................................................................................... 20 SDK 使用说明 .................................................................................................................. 21 5.8 模板上传 ....................................................................................................................... 22 SDK 使用说明 ..........................................
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值