微信公众号网页授权(springboot)
第一步在微信公众平台中找到网页授权,若没有微信公众号,可以用测试号来进行
官方链接: http://mp.weixin.qq.com/debug/cgi-bin/sandbox?t=sandbox/login
其中Token可以随便写,可能一开始配置的URL无法配置成功,别着急,按照我下面的步骤操作
在“网页授权获取用户基本信息”那一栏,点击修改
然后再填写授权回调页面,这里的页面如同手册里面一样 以xxx.xxxxx.com 即可 而且此处的域名一定要和代码中回调地址在同一个域名之下。自己测试可以去找花生壳等申请一个域名做内网穿透,详情请自己百度.
[不要包含http://或https://] 比如在下面填写的网址是在花生壳中申请的: http://abc123.ticp.io, 那么在下面应该填写abc123.ticp.io, 这个url在接下来的代码中会用到。
springboot整合微信公众号授权
1. 依赖pom.xml
<!--微信授权-->
<dependency>
<groupId>com.github.binarywang</groupId>
<artifactId>weixin-java-mp</artifactId>
<version>2.9.0</version>
</dependency>
2. application.yml
wechat:
appId: xxxxxxxxxxxxxxx
appSecret: xxxxxxxxxxxxxxxxxxxxxxxxx
3. 加载配置文件,获取appId和appSecret
package com.wechat.auth;
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;
@Data
@Component
@ConfigurationProperties(prefix = "wechat")
public class WechatAccountConfig {
private String appId;
private String appSecret;
}
package com.wechat.auth;
import me.chanjar.weixin.mp.api.WxMpConfigStorage;
import me.chanjar.weixin.mp.api.WxMpInMemoryConfigStorage;
import me.chanjar.weixin.mp.api.WxMpService;
import me.chanjar.weixin.mp.api.impl.WxMpServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Component;
@Component
public class WeChatMpConfig {
@Autowired
private WechatAccountConfig wechatAccountConfig;
@Bean
public WxMpService wxMpService(){
WxMpService wxMpService = new WxMpServiceImpl();
wxMpService.setWxMpConfigStorage(wxMpConfigStorage());
return wxMpService;
}
@Bean
public WxMpConfigStorage wxMpConfigStorage(){
WxMpInMemoryConfigStorage wxMpConfigStorage = new WxMpInMemoryConfigStorage();
wxMpConfigStorage.setAppId(wechatAccountConfig.getAppId());
wxMpConfigStorage.setSecret(wechatAccountConfig.getAppSecret());
return wxMpConfigStorage;
}
}
4.controller授权
package com.wechat.auth;
import lombok.extern.slf4j.Slf4j;
import me.chanjar.weixin.common.api.WxConsts;
import me.chanjar.weixin.common.exception.WxErrorException;
import me.chanjar.weixin.mp.api.WxMpService;
import me.chanjar.weixin.mp.bean.result.WxMpOAuth2AccessToken;
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.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import java.net.URLEncoder;
@Controller
@RequestMapping("/wechat")
@Slf4j
public class WechatController {
@Autowired
private WxMpService wxMpService;
@GetMapping("/authorize")
public String authorize(@RequestParam("returnUrl") String returnUrl) {
String url = "http://abc123.ticp.io/wechat/userInfo"; //可以看到,这里的url即为上面申请并授权的域名
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) throws Exception {
log.info("微信网页授权: code={}", code);
log.info("微信网页授权: state={}",returnUrl); //即authorize中的returnUrl
WxMpOAuth2AccessToken wxMpOAuth2AccessToken;
try {
wxMpOAuth2AccessToken = wxMpService.oauth2getAccessToken(code);
}catch (WxErrorException e) {
log.info("微信网页授权: {}", e);
throw new Exception(e.getError().getErrorMsg());
}
String openId = wxMpOAuth2AccessToken.getOpenId();
log.info("微信网页授权: openId={}", openId);
return "redirect:" + returnUrl;
}
}
5.以下功能可根据需求来用
- 获得用户基本信息
WxMpUser wxMpUser = wxMpService.oauth2getUserInfo(wxMpOAuth2AccessToken, null);
- 刷新access token
wxMpOAuth2AccessToken = wxMpService.oauth2refreshAccessToken(wxMpOAuth2AccessToken.getRefreshToken());
- 验证access token
boolean valid = wxMpService.oauth2validateAccessToken(wxMpOAuth2AccessToken);