mirror of
https://gitee.com/yadong.zhang/JustAuth.git
synced 2026-01-07 19:31:48 +08:00
QQ登录未获取真正的uuid 部分Request封装AuthUser时缺少gender属性 location属性设置方式不一致 钉钉登录获取用户信息忽略了openid 钉钉登录回调报错 bug
106 lines
4.1 KiB
Java
106 lines
4.1 KiB
Java
package me.zhyd.oauth.request;
|
|
|
|
import cn.hutool.core.util.StrUtil;
|
|
import cn.hutool.http.HttpRequest;
|
|
import cn.hutool.http.HttpResponse;
|
|
import com.alibaba.fastjson.JSONObject;
|
|
import me.zhyd.oauth.config.AuthConfig;
|
|
import me.zhyd.oauth.exception.AuthException;
|
|
import me.zhyd.oauth.model.AuthSource;
|
|
import me.zhyd.oauth.model.AuthToken;
|
|
import me.zhyd.oauth.model.AuthUser;
|
|
import me.zhyd.oauth.model.AuthUserGender;
|
|
import me.zhyd.oauth.utils.GlobalAuthUtil;
|
|
import me.zhyd.oauth.utils.StringUtils;
|
|
import me.zhyd.oauth.utils.UrlBuilder;
|
|
|
|
import java.util.Map;
|
|
|
|
/**
|
|
* qq登录
|
|
*
|
|
* @author yadong.zhang (yadong.zhang0415(a)gmail.com)
|
|
* @author yangkai.shen (https://xkcoding.com)
|
|
* @version 1.0
|
|
* @since 1.8
|
|
*/
|
|
public class AuthQqRequest extends BaseAuthRequest {
|
|
public AuthQqRequest(AuthConfig config) {
|
|
super(config, AuthSource.QQ);
|
|
}
|
|
|
|
@Override
|
|
protected AuthToken getAccessToken(String code) {
|
|
String accessTokenUrl = UrlBuilder.getQqAccessTokenUrl(config.getClientId(), config.getClientSecret(), code, config
|
|
.getRedirectUri());
|
|
HttpResponse response = HttpRequest.get(accessTokenUrl).execute();
|
|
Map<String, String> accessTokenObject = GlobalAuthUtil.parseStringToMap(response.body());
|
|
if (!accessTokenObject.containsKey("access_token")) {
|
|
throw new AuthException("Unable to get token from qq using code [" + code + "]");
|
|
}
|
|
return AuthToken.builder()
|
|
.accessToken(accessTokenObject.get("access_token"))
|
|
.expireIn(Integer.valueOf(accessTokenObject.get("expires_in")))
|
|
.refreshToken(accessTokenObject.get("refresh_token"))
|
|
.build();
|
|
}
|
|
|
|
@Override
|
|
protected AuthUser getUserInfo(AuthToken authToken) {
|
|
String accessToken = authToken.getAccessToken();
|
|
String openIdStr = this.getOpenId(accessToken);
|
|
String[] openIdArr = openIdStr.split("\\|");
|
|
authToken.setOpenId(openIdArr[0]);
|
|
String uuid = authToken.getOpenId();
|
|
if (openIdArr.length == 2) {
|
|
uuid = openIdArr[1];
|
|
}
|
|
|
|
HttpResponse response = HttpRequest.get(UrlBuilder.getQqUserInfoUrl(config.getClientId(), accessToken, authToken.getOpenId()))
|
|
.execute();
|
|
JSONObject object = JSONObject.parseObject(response.body());
|
|
if (object.getIntValue("ret") != 0) {
|
|
throw new AuthException(object.getString("msg"));
|
|
}
|
|
String avatar = object.getString("figureurl_qq_2");
|
|
if (StringUtils.isEmpty(avatar)) {
|
|
avatar = object.getString("figureurl_qq_1");
|
|
}
|
|
return AuthUser.builder()
|
|
.username(object.getString("nickname"))
|
|
.nickname(object.getString("nickname"))
|
|
.avatar(avatar)
|
|
.location(object.getString("province") + "-" + object.getString("city"))
|
|
.uuid(uuid)
|
|
.gender(AuthUserGender.getRealGender(object.getString("gender")))
|
|
.token(authToken)
|
|
.source(AuthSource.QQ)
|
|
.build();
|
|
}
|
|
|
|
private String getOpenId(String accessToken) {
|
|
HttpResponse response = HttpRequest.get(UrlBuilder.getQqOpenidUrl("https://graph.qq.com/oauth2.0/me", accessToken))
|
|
.execute();
|
|
if (response.isOk()) {
|
|
String body = response.body();
|
|
String removePrefix = StrUtil.replace(body, "callback(", "");
|
|
String removeSuffix = StrUtil.replace(removePrefix, ");", "");
|
|
String jsonStr = StrUtil.trim(removeSuffix);
|
|
JSONObject object = JSONObject.parseObject(jsonStr);
|
|
StringBuffer sb = new StringBuffer();
|
|
if (object.containsKey("openid")) {
|
|
sb.append(object.getString("openid"));
|
|
sb.append("|");
|
|
} else {
|
|
throw new AuthException("Invalid openId");
|
|
}
|
|
if (object.containsKey("unionid")) {
|
|
sb.append(object.getString("unionid"));
|
|
}
|
|
return sb.toString();
|
|
}
|
|
|
|
throw new AuthException("request error");
|
|
}
|
|
}
|