使用第三方SDK做微信授权获取用户信息

说明:上面是基于3.8.0

这里3.9.0在授权上做了一个修改:

github上有个说明:抽取oauth2相关接口方法到独立类WxOAuth2Service中

package com.example.wxsdkuse.config;

import me.chanjar.weixin.mp.api.WxMpService;
import me.chanjar.weixin.mp.api.WxOAuth2Service;
import me.chanjar.weixin.mp.api.impl.WxMpServiceImpl;
import me.chanjar.weixin.mp.api.impl.WxOAuth2ServiceImpl;
import me.chanjar.weixin.mp.config.WxMpConfigStorage;
import me.chanjar.weixin.mp.config.impl.WxMpDefaultConfigImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @author 朝花不迟暮
 * @version 1.0
 * @date 2020/9/29 9:46
 */
@Configuration
public class WxMpConfig
{
    @Autowired
    private AccountConfig accountConfig;

    @Bean
    public WxMpConfigStorage wxMpConfigStorage(){
        WxMpDefaultConfigImpl config = new WxMpDefaultConfigImpl();
        config.setAppId(accountConfig.getAppId());
        config.setSecret(accountConfig.getSecret());
        return config;
    }

    @Bean
    public WxMpService wxMpService(){
        WxMpServiceImpl service = new WxMpServiceImpl();
        service.setWxMpConfigStorage(wxMpConfigStorage());
        return service;
    }
    
	//多配置一个WxOAuth2Service
    @Bean
    public WxOAuth2Service wxOAuth2Service(){
        WxOAuth2ServiceImpl auth2Service = new WxOAuth2ServiceImpl(wxMpService());
        return auth2Service;
    }
}

使用:

package com.example.wxsdkuse.controller;

import me.chanjar.weixin.common.api.WxConsts;
import me.chanjar.weixin.common.error.WxErrorException;
import me.chanjar.weixin.mp.api.WxOAuth2Service;
import me.chanjar.weixin.mp.bean.result.WxMpOAuth2AccessToken;
import me.chanjar.weixin.mp.bean.result.WxMpUser;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import sun.util.resources.CalendarData;

/**
 * @author 朝花不迟暮
 * @version 1.0
 * @date 2020/9/29 9:51
 */
@Controller
public class WxController
{
    private final Logger log = LoggerFactory.getLogger(this.getClass());
    @Autowired
    WxOAuth2Service wxOAuth2Service;

    @GetMapping("/index")
    public String index()
    {
        return "hello";
    }

    @GetMapping("/authorize")
    public String authorize(String url)
    {
        String redirectUrl = "http://zhbcm.free.idcfengye.com/userInfo";
        String authorizationUrl = wxOAuth2Service.buildAuthorizationUrl(redirectUrl, WxConsts.OAuth2Scope.SNSAPI_USERINFO, url);
        log.info("authorizationUrl:{}", authorizationUrl);
        return "redirect:" + authorizationUrl;
    }

    @GetMapping("/userInfo")
    public String userInfo(@RequestParam("code") String code,
                           @RequestParam("state") String state)
    {
        log.info("code:{}", code);
        log.info("state:{}", state);
        WxMpOAuth2AccessToken token;
        try
        {
            token = wxOAuth2Service.getAccessToken(code);
            String openId = token.getOpenId();//用户openID
            log.info("openId:{}", openId);
            WxMpUser user = wxOAuth2Service.getUserInfo(token, "zh_CN");
            log.info("user:{}", user);
        } catch (WxErrorException e)
        {
            e.printStackTrace();
        }
        return "redirect:"+state;
    }
}

微信开发者工具输入:

[http://zhbcm.free.idcfengye.com/authorize?url=‘http://zhbcm.free.idcfengye.com/index’](

说明

由于图片的问题,如果想看完整的,请看下面码云,里面有源代码与详细的文档说明(非常详细),或者看视频教程(半个小时)

详情请看码云:
https://gitee.com/thirtyleo/java_training_ground/tree/master/Character4/WX
哔哩哔哩视频教程:
https://www.bilibili.com/video/BV1Q54y1y7Fg?t=860

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值