springboot-微信授权登录(基础)2020.6

直奔主题,以最简单的方式

1. 1内外网穿透

推荐使用utools,安装…添加插件
springboot-微信授权登录(基础)2020.6_第1张图片

1.2 配置一下,超级简单的(如下)

springboot-微信授权登录(基础)2020.6_第2张图片

2. 微信公众号

2.1 首先得有一个测试公众号:https://mp.weixin.qq.com/debug/cgi-bin/sandbox?t=sandbox/login

登录后
springboot-微信授权登录(基础)2020.6_第3张图片

2.2 找到公众号二维码(扫码关注)

springboot-微信授权登录(基础)2020.6_第4张图片

2.3 找到网页服务(配置网页账号)

在这里插入图片描述
点击修改,填入你的授权回调页面的域名,这个域名就是1.2生成的穿透地址(去除https://),下图与1.2 不一致,因为我做了两个穿透地址
springboot-微信授权登录(基础)2020.6_第5张图片
穿透地址和微信公众号都准备好了的情况下,就要开始编码了

3.编码

建项目过程就省略了(这边以最简单的方式编码)

3.1 pom依赖

		<dependency>
            <groupId>com.github.binarywang</groupId>
            <artifactId>weixin-java-mp</artifactId>
            <version>3.7.0</version>
        </dependency>

3.2 配置文件

appId和appSecret是2.1生成的
下面代码,如果WxMpInMemoryConfigStorage 报错,就换成WxMpDefaultConfigImpl

@Component
public class WechatMpConfig {
    @Bean
    public WxMpService wxMpService(){
        WxMpService wxMpService = new WxMpServiceImpl();
        wxMpService.setWxMpConfigStorage(wxMpConfigStorage());
        return wxMpService;
    }
    @Bean
    public WxMpConfigStorage wxMpConfigStorage(){
        WxMpInMemoryConfigStorage wxMpConfigStorage = new WxMpInMemoryConfigStorage();
        // WxMpDefaultConfigImpl wxMpConfigStorage = new WxMpDefaultConfigImpl();
        wxMpConfigStorage.setAppId("xxxxxxxxxxxxxxxxx");
        wxMpConfigStorage.setSecret("xxxxxxxxxxxxxxxx");
        return wxMpConfigStorage;
    }
}

3.3 Controller类

其中有一些import没有粘贴出来,导入正确的依赖,直接无脑 import class(快捷键:Alt+Enter)
流程说明:
1. 微信访问授权接口 /authorize
2. 会弹出一个确认按钮,点击授权
3. 授权之后,微信公众号会回过头调用 /userInfo 接口,这里就是前面为什么要实现内网穿透,你的项目地址是127.0.0.1:port,微信服务器肯定访问不了,通过内网穿透,将127.0.0.1:port映射成http://test.cn1.utools.club,微信服务器就可以访问了
4. 下面代码中url = http://test.cn1.utools.club/wechat/userInfo,实质上就是127.0.0.1:port/wechat/userInfo ,只是为了让微信服务器能够访问你的项目地址
5.returnUrl就是让你授权完成后,拿到用户信息后,最后回调的地址,这里我就让他最后跳转至http://baidu.com

@Controller
@RequestMapping("/wechat")
@Slf4j
public class WechatController {
    @Autowired
    private WxMpService wxMpService;
    @GetMapping("/authorize")
    public String authorize(@RequestParam("returnUrl") String returnUrl){
        String url = "http://test.cn1.utools.club/wechat/userInfo";
        String redirectUrl = wxMpService.oauth2buildAuthorizationUrl(url, WxConsts.OAuth2Scope.SNSAPI_USERINFO, URLEncoder.encode(returnUrl));
        log.info("微信网页授权获取code,redirectUrl={}",redirectUrl);
        return "redirect:" + redirectUrl;
    }

/***********************************************************************************/
    @GetMapping("/userInfo")
    public String userInfo(@RequestParam("code") String code,@RequestParam("state") String returnUrl){
        //拿code去获取AccessToken
        WxMpOAuth2AccessToken wxMpOAuth2AccessToken = new WxMpOAuth2AccessToken();
        try {
            wxMpOAuth2AccessToken = wxMpService.oauth2getAccessToken(code);
            WxMpUser wxMpUser = wxMpService.oauth2getUserInfo(wxMpOAuth2AccessToken, "xx");
            log.info("【微信网页授权】user={}", wxMpUser.toString());
        } catch (WxErrorException e) {
            log.info("[微信网页授权] {}",e);
        }
        //获取openid
        String openId = wxMpOAuth2AccessToken.getOpenId();
        log.info(openId);
        return "redirect:" + returnUrl+"?openid=" + openId;
    }
}

上述过程的原理我就不介绍了,直接点进去看源码就ojbk了

3.4 微信访问授权接口(开始见证奇迹)

http://test.cn1.utools.club/wechat/authorize?returnUrl=http://biadu.com
springboot-微信授权登录(基础)2020.6_第6张图片
(要先关注公众号,上述2.2)点击允许,最后会跳到百度去,说明授权成功了

你可能感兴趣的:(springboot)