springboot 公众号实现获取用户openid

网页授权 | 微信开放文档 

公众号测试:

https://mp.weixin.qq.com/debug/cgi-bin/sandboxinfo?action=showinfo&t=sandbox/index

网页帐号——>网页授权获取用户基本信修改你的域名

可以自己找个内网穿透的工具测试

参考链接(请在微信客户端中打开此链接体验):

scope为snsapi_base:
https://open.weixin.qq.com/connect/oauth2/authorize?appid=你的appid&redirect_uri=你的域名加回调地址(https://你的域名/回调地址)&response_type=code&scope=snsapi_base&state=123#wechat_redirect
scope为snsapi_userinfo:
https://open.weixin.qq.com/connect/oauth2/authorize?appid=你的appid&redirect_uri=你的域名加回调地址(https://你的域名/回调地址)&response_type=code&scope=snsapi_userinfo&state=123#wechat_redirect

        <dependency>
            <groupId>com.github.binarywang</groupId>
            <artifactId>weixin-java-mp</artifactId>
             //选择你的版本
            <version>4.4.0</version>
        </dependency>

scope两种方式:snsapi_base和snsapi_userinfo,一个只会返回openid,一个可以获取用户信息包括openid,需要用户确认才可以获取

下面是scope为snsapi_userinfo返回的数据。有openid、微信昵称、头像。其他敏感信息现在是不会返回了,只是空字符串

点击确认之后不同在 确认直接可以获取

未确认: 

返回的数据:

 

 配置微信config。编写WxMpServiceConfig类,主要用来将值set到 WxMpService,而WxMpService就是weixin.mp.api提供的

注意@RequiredArgsConstructor这个是lombok提供的,类似于@Autowired、@Resource,只需要在使用时加入final,就会自动注入。有兴趣可以自己了解下。

private final WxPayV3Bean wxPayV3Bean;

wxMpDefaultConfig.setAppId(wxPayV3Bean.getAppId());
wxMpDefaultConfig.setSecret(wxPayV3Bean.getSecret());

@Configuration
@RequiredArgsConstructor
public class WxMpServiceConfig {
    

    @Bean
    public WxMpService wxMpService() {
        WxMpService wxMpService = new WxMpServiceImpl();
        wxMpService.setWxMpConfigStorage(wxMpConfigStorage());
        return wxMpService;
    }

    @Bean
    public WxMpConfigStorage wxMpConfigStorage() {
        WxMpDefaultConfigImpl wxMpDefaultConfig = new WxMpDefaultConfigImpl();
        wxMpDefaultConfig.setAppId(你的appid);
        wxMpDefaultConfig.setSecret(你的secret);
       
        return wxMpDefaultConfig;
    }

}
import me.chanjar.weixin.common.bean.WxOAuth2UserInfo;
import me.chanjar.weixin.common.bean.oauth2.WxOAuth2AccessToken;
import me.chanjar.weixin.common.service.WxOAuth2Service;
import me.chanjar.weixin.mp.api.WxMpService;

@RestController
@RequestMapping("/wechat")
@CrossOrigin
@RequiredArgsConstructor
public class WxMpOauthController {
private final WxMpService wxMpService;

@SneakyThrows(Exception.class)//简化try catch
    @GetMapping("/auth")
    @ApiOperation("回调-授权登录同意")
    public WxOAuth2UserInfo callback(@RequestParam("code") String code) {
        WxOAuth2Service oAuth2Service = this.wxMpService.switchoverTo(appid).getOAuth2Service();
        WxOAuth2AccessToken wxOAuth2AccessToken = oAuth2Service.getAccessToken(code);
        String accessToken = wxOAuth2AccessToken.getAccessToken();
        String openId = wxOAuth2AccessToken.getOpenId();
        log.info("[微信公众号] 授权回调 accessToken:[{}]", accessToken);
        log.info("[微信公众号] 授权回调 openId:[{}]", openId);

        WxOAuth2UserInfo userInfo = oAuth2Service.getUserInfo(wxOAuth2AccessToken, "zh_CN");
        log.info("[微信公众号] 授权回调 用户信息:[{}]", JSONUtil.toJsonStr(userInfo));
        return userInfo;
    }
}|

  • 7
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值