Java实现微信授权 获取用户OpenID(简单易实现)
二、打开说明文档,
三、首先构造网页授权url,然后构成超链接让用户点击
@Data
@Component
@ConfigurationProperties(prefix = "wechat")
public class WechatAccountConfig {
private String mpAppId;
private String mpAppSecret;
}
@Configuration
public class WeChatMpConfig {
@Autowired
private WechatAccountConfig accountConfig;
@Bean
public WxMpService wxMpService() {
WxMpService wxMpService = new WxMpServiceImpl();
wxMpService.setWxMpConfigStorage(wxMpConfigStorage());
return wxMpService;
}
@Bean
public WxMpConfigStorage wxMpConfigStorage() {
WxMpInMemoryConfigStorage wxMpConfigStorage = new WxMpInMemoryConfigStorage();
wxMpConfigStorage.setAppId(accountConfig.getMpAppId());
wxMpConfigStorage.setSecret(accountConfig.getMpAppSecret());
return wxMpConfigStorage;
}
}
编写我们的控制类,用来获取openId的类
点击后会跳转到百度首页,此时看idea控制台输出以下内容
此时我们就拿到了OpenId,为了验证我们还可以复制此时的百度首页链接
https://www.baidu.com/?openid=oLiOO54bZfDYSSC87zwn7oml9h88
会看到链接地址中也包含了OpenId
