1
0
mirror of synced 2026-05-22 13:43:20 +00:00

!1 修复钉钉回调JSON解析用户信息异常问题

Merge pull request !1 from skqing/master
This commit is contained in:
yadong.zhang
2019-06-18 19:04:18 +08:00
committed by Gitee
@@ -2,7 +2,8 @@ package me.zhyd.oauth.request;
import cn.hutool.http.HttpRequest; import cn.hutool.http.HttpRequest;
import cn.hutool.http.HttpResponse; import cn.hutool.http.HttpResponse;
import cn.hutool.json.JSONObject; import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import me.zhyd.oauth.config.AuthConfig; import me.zhyd.oauth.config.AuthConfig;
import me.zhyd.oauth.exception.AuthException; import me.zhyd.oauth.exception.AuthException;
import me.zhyd.oauth.model.AuthDingTalkErrorCode; import me.zhyd.oauth.model.AuthDingTalkErrorCode;
@@ -12,8 +13,6 @@ import me.zhyd.oauth.model.AuthUser;
import me.zhyd.oauth.utils.GlobalAuthUtil; import me.zhyd.oauth.utils.GlobalAuthUtil;
import me.zhyd.oauth.utils.UrlBuilder; import me.zhyd.oauth.utils.UrlBuilder;
import java.util.Objects;
/** /**
* 钉钉登录 * 钉钉登录
* *
@@ -40,21 +39,27 @@ public class AuthDingTalkRequest extends BaseAuthRequest {
// 根据timestamp, appSecret计算签名值 // 根据timestamp, appSecret计算签名值
String stringToSign = System.currentTimeMillis() + ""; String stringToSign = System.currentTimeMillis() + "";
String urlEncodeSignature = GlobalAuthUtil.generateDingTalkSignature(config.getClientSecret(), stringToSign); String urlEncodeSignature = GlobalAuthUtil.generateDingTalkSignature(config.getClientSecret(), stringToSign);
JSONObject param = new JSONObject();
param.put("tmp_auth_code", code);
HttpResponse response = HttpRequest.post(UrlBuilder.getDingTalkUserInfoUrl(urlEncodeSignature, stringToSign, config.getClientId())) HttpResponse response = HttpRequest.post(UrlBuilder.getDingTalkUserInfoUrl(urlEncodeSignature, stringToSign, config.getClientId()))
.body(Objects.requireNonNull(new JSONObject().put("tmp_auth_code", code))) .body(param.toJSONString())
.execute(); .execute();
String userInfo = response.body(); String userInfo = response.body();
JSONObject object = new JSONObject(userInfo); JSONObject object = JSON.parseObject(userInfo);
AuthDingTalkErrorCode errorCode = AuthDingTalkErrorCode.getErrorCode(object.getInt("errcode")); AuthDingTalkErrorCode errorCode = AuthDingTalkErrorCode.getErrorCode(object.getIntValue("errcode"));
if (!AuthDingTalkErrorCode.EC0.equals(errorCode)) { if (!AuthDingTalkErrorCode.EC0.equals(errorCode)) {
throw new AuthException(errorCode.getDesc()); throw new AuthException(errorCode.getDesc());
} }
object = object.getJSONObject("user_info"); object = object.getJSONObject("user_info");
AuthToken token = AuthToken.builder()
.openId(object.getString("openid"))
.build();
return AuthUser.builder() return AuthUser.builder()
.uuid(object.getStr("openid")) .uuid(object.getString("unionid"))
.nickname(object.getStr("nick")) .nickname(object.getString("nick"))
.username(object.getStr("nick")) .username(object.getString("nick"))
.source(AuthSource.DINGTALK) .source(AuthSource.DINGTALK)
.token(token)
.build(); .build();
} }
} }