在微信相关开发中,获取用户openId是很多业务的基础,比如用户身份识别、账号绑定等场景都会用到。下面我们就一步步实现Spring Boot项目引入weixin-java-mp获取用户微信openId的功能。

一、添加weixin-java-mp依赖
首先在Spring Boot项目的pom.xml文件中添加weixin-java-mp的Maven依赖,这里使用稳定的版本即可:
<dependency>
<groupId>com.github.binarywang</groupId>
<artifactId>weixin-java-mp</artifactId>
<version>4.4.0</version>
</dependency>二、配置微信公众平台参数
在application.yml中添加微信公众平台的相关配置,需要提前在微信公众平台获取到appId和appSecret:
wx:
mp:
app-id: 你的公众号appId
secret: 你的公众号appSecret
token: 你的公众号token
aes-key: 你的公众号aesKey三、配置WxMpService实例
创建配置类,初始化WxMpService实例,后续所有微信相关的操作都通过这个实例完成:
import me.chanjar.weixin.mp.api.WxMpService;
import me.chanjar.weixin.mp.api.impl.WxMpServiceImpl;
import me.chanjar.weixin.mp.config.WxMpConfigStorage;
import me.chanjar.weixin.mp.config.impl.WxMpDefaultConfigImpl;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class WxMpConfig {
@Value("${wx.mp.app-id}")
private String appId;
@Value("${wx.mp.secret}")
private String secret;
@Bean
public WxMpService wxMpService() {
WxMpService service = new WxMpServiceImpl();
WxMpDefaultConfigImpl config = new WxMpDefaultConfigImpl();
config.setAppId(appId);
config.setSecret(secret);
service.setWxMpConfigStorage(config);
return service;
}
}四、实现微信授权获取openId逻辑
微信授权获取openId需要两步,第一步引导用户跳转微信授权页面,第二步通过回调的code换取openId:
1. 生成微信授权跳转链接
用户访问这个接口后,会跳转到微信的授权页面,授权完成后会回调到我们指定的redirectUri:
import me.chanjar.weixin.mp.api.WxMpService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
@RestController
public class WxAuthController {
@Autowired
private WxMpService wxMpService;
// 授权跳转地址,redirectUri需要是你在公众平台配置的授权回调域名下的地址
private static final String REDIRECT_URI = "http://ipipp.com/wx/callback";
@GetMapping("/wx/auth")
public String auth() {
// 构造授权url,scope使用snsapi_base可以直接获取openId不需要用户手动授权
String url = wxMpService.getOAuth2Service().buildAuthorizationUrl(
URLEncoder.encode(REDIRECT_URI, StandardCharsets.UTF_8),
"snsapi_base",
"state"
);
return "redirect:" + url;
}
}2. 回调接口获取openId
用户授权后,微信会携带code跳转到我们的回调地址,通过这个code就可以换取用户的openId:
import me.chanjar.weixin.mp.api.WxMpService;
import me.chanjar.weixin.mp.bean.result.WxMpOAuth2AccessToken;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class WxAuthController {
@Autowired
private WxMpService wxMpService;
@GetMapping("/wx/callback")
public String callback(@RequestParam("code") String code) {
try {
// 通过code获取access token,里面包含openId
WxMpOAuth2AccessToken accessToken = wxMpService.getOAuth2Service().getAccessToken(code);
String openId = accessToken.getOpenId();
// 这里可以处理自己的业务逻辑,比如绑定用户账号等
return "获取到的用户openId是:" + openId;
} catch (Exception e) {
e.printStackTrace();
return "获取openId失败:" + e.getMessage();
}
}
}五、注意事项
- 公众号需要在公众平台配置授权回调域名,否则无法完成授权流程
- 如果使用测试公众号,需要在测试号管理页面配置回调域名
- snsapi_base权限只能获取openId,如果需要获取用户昵称、头像等信息,需要使用snsapi_userinfo权限,且需要用户手动同意授权
- 生产环境中redirectUri建议使用https协议,避免传输过程中信息泄露
Spring_Bootweixin-java-mp微信openId微信授权修改时间:2026-05-31 05:05:48