网关Mock支持
This commit is contained in:
parent
c1666350f3
commit
c9c0575014
@ -4,6 +4,7 @@ import java.util.*;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
import cn.hutool.core.collection.CollUtil;
|
||||
import cn.hutool.core.util.NumberUtil;
|
||||
import com.alibaba.fastjson2.JSON;
|
||||
import com.sf.common.utils.*;
|
||||
import com.sf.service.gateway.domain.GatewayInterfaceInfo;
|
||||
@ -228,6 +229,13 @@ public class GatewayInterfaceInfoServiceImpl implements IGatewayInterfaceInfoSer
|
||||
case IS_NULL:
|
||||
case NOT_NULL:
|
||||
break;
|
||||
case LT:
|
||||
case LE:
|
||||
case GT:
|
||||
case GE:
|
||||
Assert.notEmpty(condition.getParameterValue(),"请输入参数值");
|
||||
Assert.isTrue(NumberUtil.isNumber(condition.getParameterValue().get(0)),"大于、大于或等于、小于、小于或等于类型,参数值只能是数字");
|
||||
break;
|
||||
default:
|
||||
Assert.notEmpty(condition.getParameterValue(),"请输入参数值");
|
||||
break;
|
||||
|
@ -12,7 +12,7 @@ import com.sf.service.gateway.domain.GatewayServer;
|
||||
import com.sf.service.gateway.enums.GatewayDataStatus;
|
||||
import com.sf.service.gateway.enums.GatewayServiceModel;
|
||||
import com.sf.service.gateway.enums.GatewayServiceType;
|
||||
import com.sf.service.gateway.enums.RequestMethod;
|
||||
import com.sf.vertx.enums.RequestMethod;
|
||||
import com.sf.service.gateway.mapper.GatewayServerMapper;
|
||||
import com.sf.service.gateway.service.IGatewayInterfaceInfoService;
|
||||
import com.sf.service.gateway.service.IGatewayServerService;
|
||||
|
@ -0,0 +1,27 @@
|
||||
package com.sf.vertx.api.pojo;
|
||||
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Data;
|
||||
import lombok.NoArgsConstructor;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* 网关服务-接口Mock
|
||||
*/
|
||||
@Data
|
||||
@NoArgsConstructor
|
||||
@AllArgsConstructor
|
||||
public class MockResponse {
|
||||
|
||||
/**
|
||||
* http状态码
|
||||
*/
|
||||
private Integer httpStatus;
|
||||
|
||||
/**
|
||||
* Mock响应,JSON字符串
|
||||
*/
|
||||
private String mockResponse;
|
||||
|
||||
}
|
@ -1,4 +1,4 @@
|
||||
package com.sf.service.gateway.enums;
|
||||
package com.sf.vertx.enums;
|
||||
|
||||
/**
|
||||
* 支持的请求方法
|
||||
@ -35,7 +35,7 @@ public enum RequestMethod
|
||||
|
||||
public static RequestMethod getByCode(String code){
|
||||
for (RequestMethod value : RequestMethod.values()) {
|
||||
if (value.code.equals(code)){
|
||||
if (value.code.equalsIgnoreCase(code)){
|
||||
return value;
|
||||
}
|
||||
}
|
@ -0,0 +1,19 @@
|
||||
package com.sf.vertx.handle;
|
||||
|
||||
import io.vertx.codegen.annotations.VertxGen;
|
||||
import io.vertx.core.Handler;
|
||||
import io.vertx.ext.web.RoutingContext;
|
||||
|
||||
/***
|
||||
* 接口mock处理
|
||||
*
|
||||
* @author zk
|
||||
*
|
||||
*/
|
||||
@VertxGen
|
||||
public interface ApiMockHandler extends Handler<RoutingContext> {
|
||||
|
||||
static ApiMockHandler create() {
|
||||
return new ApiMockHandlerImpl();
|
||||
}
|
||||
}
|
@ -0,0 +1,31 @@
|
||||
package com.sf.vertx.handle;
|
||||
|
||||
import com.sf.vertx.api.pojo.AppConfig;
|
||||
import com.sf.vertx.api.pojo.MockResponse;
|
||||
import com.sf.vertx.constans.SacErrorCode;
|
||||
import io.vertx.ext.web.RoutingContext;
|
||||
import io.vertx.ext.web.handler.HttpException;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
|
||||
@Slf4j
|
||||
public class ApiMockHandlerImpl implements ApiMockHandler {
|
||||
|
||||
@Override
|
||||
public void handle(RoutingContext rc) {
|
||||
try {
|
||||
String cacheKey = AppConfigHandler.getApiCodeConfigCacheKey(rc);
|
||||
// mock
|
||||
MockResponse mockResponse = AppConfigHandler.mock(cacheKey,rc);
|
||||
if (mockResponse != null) {
|
||||
rc.fail(new MockException(mockResponse.getHttpStatus(), mockResponse.getMockResponse()));
|
||||
return;
|
||||
}
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
rc.fail(new HttpException(SacErrorCode.DEFAULT_ERROR_CODE));
|
||||
return;
|
||||
}
|
||||
rc.next();
|
||||
}
|
||||
}
|
@ -1,5 +1,6 @@
|
||||
package com.sf.vertx.handle;
|
||||
|
||||
import java.math.BigDecimal;
|
||||
import java.time.Duration;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
@ -7,6 +8,18 @@ import java.util.List;
|
||||
import java.util.Set;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
|
||||
import cn.hutool.core.collection.CollectionUtil;
|
||||
import cn.hutool.core.util.NumberUtil;
|
||||
import com.sf.vertx.api.pojo.*;
|
||||
import com.sf.vertx.enums.MatchType;
|
||||
import com.sf.vertx.enums.RequestMethod;
|
||||
import com.sf.vertx.security.MainSecurity;
|
||||
import io.vertx.core.buffer.Buffer;
|
||||
import io.vertx.core.http.*;
|
||||
import io.vertx.core.json.JsonObject;
|
||||
import io.vertx.ext.web.RequestBody;
|
||||
import io.vertx.ext.web.RoutingContext;
|
||||
import io.vertx.ext.web.handler.HttpException;
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
import org.springframework.data.redis.core.RedisTemplate;
|
||||
|
||||
@ -17,13 +30,6 @@ import com.hazelcast.config.Config;
|
||||
import com.hazelcast.config.JoinConfig;
|
||||
import com.hazelcast.config.NetworkConfig;
|
||||
import com.hazelcast.config.TcpIpConfig;
|
||||
import com.sf.vertx.api.pojo.ApiConfig;
|
||||
import com.sf.vertx.api.pojo.AppConfig;
|
||||
import com.sf.vertx.api.pojo.Node;
|
||||
import com.sf.vertx.api.pojo.RouteContent;
|
||||
import com.sf.vertx.api.pojo.SacService;
|
||||
import com.sf.vertx.api.pojo.Strategy;
|
||||
import com.sf.vertx.api.pojo.VertxConfig;
|
||||
import com.sf.vertx.arithmetic.roundRobin.SacLoadBalancing;
|
||||
import com.sf.vertx.constans.RedisKeyConfig;
|
||||
import com.sf.vertx.init.SacVertxConfig;
|
||||
@ -39,10 +45,6 @@ import io.vertx.circuitbreaker.CircuitBreakerOptions;
|
||||
import io.vertx.core.Future;
|
||||
import io.vertx.core.Vertx;
|
||||
import io.vertx.core.VertxOptions;
|
||||
import io.vertx.core.http.HttpClient;
|
||||
import io.vertx.core.http.HttpMethod;
|
||||
import io.vertx.core.http.HttpServer;
|
||||
import io.vertx.core.http.HttpServerOptions;
|
||||
import io.vertx.core.net.JksOptions;
|
||||
import io.vertx.core.spi.cluster.ClusterManager;
|
||||
import io.vertx.ext.web.Route;
|
||||
@ -177,14 +179,27 @@ public class AppConfigHandler {
|
||||
* @return
|
||||
*/
|
||||
public static boolean isAnalysisBody(String appCode, String apiCode, String contentType) {
|
||||
String keyCircuitBreaker = appCode + ":" + apiCode + ":" + "CIRCUIT_BREAKER";
|
||||
CircuitBreaker circuitBreaker = AppConfigHandler.getApiCodeCircuitBreaker(keyCircuitBreaker);
|
||||
boolean isDataSecurity = AppConfigHandler.isDataSecurity(appCode);
|
||||
String apiCodeCacheKey = appCode + ":" + apiCode;
|
||||
// Mock解析body
|
||||
if (APICODE_CONFIG_MAP.get(apiCodeCacheKey) != null && APICODE_CONFIG_MAP.get(apiCodeCacheKey).getMockDefaultHttpStatus() != null){
|
||||
return true;
|
||||
}
|
||||
ApiConfig apicodeConfig = AppConfigHandler.getApicodeConfig(apiCodeCacheKey);
|
||||
// GET,DELETE,HEAD请求不解析body
|
||||
RequestMethod requestMethod = RequestMethod.getByCode(apicodeConfig.getMethod());
|
||||
if (requestMethod == null || RequestMethod.GET.equals(requestMethod)
|
||||
|| RequestMethod.DELETE.equals(requestMethod)
|
||||
|| RequestMethod.HEAD.equals(requestMethod)){
|
||||
return false;
|
||||
}
|
||||
// 模糊匹配不解析body
|
||||
String uri = AppConfigHandler.getApicodeConfig(appCode + ":" + apiCode).getUri();
|
||||
String uri = apicodeConfig.getUri();
|
||||
if (StringUtils.equals(uri, "*")) {
|
||||
return false;
|
||||
}
|
||||
String keyCircuitBreaker = apiCodeCacheKey + ":" + "CIRCUIT_BREAKER";
|
||||
CircuitBreaker circuitBreaker = AppConfigHandler.getApiCodeCircuitBreaker(keyCircuitBreaker);
|
||||
boolean isDataSecurity = AppConfigHandler.isDataSecurity(appCode);
|
||||
// 文件上传不走加解密
|
||||
return (isDataSecurity || circuitBreaker != null) && StringUtils.startsWith(contentType, "multipart") == false;
|
||||
}
|
||||
@ -240,8 +255,115 @@ public class AppConfigHandler {
|
||||
&& StringUtils.equals(httpMethod, APICODE_CONFIG_MAP.get(key).getMethod());
|
||||
}
|
||||
|
||||
public static String mock(String key) {
|
||||
return APICODE_CONFIG_MAP.get(key) != null ? APICODE_CONFIG_MAP.get(key).getMockResponse() : null;
|
||||
public static MockResponse mock(String key, RoutingContext rc) {
|
||||
if (APICODE_CONFIG_MAP.get(key) != null && APICODE_CONFIG_MAP.get(key).getMockDefaultHttpStatus() != null){
|
||||
Integer httpStatus = APICODE_CONFIG_MAP.get(key).getMockDefaultHttpStatus();
|
||||
String mockResponse = APICODE_CONFIG_MAP.get(key).getMockDefaultResponse();
|
||||
List<MockExpectation> mockExpectations = APICODE_CONFIG_MAP.get(key).getMockExpectations();
|
||||
if (mockExpectations != null && !mockExpectations.isEmpty()){
|
||||
RequestBody body = rc.body();
|
||||
JsonObject jsonBody = body.asJsonObject();
|
||||
for (MockExpectation mockExpectation : mockExpectations) {
|
||||
// 匹配条件需要全部匹配成功
|
||||
boolean matchSuccess = true;
|
||||
for (MockMatchCondition matchCondition : mockExpectation.getMatchConditions()) {
|
||||
if (!matchSuccess){
|
||||
break;
|
||||
}
|
||||
MatchType matchType = MatchType.getByCode(matchCondition.getMatchType());
|
||||
if (matchType == null) {
|
||||
matchSuccess = false;
|
||||
break;
|
||||
}
|
||||
String parameterPosition = matchCondition.getParameterPosition();
|
||||
String parameterKey = matchCondition.getParameterKey();
|
||||
List<String> parameterValue = matchCondition.getParameterValue();
|
||||
String requestParameterValue = getRequestParameterValue(parameterPosition,parameterKey,rc.request(),jsonBody);
|
||||
if (!MatchType.IS_NULL.equals(matchType) && !MatchType.NOT_NULL.equals(matchType)){
|
||||
// 需要值而没有设置值,直接匹配失败
|
||||
if (CollectionUtil.isEmpty(parameterValue) || requestParameterValue == null) {
|
||||
matchSuccess = false;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
switch (matchType){
|
||||
case EQ:
|
||||
matchSuccess = parameterValue.get(0).equals(requestParameterValue);
|
||||
break;
|
||||
case NOT_EQ:
|
||||
matchSuccess = !parameterValue.get(0).equals(requestParameterValue);
|
||||
break;
|
||||
case GT:
|
||||
if (NumberUtil.isNumber(requestParameterValue) && NumberUtil.isNumber(parameterValue.get(0))) {
|
||||
matchSuccess = new BigDecimal(requestParameterValue).compareTo(new BigDecimal(parameterValue.get(0))) > 0;
|
||||
}else {
|
||||
matchSuccess = requestParameterValue.compareTo(parameterValue.get(0)) > 0;
|
||||
}
|
||||
break;
|
||||
case GE:
|
||||
if (NumberUtil.isNumber(requestParameterValue) && NumberUtil.isNumber(parameterValue.get(0))) {
|
||||
matchSuccess = new BigDecimal(requestParameterValue).compareTo(new BigDecimal(parameterValue.get(0))) >= 0;
|
||||
}else {
|
||||
matchSuccess = requestParameterValue.compareTo(parameterValue.get(0)) >= 0;
|
||||
}
|
||||
break;
|
||||
case LT:
|
||||
if (NumberUtil.isNumber(requestParameterValue) && NumberUtil.isNumber(parameterValue.get(0))) {
|
||||
matchSuccess = new BigDecimal(requestParameterValue).compareTo(new BigDecimal(parameterValue.get(0))) < 0;
|
||||
}else {
|
||||
matchSuccess = requestParameterValue.compareTo(parameterValue.get(0)) < 0;
|
||||
}
|
||||
break;
|
||||
case LE:
|
||||
if (NumberUtil.isNumber(requestParameterValue) && NumberUtil.isNumber(parameterValue.get(0))) {
|
||||
matchSuccess = new BigDecimal(requestParameterValue).compareTo(new BigDecimal(parameterValue.get(0))) <= 0;
|
||||
}else {
|
||||
matchSuccess = requestParameterValue.compareTo(parameterValue.get(0)) <= 0;
|
||||
}
|
||||
break;
|
||||
case IN:
|
||||
matchSuccess = parameterValue.contains(requestParameterValue);
|
||||
break;
|
||||
case NOT_IN:
|
||||
matchSuccess = !parameterValue.contains(requestParameterValue);
|
||||
break;
|
||||
case IS_NULL:
|
||||
matchSuccess = requestParameterValue == null;
|
||||
break;
|
||||
case NOT_NULL:
|
||||
matchSuccess = requestParameterValue != null;
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
|
||||
}
|
||||
if (matchSuccess){
|
||||
httpStatus = mockExpectation.getHttpStatus();
|
||||
mockResponse = mockExpectation.getMockResponse();
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return new MockResponse(httpStatus,mockResponse);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
private static String getRequestParameterValue(String parameterPosition, String parameterKey, HttpServerRequest request,JsonObject jsonBody) {
|
||||
switch (parameterPosition){
|
||||
case "query":
|
||||
return request.getParam(parameterKey);
|
||||
case "header":
|
||||
return request.getHeader(parameterKey);
|
||||
case "body":
|
||||
return jsonBody.getString(parameterKey);
|
||||
default:
|
||||
break;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
private static RateLimiterRegistry createRateLimiter(Strategy strategy) {
|
||||
@ -607,14 +729,22 @@ public class AppConfigHandler {
|
||||
String rateLimitModel = vertxConfig.getRateLimitModel();
|
||||
Route routeSac = mainHttpRouter.route(rpcUri());
|
||||
Route routeOpen = mainHttpRouter.route();
|
||||
routeSac.handler(ParameterCheckHandler.create()).handler(AppRateLimitHandler.create(rateLimitModel))
|
||||
.handler(ApiRateLimitHandler.create(rateLimitModel)).handler(BodyHandler.create())
|
||||
.handler(ProxyHandler.create(mainWebClient, proxy)).failureHandler(RestfulFailureHandler.create());
|
||||
routeSac.handler(ParameterCheckHandler.create())
|
||||
.handler(AppRateLimitHandler.create(rateLimitModel))
|
||||
.handler(ApiRateLimitHandler.create(rateLimitModel))
|
||||
.handler(BodyHandler.create())
|
||||
.handler(ApiMockHandler.create())
|
||||
.handler(ProxyHandler.create(mainWebClient, proxy))
|
||||
.failureHandler(RestfulFailureHandler.create());
|
||||
// mainHttpRouter.route().handler(ProxyHandler.create(mainWebClient, proxy));
|
||||
|
||||
routeOpen.handler(ParameterCheckHandler.create()).handler(AppRateLimitHandler.create(rateLimitModel))
|
||||
.handler(ApiRateLimitHandler.create(rateLimitModel)).handler(BodyHandler.create())
|
||||
.handler(ProxyHandler.create(mainWebClient, proxy)).failureHandler(RestfulFailureHandler.create());
|
||||
routeOpen.handler(ParameterCheckHandler.create())
|
||||
.handler(AppRateLimitHandler.create(rateLimitModel))
|
||||
.handler(ApiRateLimitHandler.create(rateLimitModel))
|
||||
.handler(BodyHandler.create())
|
||||
.handler(ApiMockHandler.create())
|
||||
.handler(ProxyHandler.create(mainWebClient, proxy))
|
||||
.failureHandler(RestfulFailureHandler.create());
|
||||
}
|
||||
|
||||
/***
|
||||
@ -651,4 +781,57 @@ public class AppConfigHandler {
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public static String bodyEncrypt(String body, String appCode) {
|
||||
DataSecurity dataSecurity = getAppConfig(appCode).getDataSecurity();
|
||||
switch (dataSecurity.getAlgorithm()) {
|
||||
case "AES":
|
||||
return MainSecurity.aesEncrypt(body, dataSecurity.getPrivateKey());
|
||||
case "RSA":
|
||||
return MainSecurity.rsaEncrypt(body, dataSecurity.getPublicKey());
|
||||
default:
|
||||
break;
|
||||
}
|
||||
log.info(" appCode:{}, encrypt key config is error.", appCode);
|
||||
throw new HttpException(10011);
|
||||
}
|
||||
|
||||
public static String bodyDecrypt(String body, String appCode) {
|
||||
DataSecurity dataSecurity = getAppConfig(appCode).getDataSecurity();
|
||||
switch (dataSecurity.getAlgorithm()) {
|
||||
case "AES":
|
||||
return MainSecurity.aesDecrypt(body, dataSecurity.getPrivateKey());
|
||||
case "RSA":
|
||||
return MainSecurity.rsaDecrypt(body, dataSecurity.getPrivateKey());
|
||||
default:
|
||||
break;
|
||||
}
|
||||
log.info(" appCode:{}, decrypt key config is error.", appCode);
|
||||
throw new HttpException(10011);
|
||||
}
|
||||
|
||||
public static String getApiCodeConfigCacheKey(RoutingContext rc){
|
||||
String domain = rc.request().authority().host();
|
||||
log.info("domain:{}", domain);
|
||||
String appCode = rc.request().headers().get(AppConfigHandler.getAppCodeHeaderKey());
|
||||
String apiCode = rc.request().headers().get(AppConfigHandler.getApiCodeHeaderKey());
|
||||
String key = appCode + ":" + apiCode;
|
||||
if (StringUtils.isBlank(appCode) || StringUtils.isBlank(apiCode)
|
||||
|| AppConfigHandler.getAppConfig(appCode) == null
|
||||
|| AppConfigHandler.getApicodeConfig(key) == null) {
|
||||
// 判断OPEN模式, header不传递参数, 走域名映射
|
||||
AppConfig appConfig = AppConfigHandler.getAppConfigByDomain(domain);
|
||||
if(appConfig != null) {
|
||||
appCode = appConfig.getAppCode();
|
||||
apiCode = domain;
|
||||
rc.request().headers().add(AppConfigHandler.getAppCodeHeaderKey(), appCode);
|
||||
rc.request().headers().add(AppConfigHandler.getApiCodeHeaderKey(), apiCode);
|
||||
key = appCode + ":" + apiCode;
|
||||
} else {
|
||||
rc.fail(new HttpException(10012));
|
||||
}
|
||||
}
|
||||
return key;
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -1,5 +1,7 @@
|
||||
package com.sf.vertx.handle;
|
||||
|
||||
import com.sf.vertx.api.pojo.MockResponse;
|
||||
import io.vertx.ext.web.RequestBody;
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
|
||||
import com.sf.vertx.api.pojo.AppConfig;
|
||||
@ -50,12 +52,6 @@ public class ParameterCheckHandlerImpl implements ParameterCheckHandler {
|
||||
return;
|
||||
}
|
||||
|
||||
// mock
|
||||
String mockResponse = AppConfigHandler.mock(key);
|
||||
if(StringUtils.isNotBlank(mockResponse)) {
|
||||
rc.fail(new MockException(10020, mockResponse));
|
||||
return;
|
||||
}
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
rc.fail(new HttpException(SacErrorCode.DEFAULT_ERROR_CODE));
|
||||
|
@ -32,8 +32,8 @@ public class RestfulFailureHandlerImpl implements RestfulFailureHandler {
|
||||
} else if (failure instanceof MockException) {
|
||||
MockException httpException = (MockException) failure;
|
||||
sc = httpException.getStatusCode();
|
||||
statusCode = httpException.getStatusCode();
|
||||
if (StringUtils.isNoneBlank(httpException.getPayload())) {
|
||||
statusCode = 200;
|
||||
errorJson = new JsonObject(httpException.getPayload());
|
||||
} else {
|
||||
errorJson = SacErrorCode.returnErrorMsg(httpException.getStatusCode());
|
||||
|
@ -251,7 +251,7 @@ public class ReverseProxy implements HttpProxy {
|
||||
HttpRequest<Buffer> requestBuffer = methodGetRequestBuffer(proxyRequest);
|
||||
requestBuffer.timeout(AppConfigHandler.getApicodeConfigTimeOut(key));
|
||||
requestBuffer.putHeaders(proxyRequest.headers())
|
||||
.sendJson(bodyDecrypt(ctx.getBodyAsString(), appCode), h -> {
|
||||
.sendJson(AppConfigHandler.bodyDecrypt(ctx.getBodyAsString(), appCode), h -> {
|
||||
if (h.succeeded()) {
|
||||
log.info("==========uri:{},response http code:{}", proxyRequest.getURI(),
|
||||
h.result().statusCode());
|
||||
@ -268,7 +268,7 @@ public class ReverseProxy implements HttpProxy {
|
||||
JsonObject responseData = h.result().bodyAsJsonObject();
|
||||
log.info("responseData:{}", responseData);
|
||||
// 加密
|
||||
String dataStr = bodyEncrypt(responseData.toString(), appCode);
|
||||
String dataStr = AppConfigHandler.bodyEncrypt(responseData.toString(), appCode);
|
||||
log.info("aesEncrypt dataStr:{}", dataStr);
|
||||
Buffer buffer = Buffer.buffer(dataStr);
|
||||
ProxyResponse proxyResponse = proxyRequest.response().setStatusCode(200)
|
||||
@ -362,7 +362,7 @@ public class ReverseProxy implements HttpProxy {
|
||||
return Future.future(p -> {
|
||||
HttpRequest<Buffer> requestBuffer = methodGetRequestBuffer(proxyRequest);
|
||||
requestBuffer.timeout(AppConfigHandler.getApicodeConfigTimeOut(key));
|
||||
requestBuffer.putHeaders(proxyRequest.headers()).sendJson(bodyDecrypt(ctx.getBodyAsString(), appCode),
|
||||
requestBuffer.putHeaders(proxyRequest.headers()).sendJson(AppConfigHandler.bodyDecrypt(ctx.getBodyAsString(), appCode),
|
||||
h -> {
|
||||
if (h.succeeded()) {
|
||||
log.info("==========uri:{},response http code:{}", proxyRequest.getURI(),
|
||||
@ -373,7 +373,7 @@ public class ReverseProxy implements HttpProxy {
|
||||
JsonObject responseData = h.result().bodyAsJsonObject();
|
||||
log.info("responseData:{}", responseData);
|
||||
// 加密
|
||||
String dataStr = bodyEncrypt(responseData.toString(), appCode);
|
||||
String dataStr = AppConfigHandler.bodyEncrypt(responseData.toString(), appCode);
|
||||
log.info("encrypt dataStr:{}", dataStr);
|
||||
Buffer buffer = Buffer.buffer(dataStr);
|
||||
ProxyResponse proxyResponse = proxyRequest.response().setStatusCode(200)
|
||||
@ -491,32 +491,6 @@ public class ReverseProxy implements HttpProxy {
|
||||
return sendResponse();
|
||||
}
|
||||
|
||||
private String bodyEncrypt(String body, String appCode) {
|
||||
DataSecurity dataSecurity = AppConfigHandler.getAppConfig(appCode).getDataSecurity();
|
||||
switch (dataSecurity.getAlgorithm()) {
|
||||
case "AES":
|
||||
return MainSecurity.aesEncrypt(body, dataSecurity.getPrivateKey());
|
||||
case "RSA":
|
||||
return MainSecurity.rsaEncrypt(body, dataSecurity.getPublicKey());
|
||||
default:
|
||||
break;
|
||||
}
|
||||
log.info(" appCode:{}, encrypt key config is error.", appCode);
|
||||
throw new HttpException(10011);
|
||||
}
|
||||
|
||||
private String bodyDecrypt(String body, String appCode) {
|
||||
DataSecurity dataSecurity = AppConfigHandler.getAppConfig(appCode).getDataSecurity();
|
||||
switch (dataSecurity.getAlgorithm()) {
|
||||
case "AES":
|
||||
return MainSecurity.aesDecrypt(body, dataSecurity.getPrivateKey());
|
||||
case "RSA":
|
||||
return MainSecurity.rsaDecrypt(body, dataSecurity.getPrivateKey());
|
||||
default:
|
||||
break;
|
||||
}
|
||||
log.info(" appCode:{}, decrypt key config is error.", appCode);
|
||||
throw new HttpException(10011);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user