1.在 pom.xml 中添加微信开发 SDK 依赖:
<dependency>
<groupId>com.github.binarywang</groupId>
<artifactId>weixin-java-mp</artifactId>
<version>3.5.0</version>
</dependency>
2.创建一个处理微信消息的 Controller:
@RestController
@RequestMapping("/wx/portal")
public class WxPortalController {
private final WxMpService wxService;
public WxPortalController(WxMpService wxService) {
this.wxService = wxService;
}
@GetMapping(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) {
if (StringUtils.isAnyBlank(signature, timestamp, nonce, echostr)) {
throw new IllegalArgumentException("请求参数非法,请核实!");
}
if (this.wxService.checkSignature(timestamp, nonce, signature)) {
return echostr;
}
return "非法请求";
}
@PostMapping(produces = "application/xml; charset=UTF-8")
public String post(@RequestBody String requestBody, @RequestParam("signature") String signature,
@RequestParam("timestamp") String timestamp, @RequestParam("nonce") String nonce,
@RequestParam(name = "encrypt_type", required = false) String encType,
@RequestParam(name = "msg_signature", required = false) String msgSignature) {
if (!this.wxService.checkSignature(timestamp, nonce, signature)) {
throw new IllegalArgumentException("非法请求,可能属于伪造的请求!");
}
String out = null;
if (encType == null) {
// 明文传输的消息
WxMpXmlMessage inMessage = WxMpXmlMessage.fromXml(requestBody);
WxMpXmlOutMessage outMessage = this.route(inMessage);
if (outMessage == null) {
return "";
}
out = outMessage.toXml();
} else if ("aes".equals(encType)) {
// aes加密的消息
WxMpXmlMessage inMessage = WxMpXmlMessage.fromEncryptedXml(requestBody, this.wxService.getWxMpConfigStorage(), timestamp, nonce, msgSignature);
WxMpXmlOutMessage outMessage = this.route(inMessage);
if (outMessage == null) {
return "";
}
out = outMessage.toEncryptedXml(this.wxService.getWxMpConfigStorage());
}
return out;
}
private WxMpXmlOutMessage route(WxMpXmlMessage message) {
try {
return this.wxService.route(message);
} catch (Exception e) {
log.error(e.getMessage(), e);
}
return null;
}
}
在上述代码中,我们创建了一个 WxPortalController 控制器类,在其中添加了两个方法分别用于处理 GET 请求和 POST 请求。这些方法接受微信发送过来的参数,并使用 WxMpService 对象进行验证和处理。其中,WxMpService 是对微信公众号相关接口进行封装的类,我们可以通过依赖注入的方式获取它的实例。
3.在 application.yml 中配置微信相关的信息:
wx:
mp:
app-id: your-app-id
secret: your-app-secret
token: your-token
aes-key: your-aes-key
在上述配置中,我们使用了 wx.mp 作为微信公众号相关配置的前缀,并在下面分别设置了 AppID、AppSecret、Token 和 AES Key 的值。这些值可以在微信公众平台中获取到。
5.启动 Spring Boot 应用程序,并将接口地址填写到微信公众平台中的开发者中心中。
例如,假设你的应用程序运行在 8080 端口,接口地址为 /wx/portal,那么你就可以将 http://your-domain.com/wx/portal 填写到微信公众平台中的开发者中心的 URL 配置中。
微信网页授权流程主要包括以下几个步骤:
1.构造授权链接:生成一个跳转到微信授权页面的链接,用户点击后将跳转到微信授权页面。
2.用户授权:用户在微信授权页面上确认授权登录,并同意授权给你的应用获取用户信息。
3.获取授权 code:微信授权页面重定向回你指定的 redirect_uri 并携带授权 code。
4.通过 code 获取 access_token:使用授权 code 调用微信接口,获取 access_token 和 openid。
5.获取用户信息:使用 access_token 和 openid 调用微信接口,获取用户的基本信息。
下面是在 Spring 中实现微信网页授权流程的示例代码:
1.构造授权链接:
String appId = "your_app_id";
String redirectUri = "your_redirect_uri";
String scope = "snsapi_userinfo";
String state = "your_state_parameter"; // 可选,可以用于防止 CSRF 攻击
String authorizeUrl = "https://open.weixin.qq.com/connect/oauth2/authorize?appid=" + appId + "&redirect_uri=" + URLEncoder.encode(redirectUri, "UTF-8") + "&response_type=code&scope=" + scope + "&state=" + state + "#wechat_redirect";
// 将 authorizeUrl 返回给前端,让用户点击该链接进行授权
2.获取授权 code:
// 在 redirectUri 对应