Commit c5e15fb3 by lijinqi

1.appId为空的处理

2.HttpUtils的优化
3.
parent 0bdbd9b8
......@@ -149,11 +149,34 @@ public class HttpUtils {
* @return 请求结果
*/
public static String post(String url, Map<String, String> headers, String requestBody) {
try (HttpResponse response = HttpRequest.post(url)
.addHeaders(headers)
Map<String, String> safeHeaders = headers == null ? new java.util.HashMap<>() : 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)
.execute()) {
return redirected.body();
}
}
}
return response.body();
} finally {
if (response != null) {
response.close();
}
}
}
......@@ -167,34 +190,76 @@ public class HttpUtils {
* @return 请求结果
*/
public static String get(String url, Map<String, String> headers) {
try (HttpResponse response = HttpRequest.get(url)
.addHeaders(headers)
Map<String, String> safeHeaders = headers == null ? new java.util.HashMap<>() : 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()) {
return redirected.body();
}
}
}
return response.body();
} finally {
if (response != null) {
response.close();
}
}
}
public static String postIncludeImage(String url, Map<String, String> headers, Map<String, Object> formMaps) {
try (HttpResponse response = openApiPost(url)
.addHeaders(headers)
Map<String, String> safeHeaders = headers == null ? new java.util.HashMap<>() : 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(fileName, file) // 上传文件,"fileName"是接口中定义的字段名
.execute()) {
return redirected.body();
}
}
}
return response.body();
} finally {
if (response != null) {
response.close();
}
}
}
public static HttpRequest openApiPost(String 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) {
try {
// 构建查询参数字符串
StringBuilder queryBuilder = new StringBuilder();
for (Map.Entry<String, String> entry : queryParams.entrySet()) {
if (queryBuilder.length() > 0) {
......@@ -205,17 +270,36 @@ public class HttpUtils {
.append(entry.getValue());
}
// 拼接完整的 URL
String fullUrl = url;
if (queryBuilder.length() > 0) {
fullUrl += "?" + queryBuilder.toString();
fullUrl += "?" + queryBuilder;
}
// 发送 GET 请求并返回响应体
try (HttpResponse response = HttpRequest.get(fullUrl)
.addHeaders(headers)
Map<String, String> safeHeaders = headers == null ? new java.util.HashMap<>() : headers;
HttpResponse response = null;
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()) {
return redirected.body();
}
}
}
return response.body();
} finally {
if (response != null) {
response.close();
}
}
} catch (Exception e) {
e.printStackTrace();
......@@ -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;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.util.StringUtils;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
......@@ -82,6 +83,9 @@ public class ApiSignatureAspect {
//查询appId对应的Secret
String appId = request.getHeader(signature.appId());
if (StringUtils.isEmpty(appId)) {
throw new ServiceException(INVALID_APPID);
}
AppCredentialRespDTO appCredentialRespDTO = appCredentialApi.getAppSecretByAppid(appId);
if (!ObjectUtil.isEmpty(appCredentialRespDTO)) {
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