参考:
接入指南: https://developers.weixin.qq.com/doc/offiaccount/Basic_Information/Access_Overview.html
视频参考:
https://www.bilibili.com/video/BV1QJ4m1n7ut
php修改后代码参考:
<?php
// 设置状态码为200
header('HTTP/1.1 200 OK');
// 设置内容类型为HTML
header('Content-Type: text/html');
function checkSignature()
{
$signature = $_GET["signature"];
$timestamp = $_GET["timestamp"];
$nonce = $_GET["nonce"];
$echostr = $_GET["echostr"];
$token = "udjxxx";
$tmpArr = array($token, $timestamp, $nonce);
sort($tmpArr, SORT_STRING);
$tmpStr = implode( $tmpArr );
$tmpStr = sha1( $tmpStr );
if( $tmpStr == $signature ){
echo $echostr;
}else{
return false;
}
}
checkSignature();
?>
使用java代码,参考:
类库:https://github.com/Wechat-Group/WxJava
引入依赖
<dependency>
<groupId>com.github.binarywang</groupId>
<artifactId>wx-java-mp-spring-boot-starter</artifactId>
<version>4.6.0</version>
</dependency>
<dependency>
<groupId>com.github.binarywang</groupId>
<artifactId>weixin-java-cp</artifactId>
<version>4.6.0</version>
</dependency>
package com.ruoyi.moviemonitor.controller;
import cn.hutool.json.JSONObject;
import com.ruoyi.common.annotation.Anonymous;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.moviemonitor.domain.DoubanInfo;
import com.ruoyi.moviemonitor.service.IDoubanInfoService;
import com.ruoyi.moviemonitor.service.SpiderService;
import com.ruoyi.moviemonitor.spider.SpiderExecution;
import com.ruoyi.moviemonitor.spider.impl.douban.DoubanBasicSpider;
import me.chanjar.weixin.common.error.WxErrorException;
import me.chanjar.weixin.mp.api.WxMpMessageRouter;
import me.chanjar.weixin.mp.api.WxMpService;
import me.chanjar.weixin.mp.bean.message.WxMpXmlMessage;
import me.chanjar.weixin.mp.bean.message.WxMpXmlOutMessage;
import me.chanjar.weixin.mp.bean.message.WxMpXmlOutTextMessage;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.net.http.HttpResponse;
import java.util.List;
@RestController
@Anonymous
@RequestMapping("/weixin")
public class WeixinController {
@Autowired
private WxMpService wxService;
@Autowired
private IDoubanInfoService doubanInfoService;
Logger logger = LoggerFactory.getLogger(WeixinController.class);
// @GetMapping("/auth")
// public String test() throws WxErrorException {
// // this.mpService.getWxMpConfigStorage().getAppId();
// return this.wxService.getAccessToken();
// }
@ResponseBody
@GetMapping(value = "/auth",produces = "text/plain;charset=utf-8")
public String authGet(
@RequestParam(name = "signature",
required = false) String signature,
@RequestParam(name = "timestamp",
required = false) String timestamp,
@RequestParam(name = "nonce", required = false) String nonce,
@RequestParam(name = "echostr", required = false) String echostr) {
this.logger.info("\n接收到来自微信服务器的认证消息:[{}, {}, {}, {}]", signature,
timestamp, nonce, echostr);
if (StringUtils.isAnyBlank(signature, timestamp, nonce, echostr)) {
throw new IllegalArgumentException("请求参数非法,请核实!");
}
if (this.wxService.checkSignature(timestamp, nonce, signature)) {
return echostr;
}
return "非法请求";
}
}