|
|
@ -1,16 +1,21 @@ |
|
|
|
package com.gitee.sop.gatewaycommon.gateway; |
|
|
|
package com.gitee.sop.gatewaycommon.gateway; |
|
|
|
|
|
|
|
|
|
|
|
import com.gitee.sop.gatewaycommon.bean.ApiContext; |
|
|
|
import com.alibaba.fastjson.JSON; |
|
|
|
import com.gitee.sop.gatewaycommon.bean.SopConstants; |
|
|
|
import com.gitee.sop.gatewaycommon.bean.SopConstants; |
|
|
|
import com.gitee.sop.gatewaycommon.param.ApiParam; |
|
|
|
import com.gitee.sop.gatewaycommon.param.ApiParam; |
|
|
|
import com.gitee.sop.gatewaycommon.util.RequestUtil; |
|
|
|
import com.gitee.sop.gatewaycommon.util.RequestUtil; |
|
|
|
|
|
|
|
import org.apache.commons.lang3.StringUtils; |
|
|
|
|
|
|
|
import org.springframework.http.HttpHeaders; |
|
|
|
import org.springframework.http.HttpMethod; |
|
|
|
import org.springframework.http.HttpMethod; |
|
|
|
|
|
|
|
import org.springframework.http.MediaType; |
|
|
|
|
|
|
|
import org.springframework.http.server.reactive.ServerHttpRequest; |
|
|
|
import org.springframework.util.MultiValueMap; |
|
|
|
import org.springframework.util.MultiValueMap; |
|
|
|
import org.springframework.web.server.ServerWebExchange; |
|
|
|
import org.springframework.web.server.ServerWebExchange; |
|
|
|
|
|
|
|
|
|
|
|
import java.util.HashMap; |
|
|
|
import java.util.HashMap; |
|
|
|
import java.util.List; |
|
|
|
import java.util.List; |
|
|
|
import java.util.Map; |
|
|
|
import java.util.Map; |
|
|
|
|
|
|
|
import java.util.function.Consumer; |
|
|
|
|
|
|
|
|
|
|
|
import static com.gitee.sop.gatewaycommon.bean.SopConstants.CACHE_REQUEST_BODY_FOR_MAP; |
|
|
|
import static com.gitee.sop.gatewaycommon.bean.SopConstants.CACHE_REQUEST_BODY_FOR_MAP; |
|
|
|
import static com.gitee.sop.gatewaycommon.bean.SopConstants.CACHE_REQUEST_BODY_OBJECT_KEY; |
|
|
|
import static com.gitee.sop.gatewaycommon.bean.SopConstants.CACHE_REQUEST_BODY_OBJECT_KEY; |
|
|
@ -18,12 +23,12 @@ import static com.gitee.sop.gatewaycommon.bean.SopConstants.CACHE_REQUEST_BODY_O |
|
|
|
/** |
|
|
|
/** |
|
|
|
* @author tanghc |
|
|
|
* @author tanghc |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
public class GatewayContext extends ApiContext { |
|
|
|
public class ServerWebExchangeUtil { |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
/** |
|
|
|
* 获取请求参数 |
|
|
|
* 获取请求参数 |
|
|
|
* @param exchange |
|
|
|
* @param exchange ServerWebExchange |
|
|
|
* @return |
|
|
|
* @return 返回请求参数 |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
public static ApiParam getApiParam(ServerWebExchange exchange) { |
|
|
|
public static ApiParam getApiParam(ServerWebExchange exchange) { |
|
|
|
return exchange.getAttribute(SopConstants.CACHE_API_PARAM); |
|
|
|
return exchange.getAttribute(SopConstants.CACHE_API_PARAM); |
|
|
@ -31,8 +36,8 @@ public class GatewayContext extends ApiContext { |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
/** |
|
|
|
* 设置请求参数 |
|
|
|
* 设置请求参数 |
|
|
|
* @param exchange |
|
|
|
* @param exchange ServerWebExchange |
|
|
|
* @param apiParam |
|
|
|
* @param apiParam 请求参数 |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
public static void setApiParam(ServerWebExchange exchange, ApiParam apiParam) { |
|
|
|
public static void setApiParam(ServerWebExchange exchange, ApiParam apiParam) { |
|
|
|
exchange.getAttributes().put(SopConstants.CACHE_API_PARAM, apiParam); |
|
|
|
exchange.getAttributes().put(SopConstants.CACHE_API_PARAM, apiParam); |
|
|
@ -40,12 +45,12 @@ public class GatewayContext extends ApiContext { |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
/** |
|
|
|
* 获取Spring Cloud Gateway请求的原始参数。前提是要使用ReadBodyRoutePredicateFactory |
|
|
|
* 获取Spring Cloud Gateway请求的原始参数。前提是要使用ReadBodyRoutePredicateFactory |
|
|
|
* @param exchange |
|
|
|
* @param exchange ServerWebExchange |
|
|
|
* @return 没有参数返回null |
|
|
|
* @return 没有参数返回null |
|
|
|
* @see com.gitee.sop.gatewaycommon.gateway.route.ReadBodyRoutePredicateFactory |
|
|
|
* @see com.gitee.sop.gatewaycommon.gateway.route.ReadBodyRoutePredicateFactory |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
public static Map<String, String> getRequestParams(ServerWebExchange exchange) { |
|
|
|
public static Map<String, ?> getRequestParams(ServerWebExchange exchange) { |
|
|
|
Map<String, String> params = exchange.getAttribute(CACHE_REQUEST_BODY_FOR_MAP); |
|
|
|
Map<String, ?> params = exchange.getAttribute(CACHE_REQUEST_BODY_FOR_MAP); |
|
|
|
if (params != null) { |
|
|
|
if (params != null) { |
|
|
|
return params; |
|
|
|
return params; |
|
|
|
} |
|
|
|
} |
|
|
@ -55,7 +60,14 @@ public class GatewayContext extends ApiContext { |
|
|
|
} else { |
|
|
|
} else { |
|
|
|
String cachedBody = exchange.getAttribute(CACHE_REQUEST_BODY_OBJECT_KEY); |
|
|
|
String cachedBody = exchange.getAttribute(CACHE_REQUEST_BODY_OBJECT_KEY); |
|
|
|
if (cachedBody != null) { |
|
|
|
if (cachedBody != null) { |
|
|
|
params = RequestUtil.parseQueryToMap(cachedBody); |
|
|
|
MediaType contentType = exchange.getRequest().getHeaders().getContentType(); |
|
|
|
|
|
|
|
// 如果是json方式提交
|
|
|
|
|
|
|
|
if (contentType != null |
|
|
|
|
|
|
|
&& StringUtils.containsAny(contentType.toString().toLowerCase(), "json", "text")) { |
|
|
|
|
|
|
|
params = JSON.parseObject(cachedBody); |
|
|
|
|
|
|
|
} else { |
|
|
|
|
|
|
|
params = RequestUtil.parseQueryToMap(cachedBody); |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
if (params != null) { |
|
|
|
if (params != null) { |
|
|
@ -64,15 +76,33 @@ public class GatewayContext extends ApiContext { |
|
|
|
return params; |
|
|
|
return params; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
private static Map<String, String> buildParams(MultiValueMap<String, String> queryParams) { |
|
|
|
public static Map<String, String> buildParams(MultiValueMap<String, String> queryParams) { |
|
|
|
if (queryParams == null || queryParams.size() == 0) { |
|
|
|
if (queryParams == null || queryParams.size() == 0) { |
|
|
|
return null; |
|
|
|
return null; |
|
|
|
} |
|
|
|
} |
|
|
|
Map<String, String> params = new HashMap<>(queryParams.size()); |
|
|
|
Map<String, String> params = new HashMap<>(queryParams.size()); |
|
|
|
for (Map.Entry<String, List<String>> entry : queryParams.entrySet()) { |
|
|
|
for (Map.Entry<String, List<String>> entry : queryParams.entrySet()) { |
|
|
|
String val = entry.getValue().get(0); |
|
|
|
params.put(entry.getKey(), entry.getValue().get(0)); |
|
|
|
params.put(entry.getKey(), val); |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
return params; |
|
|
|
return params; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
|
|
|
* 添加header |
|
|
|
|
|
|
|
* @param exchange 当前ServerWebExchange |
|
|
|
|
|
|
|
* @param headersConsumer headers |
|
|
|
|
|
|
|
* @return 返回一个新的ServerWebExchange |
|
|
|
|
|
|
|
*/ |
|
|
|
|
|
|
|
public static ServerWebExchange addHeaders(ServerWebExchange exchange, Consumer<HttpHeaders> headersConsumer) { |
|
|
|
|
|
|
|
// 创建一个新的request
|
|
|
|
|
|
|
|
ServerHttpRequest serverHttpRequestNew = exchange.getRequest() |
|
|
|
|
|
|
|
.mutate() |
|
|
|
|
|
|
|
.headers(headersConsumer) |
|
|
|
|
|
|
|
.build(); |
|
|
|
|
|
|
|
// 将现在的request 变成 change对象
|
|
|
|
|
|
|
|
return exchange |
|
|
|
|
|
|
|
.mutate() |
|
|
|
|
|
|
|
.request(serverHttpRequestNew) |
|
|
|
|
|
|
|
.build(); |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
} |