diff --git a/.gitee/PULL_REQUEST_TEMPLATE.zh-CN.md b/.gitee/PULL_REQUEST_TEMPLATE.zh-CN.md index d135ee65ee69931c235695f8fb3cbdf0755e2fff..b92ffb1f82509f119701209b63b8c76a571dc72d 100644 --- a/.gitee/PULL_REQUEST_TEMPLATE.zh-CN.md +++ b/.gitee/PULL_REQUEST_TEMPLATE.zh-CN.md @@ -1,15 +1,15 @@ -[ ] 是否为解决Issue? +- [ ] 是否为解决Issue? ### 您做了哪些更新? -#### 新增 +- 新增 -#### 修改 +- 修改 -#### 修复 +- 修复 -#### 其他 +- 其他 ### 是否做了充分测试? diff --git a/src/main/java/me/zhyd/oauth/config/AuthSource.java b/src/main/java/me/zhyd/oauth/config/AuthSource.java index 5fb77e00dcb133eebaf0f840f38bfd203ad4e82b..52a18cd232c9d60ba265bc8d098732c135a50dc4 100644 --- a/src/main/java/me/zhyd/oauth/config/AuthSource.java +++ b/src/main/java/me/zhyd/oauth/config/AuthSource.java @@ -14,13 +14,12 @@ import me.zhyd.oauth.model.AuthCallback; *

* 注: * ①、如需通过JustAuth扩展实现第三方授权,请参考{@link AuthDefaultSource}自行创建对应的枚举类并实现{@link AuthSource}接口 - * ②、如果不是使用的枚举类,那么在授权成功后获取用户信息时,需要单独处理{@link me.zhyd.oauth.model.AuthUser#setSource}字段的赋值 + * ②、如果不是使用的枚举类,那么在授权成功后获取用户信息时,需要单独处理sourcec字段的赋值 * ③、如果扩展了对应枚举类时,在{@link me.zhyd.oauth.request.AuthRequest#login(AuthCallback)}中可以通过{@code xx.toString()}获取对应的source * * @author yadong.zhang (yadong.zhang0415(a)gmail.com) * @version 1.0 - * @date 2019/9/6 21:17 - * @since 1.8 + * @since 1.12.0 */ public interface AuthSource { diff --git a/src/main/java/me/zhyd/oauth/request/AuthElemeRequest.java b/src/main/java/me/zhyd/oauth/request/AuthElemeRequest.java index db064b2dfd91089a728dbb33d3486062a5688b75..4a8c3b727134fa7db2fd0f01c18db94f89b96891 100644 --- a/src/main/java/me/zhyd/oauth/request/AuthElemeRequest.java +++ b/src/main/java/me/zhyd/oauth/request/AuthElemeRequest.java @@ -57,7 +57,6 @@ public class AuthElemeRequest extends AuthDefaultRequest { this.checkResponse(object); return AuthToken.builder() - .openId(this.getOpenId(authCallback.getCode())) .accessToken(object.getString("access_token")) .refreshToken(object.getString("refresh_token")) .tokenType(object.getString("token_type")) @@ -73,29 +72,38 @@ public class AuthElemeRequest extends AuthDefaultRequest { // 时间戳,单位秒。API服务端允许客户端请求最大时间误差为正负5分钟。 final long timestamp = System.currentTimeMillis(); // 公共参数 - Map metasHashMap = new HashMap(); + Map metasHashMap = new HashMap<>(); metasHashMap.put("app_key", config.getClientId()); metasHashMap.put("timestamp", timestamp); String signature = GlobalAuthUtil.generateElemeSignature(config.getClientId(), config.getClientSecret(), timestamp, action, authToken.getAccessToken(), parameters); + String requestId = this.getRequestId(); + + + Map paramsMap = new HashMap<>(); + paramsMap.put("nop", "1.0.0"); + paramsMap.put("id", requestId); + paramsMap.put("action", action); + paramsMap.put("token", authToken.getAccessToken()); + paramsMap.put("metas", metasHashMap); + paramsMap.put("params", parameters); + paramsMap.put("signature", signature); + HttpRequest request = HttpRequest.post(source.userInfo()) - .form("nop", "1.0.0") - .form("id", this.getRequestId()) - .form("metas", metasHashMap) - .form("action", action) - .form("token", authToken.getAccessToken()) - .form("params", parameters) - .form("signature", signature); + .body(JSONObject.toJSONBytes(paramsMap)); // 设置header - this.setHeader(request, "application/json; charset=utf-8"); + this.setHeader(request, "application/json; charset=utf-8", requestId); HttpResponse response = request.execute(); JSONObject object = JSONObject.parseObject(response.body()); // 校验请求 - if (object.containsKey("error")) { + if (object.containsKey("name")) { + throw new AuthException(object.getString("message")); + } + if (object.containsKey("error") && null != object.get("error")) { throw new AuthException(object.getJSONObject("error").getString("message")); } @@ -143,23 +151,6 @@ public class AuthElemeRequest extends AuthDefaultRequest { .build(); } - private String getOpenId(String code) { - HttpRequest request = HttpRequest.post("https://open-api.shop.ele.me/identity") - .form("grant_type", "authorization_code") - .form("code", code) - .form("redirect_uri", config.getRedirectUri()) - .form("client_id", config.getClientId()); - - // 设置header - this.setHeader(request); - - HttpResponse response = request.execute(); - JSONObject object = JSONObject.parseObject(response.body()); - - this.checkResponse(object); - return object.getString("openId"); - } - private String getBasic(String appKey, String appSecret) { StringBuilder sb = new StringBuilder(); String encodeToString = Base64.encode((appKey + ":" + appSecret).getBytes()); @@ -168,20 +159,20 @@ public class AuthElemeRequest extends AuthDefaultRequest { } private void setHeader(HttpRequest request) { - setHeader(request, "application/x-www-form-urlencoded;charset=UTF-8"); + setHeader(request, "application/x-www-form-urlencoded;charset=UTF-8", getRequestId()); + request.header("Authorization", this.getBasic(config.getClientId(), config.getClientSecret())); } - private void setHeader(HttpRequest request, String contentType) { + private void setHeader(HttpRequest request, String contentType, String requestId) { request.header("Accept", "text/xml,text/javascript,text/html") .header("Content-Type", contentType) .header("Accept-Encoding", "gzip") .header("User-Agent", "eleme-openapi-java-sdk") - .header("x-eleme-requestid", getRequestId()) - .header("Authorization", this.getBasic(config.getClientId(), config.getClientSecret())); + .header("x-eleme-requestid", requestId); } private String getRequestId() { - return UuidUtils.getUUID() + "|" + System.currentTimeMillis(); + return (UuidUtils.getUUID() + "|" + System.currentTimeMillis()).toUpperCase(); } private void checkResponse(JSONObject object) {