第一步:先去微信开放平台进行创建移动应用:微信开放平台 如下图:
第二步:新建一个yml资源文件(application-loc.yml)名称可以自己改,如下图:
application-loc.yml内容:
## 微信用户授权信息
wxUserInfo:
appid: *******
appSecret : *******
## 获取accessToken(https://api.weixin.qq.com/sns/oauth2/access_token?appid=APPID&secret=SECRET&code=CODE&grant_type=authorization_code)
getAccessToken: https://api.weixin.qq.com/sns/oauth2/access_token
## 校验accoessToken是否有效(https://api.weixin.qq.com/sns/auth?access_token=ACCESS_TOKEN&openid=OPENID)
authToken: https://api.weixin.qq.com/sns/auth
## 刷新accessToken(https://api.weixin.qq.com/sns/oauth2/refresh_token?appid=APPID&grant_type=refresh_token&refresh_token=REFRESH_TOKEN)
refreshAccessToken: https://api.weixin.qq.com/sns/oauth2/refresh_token
## 获取用户信息(https://api.weixin.qq.com/sns/userinfo?access_token=ACCESS_TOKEN&openid=OPENID)
getUserinfo: https://api.weixin.qq.com/sns/userinfo
第三步:controller代码,code是前端传过来的哈
/**
* @description: 授权微信登录
* @param code 授权code
* @return: 返回用户信息
*/
@Login(false)
@ApiOperation("授权微信登录")
@GetMapping("/weChatAuthorizateInfo")
public ResultVO<WeChatInfoDto> weChatAuthorizateInfo(@ApiParam(name = "code", value = "微信授权code") @RequestParam(name = "code") String code) {
if (StringUtils.isEmpty(code)) {
return ResultVO.error("授权code不能为空!");
}
return loginService.weChatAuthorizateInfo(code);
}
第四步:server代码
// 微信appid
@Value("${wxUserInfo.appid}")
private String appid;
// 微信appSecret
@Value("${wxUserInfo.appSecret}")
private String appSecret;
// 获取accessToken
@Value("${wxUserInfo.getAccessToken}")
private String getAccessToken;
// 校验accoessToken是否有效
@Value("${wxUserInfo.authToken}")
private String authToken;
// 刷新accessToken
@Value("${wxUserInfo.refreshAccessToken}")
private String refreshAccessToken;
// 获取用户信息
@Value("${wxUserInfo.getUserinfo}")
private String getUserinfo;
/**
* @description: 授权微信登录
* @param code 授权code
* @return:
*/
@Override
public ResultVO<WeChatInfoDto> weChatAuthorizateInfo(String code) {
// 获取accessToken
String getAccessTokenParam = "appid=" + appid + "&secret=" + appSecret + "&code=" + code + "&grant_type=authorization_code";
JSONObject getAccessTokenRes = HttpUtil.sendGet(getAccessToken, getAccessTokenParam);
if (getAccessTokenRes.containsKey("errcode")) {
log.error("获取微信accessToken失败!" + getAccessTokenRes.getString("errmsg"));
return ResultVO.error("授权失败!");
}
// 校验accessToken是否有效
String access_token = getAccessTokenRes.getString("access_token");
String openid = getAccessTokenRes.getString("openid");
String validateAccessTokenParam = "access_token=" + access_token + "&openid=" + openid;
JSONObject authTokenRes = HttpUtil.sendGet(authToken, validateAccessTokenParam);
if (authTokenRes.getInteger("errcode").intValue() != 0) {
// 刷新token
String refresh_token = getAccessTokenRes.getString("refresh_token");
String refreshTokenParam = "appid=" + appid + "&grant_type=refresh_token&refresh_token=" + refresh_token;
JSONObject refreshTokenRes = HttpUtil.sendGet(refreshAccessToken, refreshTokenParam);
if (refreshTokenRes.containsKey("errcode")) {
log.error("刷新微信accessToken失败!" + refreshTokenRes.getString("errmsg"));
return ResultVO.error("授权失败!");
}
access_token = refreshTokenRes.getString("access_token");
openid = refreshTokenRes.getString("openid");
}
// 获取授权用户信息
String getUserinfoParam = "access_token=" + access_token + "&openid=" + openid+"&lang=zh_CN";
JSONObject getUserinfoRes = HttpUtil.sendGetEncode(getUserinfo, getUserinfoParam);
if (getUserinfoRes.containsKey("errcode")) {
log.error("获取微信accessToken失败!" + getAccessTokenRes.getString("errmsg"));
return ResultVO.error("获取授权用户信息失败!");
}
// 返回授权结果
WeChatInfoDto weChatInfoDto = JSON.parseObject(getUserinfoRes.toJSONString(), WeChatInfoDto.class);
log.info("微信授权用户信息:" + JSONObject.toJSONString(weChatInfoDto));
return ResultVO.success("授权成功!", weChatInfoDto);
}
ResultVO返回类
package com.sport.sportcloudmarathonh5.vo;
import java.io.Serializable;
/**
* @author zdj
* @version 1.0
* @date 2021-10-31 19:42:33
*/
public class ResultVO<T> implements Serializable {
private Integer code;
private String message;
private T data;
private static <T> ResultVO<T> getInstance() {
return new ResultVO<T>();
}
public static <T> ResultVO<T> token() {
return response(300, "请登录", null);
}
public static <T> ResultVO<T> success() {
return response(200, "success", null);
}
public static <T> ResultVO<T> success(T data) {
return response(200, "success", data);
}
public static <T> ResultVO<T> success(String message, T data) {
return response(200, message, data);
}
public static <T> ResultVO<T> error() {
return response(500, "服务器异常", null);
}
public static <T> ResultVO<T> error(String message) {
return response(500, message, null);
}
public static <T> ResultVO<T> error(String message, T data) {
return response(500, message, data);
}
public static <T> ResultVO<T> exp() {
return response(500, "服务器异常", null);
}
public static <T> ResultVO<T> response(Integer code, String message, T data) {
ResultVO<T> instance = getInstance();
instance.setCode(code);
instance.setMessage(message);
instance.setData(data);
return instance;
}
public Integer getCode() {
return code;
}
public ResultVO<T> setCode(Integer code) {
this.code = code;
return this;
}
public String getMessage() {
return message;
}
public ResultVO<T> setMessage(String message) {
this.message = message;
return this;
}
public T getData() {
return data;
}
public ResultVO<T> setData(T data) {
this.data = data;
return this;
}
}
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。
文章由极客之音整理,本文链接:https://www.bmabk.com/index.php/post/137445.html