网页授权 | 微信开放文档 

公众号测试:

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;
    }
}|

Logo

魔乐社区(Modelers.cn) 是一个中立、公益的人工智能社区,提供人工智能工具、模型、数据的托管、展示与应用协同服务,为人工智能开发及爱好者搭建开放的学习交流平台。社区通过理事会方式运作,由全产业链共同建设、共同运营、共同享有,推动国产AI生态繁荣发展。

更多推荐