Commit c5e15fb3 by lijinqi

1.appId为空的处理

2.HttpUtils的优化
3.
parent 0bdbd9b8
...@@ -149,11 +149,34 @@ public class HttpUtils { ...@@ -149,11 +149,34 @@ public class HttpUtils {
* @return 请求结果 * @return 请求结果
*/ */
public static String post(String url, Map<String, String> headers, String requestBody) { public static String post(String url, Map<String, String> headers, String requestBody) {
try (HttpResponse response = HttpRequest.post(url) Map<String, String> safeHeaders = headers == null ? new java.util.HashMap<>() : headers;
.addHeaders(headers) HttpResponse response = null;
try {
response = HttpRequest.post(url)
.addHeaders(safeHeaders)
.setFollowRedirects(true)
.body(requestBody)
.execute();
// Manually follow 30x if not auto-followed
if (isRedirect(response)) {
String location = response.header("Location");
if (StrUtil.isNotBlank(location)) {
String newUrl = resolveRedirectUrl(url, location);
try (HttpResponse redirected = HttpRequest.post(newUrl)
.addHeaders(safeHeaders)
.setFollowRedirects(true)
.body(requestBody) .body(requestBody)
.execute()) { .execute()) {
return redirected.body();
}
}
}
return response.body(); return response.body();
} finally {
if (response != null) {
response.close();
}
} }
} }
...@@ -167,34 +190,76 @@ public class HttpUtils { ...@@ -167,34 +190,76 @@ public class HttpUtils {
* @return 请求结果 * @return 请求结果
*/ */
public static String get(String url, Map<String, String> headers) { public static String get(String url, Map<String, String> headers) {
try (HttpResponse response = HttpRequest.get(url) Map<String, String> safeHeaders = headers == null ? new java.util.HashMap<>() : headers;
.addHeaders(headers) HttpResponse response = null;
try {
response = HttpRequest.get(url)
.addHeaders(safeHeaders)
.setFollowRedirects(true)
.execute();
if (isRedirect(response)) {
String location = response.header("Location");
if (StrUtil.isNotBlank(location)) {
String newUrl = resolveRedirectUrl(url, location);
try (HttpResponse redirected = HttpRequest.get(newUrl)
.addHeaders(safeHeaders)
.setFollowRedirects(true)
.execute()) { .execute()) {
return redirected.body();
}
}
}
return response.body(); return response.body();
} finally {
if (response != null) {
response.close();
}
} }
} }
public static String postIncludeImage(String url, Map<String, String> headers, Map<String, Object> formMaps) { public static String postIncludeImage(String url, Map<String, String> headers, Map<String, Object> formMaps) {
try (HttpResponse response = openApiPost(url) Map<String, String> safeHeaders = headers == null ? new java.util.HashMap<>() : headers;
.addHeaders(headers) HttpResponse response = null;
try {
response = openApiPost(url)
.addHeaders(safeHeaders)
.setFollowRedirects(true)
.form(formMaps)
.execute();
if (isRedirect(response)) {
String location = response.header("Location");
if (StrUtil.isNotBlank(location)) {
String newUrl = resolveRedirectUrl(url, location);
try (HttpResponse redirected = openApiPost(newUrl)
.addHeaders(safeHeaders)
.setFollowRedirects(true)
.form(formMaps) .form(formMaps)
//.form(fileName, file) // 上传文件,"fileName"是接口中定义的字段名
.execute()) { .execute()) {
return redirected.body();
}
}
}
return response.body(); return response.body();
} finally {
if (response != null) {
response.close();
}
} }
} }
public static HttpRequest openApiPost(String url) { public static HttpRequest openApiPost(String url) {
return HttpRequest.post(url) return HttpRequest.post(url)
.setReadTimeout(300000); .setReadTimeout(300000)
.setFollowRedirects(true);
} }
public static String get(String url, Map<String, String> headers, Map<String, String> queryParams) { public static String get(String url, Map<String, String> headers, Map<String, String> queryParams) {
try { try {
// 构建查询参数字符串
StringBuilder queryBuilder = new StringBuilder(); StringBuilder queryBuilder = new StringBuilder();
for (Map.Entry<String, String> entry : queryParams.entrySet()) { for (Map.Entry<String, String> entry : queryParams.entrySet()) {
if (queryBuilder.length() > 0) { if (queryBuilder.length() > 0) {
...@@ -205,17 +270,36 @@ public class HttpUtils { ...@@ -205,17 +270,36 @@ public class HttpUtils {
.append(entry.getValue()); .append(entry.getValue());
} }
// 拼接完整的 URL
String fullUrl = url; String fullUrl = url;
if (queryBuilder.length() > 0) { if (queryBuilder.length() > 0) {
fullUrl += "?" + queryBuilder.toString(); fullUrl += "?" + queryBuilder;
} }
// 发送 GET 请求并返回响应体 Map<String, String> safeHeaders = headers == null ? new java.util.HashMap<>() : headers;
try (HttpResponse response = HttpRequest.get(fullUrl) HttpResponse response = null;
.addHeaders(headers) try {
response = HttpRequest.get(fullUrl)
.addHeaders(safeHeaders)
.setFollowRedirects(true)
.execute();
if (isRedirect(response)) {
String location = response.header("Location");
if (StrUtil.isNotBlank(location)) {
String newUrl = resolveRedirectUrl(fullUrl, location);
try (HttpResponse redirected = HttpRequest.get(newUrl)
.addHeaders(safeHeaders)
.setFollowRedirects(true)
.execute()) { .execute()) {
return redirected.body();
}
}
}
return response.body(); return response.body();
} finally {
if (response != null) {
response.close();
}
} }
} catch (Exception e) { } catch (Exception e) {
e.printStackTrace(); e.printStackTrace();
...@@ -223,5 +307,24 @@ public class HttpUtils { ...@@ -223,5 +307,24 @@ public class HttpUtils {
} }
} }
private static boolean isRedirect(HttpResponse response) {
if (response == null) return false;
int status = response.getStatus();
return status == 301 || status == 302 || status == 303 || status == 307 || status == 308;
}
private static String resolveRedirectUrl(String baseUrl, String location) {
if (StrUtil.startWithIgnoreCase(location, "http://") || StrUtil.startWithIgnoreCase(location, "https://")) {
return location;
}
// Relative redirect; resolve against baseUrl
try {
java.net.URI base = java.net.URI.create(baseUrl);
return base.resolve(location).toString();
} catch (Exception ignore) {
return location;
}
}
} }
...@@ -26,6 +26,7 @@ import lombok.extern.slf4j.Slf4j; ...@@ -26,6 +26,7 @@ import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.ProceedingJoinPoint; import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around; import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect; import org.aspectj.lang.annotation.Aspect;
import org.springframework.util.StringUtils;
import javax.annotation.Resource; import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletRequest;
...@@ -82,6 +83,9 @@ public class ApiSignatureAspect { ...@@ -82,6 +83,9 @@ public class ApiSignatureAspect {
//查询appId对应的Secret //查询appId对应的Secret
String appId = request.getHeader(signature.appId()); String appId = request.getHeader(signature.appId());
if (StringUtils.isEmpty(appId)) {
throw new ServiceException(INVALID_APPID);
}
AppCredentialRespDTO appCredentialRespDTO = appCredentialApi.getAppSecretByAppid(appId); AppCredentialRespDTO appCredentialRespDTO = appCredentialApi.getAppSecretByAppid(appId);
if (!ObjectUtil.isEmpty(appCredentialRespDTO)) { if (!ObjectUtil.isEmpty(appCredentialRespDTO)) {
Assert.notNull(appCredentialRespDTO.getAppId(), "[appId({})] 找不到对应的 appSecret", appId); Assert.notNull(appCredentialRespDTO.getAppId(), "[appId({})] 找不到对应的 appSecret", appId);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment