The first step is to apply for a public account
Enter https://mp.weixin.qq.com/ to apply for a subscription number, anyone can apply
Check the interface permissions after entering
! [Please add a picture description](https://img-blog.csdnimg.cn/a325986c64674714ab1b6bbf854c0fdf.png = 600x400)
These important interface functions are only available for the authentication service account, but we can apply for a test account.
Click Developer Tools = "Test Account
Note: Submit here requires the backend to start the service and call the interface all the time to succeed. If you don’t understand, you can see the server configuration below and then submit the test! Click to see the configuration below
If you debug locally, you need to open an intranet penetration first .
Fill in url and token here.
url=intranet penetration ip+/wx/portal/public
Token=write whatever you want
The basics of official account development , it is recommended to learn the tutorial link内网穿透
first
For users who need to obtain user permissions (avatar, nickname), you need to configure something.
Just fill in your penetration address, and you don’t need to add a protocol.
The second step backend server configuration
1. pom.xml introduces the corresponding dependencies
<!-- 基于Spring Boot 和 WxJava 实现的微信公众号Java后端Demo,支持多公众号 -->
<dependency>
<groupId>com.github.binarywang</groupId>
<artifactId>weixin-java-mp</artifactId>
<version>4.5.0</version>
</dependency>
<!-- 其他的依赖省略。。比如springbootweb之类的必须要配置 -->
The dependency comes from Binary Wang , and there are specific usage methods in his github, you can check it out
2. Go to application.yml related configuration
wx:
mp:
# callback:如果本地填写穿透地址,如果是服务器填写服务器地址
callback: http://sysm8w.natappfree.cc
configs:
- appId: wx71axxxx318444e6 # 第一个公众号的appid
secret: 11f8d45xxxxxxxxc8c60dd3df48aa9 # 公众号的appsecret
token: abcc # 对应接口配置里的Token值
aesKey: sha1 # 接口配置里的EncodingAESKey值,如果是测试号可以不用填写
3. Create the corresponding configuration attribute class
WxMpProperties.java
import cn.hutool.json.JSONUtil;
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import java.util.List;
/**
* wechat mp properties
*
* @author <a href="https://github.com/binarywang">Binary Wang</a>
*/
@Data
@ConfigurationProperties(prefix = "wx.mp")
public class WxMpProperties {
/**
* 是否使用redis存储access token
*/
private boolean useRedis;
/**
* redis 配置
*/
private RedisConfig redisConfig;
@Data
public static class RedisConfig {
/**
* redis服务器 主机地址
*/
private String host;
/**
* redis服务器 端口号
*/
private Integer port;
/**
* redis服务器 密码
*/
private String password;
/**
* redis 服务连接超时时间
*/
private Integer timeout;
}
/**
* 多个公众号配置信息
*/
private List<MpConfig> configs;
@Data
public static class MpConfig {
/**
* 设置微信公众号的appid
*/
private String appId;
/**
* 设置微信公众号的app secret
*/
private String secret;
/**
* 设置微信公众号的token
*/
private String token;
/**
* 设置微信公众号的EncodingAESKey
*/
private String aesKey;
}
@Override
public String toString() {
return JSONUtil.toJsonStr(this);
}
}
4. Create a WeChat interface configuration class
WxMpConfig.java
/**
* 创建并配置一个用于微信公众号操作的 WxMpService 实例。
* 该方法读取配置属性并为每个公众号配置创建一个 WxMpDefaultConfigImpl 实例,
* 将这些实例放入 MultiConfigStorages 中,并返回最终的 WxMpService 实例。
*
* @return WxMpService 实例,用于微信公众号操作
* @throws RuntimeException 如果 wxService 配置异常或 Application 相关配置对象缺失
*/
@Bean
public WxMpService wxService() {
final List<WxMpProperties.MpConfig> configs = wxMpProperties.getConfigs();
// 检查配置是否存在
if (configs == null) {
throw new RuntimeException("wxService配置异常,请检查Application相关配置对象");
}
// 创建 WxMpService 实例
WxMpService service = new WxMpServiceImpl();
// 配置 MultiConfigStorages
service.setMultiConfigStorages(
configs.stream().map(item -> {
WxMpDefaultConfigImpl wxMpDefaultConfig = new WxMpDefaultConfigImpl();
// 设置公众号的 AppId
wxMpDefaultConfig.setAppId(item.getAppId());
// 设置公众号的 Secret
wxMpDefaultConfig.setSecret(item.getSecret());
// 设置公众号的 Token
wxMpDefaultConfig.setToken(item.getToken());
// 设置公众号的 AesKey
wxMpDefaultConfig.setAesKey(item.getAesKey());
return wxMpDefaultConfig;
}).collect(Collectors.toMap(WxMpDefaultConfigImpl::getAppId, a -> a, (o, n) -> o))
);
return service;
}
5. Web layer verification
WxController.java
package com.wwk.usercenter.controller;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import me.chanjar.weixin.mp.api.WxMpService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import javax.annotation.Resource;
import java.util.Map;
/**
* @Author wwk
* @Date 2023/6/6 13:14
*/
@RestController
@RequestMapping("/wx/portal")
@Slf4j
public class WxController {
@Resource
private WxMpService wxService;
@RequestMapping("/public")
public String checkWxServer(@RequestParam Map<String, String> map) {
log.info("开始验证消息是否来自微信服务器,传来的参数{}",map);
String signature = map.get("signature");
String echostr = map.get("echostr");
String timestamp = map.get("timestamp");
String nonce = map.get("nonce");
if (StringUtils.isAnyBlank(signature, echostr, timestamp, nonce)) {
throw new IllegalArgumentException("请求参数非法,请核实!");
}
if (wxService.checkSignature(timestamp, nonce, signature)) {
log.info("消息来自微信服务器,验证成功,signature;{}",signature);
return echostr;
}
log.warn("消息来自其他服务器,非法请求!!!");
return "微信非法请求";
}
}
Note: After configuring all the configurations and starting the service, you can click the submit button of the WeChat interface configuration above to test. A successful configuration means that the verification is passed!