说明:上面是基于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
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。
文章由极客之音整理,本文链接:https://www.bmabk.com/index.php/post/16427.html