提交 66ee684e 编写于 作者: H Hongwei Peng

refactor: extract common method

上级 d0375ce3
......@@ -140,7 +140,7 @@ public class AuthLinkedinRequest extends AuthDefaultRequest {
private AuthToken getToken(String accessTokenUrl) {
HttpResponse response = HttpRequest.post(accessTokenUrl)
.header("Host", "www.linkedin.com")
.header("Content-Type", "application/x-www-form-urlencoded")
.contentType("application/x-www-form-urlencoded")
.execute();
String accessTokenStr = response.body();
JSONObject accessTokenObject = JSONObject.parseObject(accessTokenStr);
......
......@@ -14,6 +14,8 @@ import me.zhyd.oauth.url.entity.AuthUserInfoEntity;
import java.util.HashMap;
import java.util.Map;
import static me.zhyd.oauth.utils.GlobalAuthUtil.parseQueryToMap;
/**
* 微软登录
*
......@@ -40,12 +42,10 @@ public class AuthMicrosoftRequest extends AuthDefaultRequest {
* @return token对象
*/
private AuthToken getToken(String accessTokenUrl) {
Map<String, Object> paramMap = new HashMap<>(6);
HttpUtil.decodeParamMap(accessTokenUrl, "UTF-8").forEach(paramMap::put);
HttpResponse response = HttpRequest.post(accessTokenUrl)
.header("Host", "https://login.microsoftonline.com")
.header("Content-Type", "application/x-www-form-urlencoded")
.form(paramMap)
.contentType("application/x-www-form-urlencoded")
.form(parseQueryToMap(accessTokenUrl))
.execute();
String accessTokenStr = response.body();
JSONObject accessTokenObject = JSONObject.parseObject(accessTokenStr);
......
......@@ -2,7 +2,6 @@ package me.zhyd.oauth.request;
import cn.hutool.http.HttpRequest;
import cn.hutool.http.HttpResponse;
import cn.hutool.http.HttpUtil;
import com.alibaba.fastjson.JSONObject;
import me.zhyd.oauth.config.AuthConfig;
import me.zhyd.oauth.exception.AuthException;
......@@ -13,10 +12,8 @@ import me.zhyd.oauth.model.AuthUserGender;
import me.zhyd.oauth.url.AuthStackOverflowUrlBuilder;
import me.zhyd.oauth.url.entity.AuthUserInfoEntity;
import java.util.HashMap;
import java.util.Map;
import static me.zhyd.oauth.config.AuthSource.STACK_OVERFLOW;
import static me.zhyd.oauth.utils.GlobalAuthUtil.parseQueryToMap;
/**
* Stack Overflow登录
......@@ -36,7 +33,7 @@ public class AuthStackOverflowRequest extends AuthDefaultRequest {
String accessTokenUrl = this.urlBuilder.getAccessTokenUrl(authCallback.getCode());
HttpResponse response = HttpRequest.post(accessTokenUrl)
.contentType("application/x-www-form-urlencoded")
.form(buildBody(accessTokenUrl))
.form(parseQueryToMap(accessTokenUrl))
.execute();
JSONObject accessTokenObject = JSONObject.parseObject(response.body());
if (!response.isOk()) {
......@@ -68,11 +65,4 @@ public class AuthStackOverflowRequest extends AuthDefaultRequest {
.source(STACK_OVERFLOW)
.build();
}
private Map<String, Object> buildBody(String accessTokenUrl) {
Map<String, Object> paramMap = new HashMap<>();
HttpUtil.decodeParamMap(accessTokenUrl, "UTF-8").forEach(paramMap::put);
return paramMap;
}
}
package me.zhyd.oauth.utils;
import cn.hutool.core.codec.Base64;
import cn.hutool.http.HttpUtil;
import me.zhyd.oauth.exception.AuthException;
import javax.crypto.Mac;
......@@ -82,6 +83,12 @@ public class GlobalAuthUtil {
return res;
}
public static Map<String, Object> parseQueryToMap(String url) {
Map<String, Object> paramMap = new HashMap<>();
HttpUtil.decodeParamMap(url, "UTF-8").forEach(paramMap::put);
return paramMap;
}
public static boolean isHttpProtocol(String url) {
if (StringUtils.isEmpty(url)) {
return false;
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册