From 8fd88052c5fa572f9fef707bac9bc6f1677094cc Mon Sep 17 00:00:00 2001 From: tanghc Date: Tue, 18 Jun 2019 17:21:08 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96=E9=AA=8C=E8=AF=81=E9=80=BB?= =?UTF-8?q?=E8=BE=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../gateway/filter/ValidateFilter.java | 2 - .../gateway/route/GatewayRouteRepository.java | 4 +- .../manager/RouteRepositoryContext.java | 13 ++++++ .../gatewaycommon/param/BaseParamBuilder.java | 18 +++----- .../sop/gatewaycommon/util/RouteUtil.java | 22 +-------- .../gatewaycommon/validate/ApiValidator.java | 46 +++++++++++++------ .../zuul/filter/PreValidateFilter.java | 5 +- .../zuul/route/ZuulRouteRepository.java | 3 ++ 8 files changed, 62 insertions(+), 51 deletions(-) diff --git a/sop-common/sop-gateway-common/src/main/java/com/gitee/sop/gatewaycommon/gateway/filter/ValidateFilter.java b/sop-common/sop-gateway-common/src/main/java/com/gitee/sop/gatewaycommon/gateway/filter/ValidateFilter.java index 8e574678..32dc3baa 100644 --- a/sop-common/sop-gateway-common/src/main/java/com/gitee/sop/gatewaycommon/gateway/filter/ValidateFilter.java +++ b/sop-common/sop-gateway-common/src/main/java/com/gitee/sop/gatewaycommon/gateway/filter/ValidateFilter.java @@ -5,7 +5,6 @@ import com.gitee.sop.gatewaycommon.bean.ApiContext; import com.gitee.sop.gatewaycommon.bean.SopConstants; import com.gitee.sop.gatewaycommon.exception.ApiException; import com.gitee.sop.gatewaycommon.param.ApiParam; -import com.gitee.sop.gatewaycommon.util.RouteUtil; import com.gitee.sop.gatewaycommon.validate.Validator; import lombok.extern.slf4j.Slf4j; import org.springframework.cloud.gateway.filter.GatewayFilterChain; @@ -26,7 +25,6 @@ public class ValidateFilter implements GlobalFilter, Ordered { // 解析参数 ApiParam param = apiConfig.getGatewayParamBuilder().build(exchange); exchange.getAttributes().put(SopConstants.CACHE_API_PARAM, param); - RouteUtil.checkEnable(param); // 验证操作,这里有负责验证签名参数 Validator validator = apiConfig.getValidator(); try { diff --git a/sop-common/sop-gateway-common/src/main/java/com/gitee/sop/gatewaycommon/gateway/route/GatewayRouteRepository.java b/sop-common/sop-gateway-common/src/main/java/com/gitee/sop/gatewaycommon/gateway/route/GatewayRouteRepository.java index 8d8c913a..f9f14d68 100644 --- a/sop-common/sop-gateway-common/src/main/java/com/gitee/sop/gatewaycommon/gateway/route/GatewayRouteRepository.java +++ b/sop-common/sop-gateway-common/src/main/java/com/gitee/sop/gatewaycommon/gateway/route/GatewayRouteRepository.java @@ -11,7 +11,6 @@ import org.springframework.cloud.gateway.event.PredicateArgsEvent; import org.springframework.cloud.gateway.event.RefreshRoutesEvent; import org.springframework.cloud.gateway.handler.predicate.PredicateDefinition; import org.springframework.cloud.gateway.handler.predicate.RoutePredicateFactory; -import org.springframework.cloud.gateway.route.InMemoryRouteDefinitionRepository; import org.springframework.cloud.gateway.route.RouteDefinition; import org.springframework.cloud.gateway.route.RouteDefinitionRepository; import org.springframework.cloud.gateway.support.ConfigurationUtils; @@ -80,6 +79,9 @@ public class GatewayRouteRepository implements ApplicationEventPublisherAware, */ @Override public GatewayTargetRoute get(String id) { + if (id == null) { + return null; + } return routes.get(id); } diff --git a/sop-common/sop-gateway-common/src/main/java/com/gitee/sop/gatewaycommon/manager/RouteRepositoryContext.java b/sop-common/sop-gateway-common/src/main/java/com/gitee/sop/gatewaycommon/manager/RouteRepositoryContext.java index 951b3512..f103f55e 100644 --- a/sop-common/sop-gateway-common/src/main/java/com/gitee/sop/gatewaycommon/manager/RouteRepositoryContext.java +++ b/sop-common/sop-gateway-common/src/main/java/com/gitee/sop/gatewaycommon/manager/RouteRepositoryContext.java @@ -1,6 +1,7 @@ package com.gitee.sop.gatewaycommon.manager; import com.gitee.sop.gatewaycommon.bean.TargetRoute; +import com.gitee.sop.gatewaycommon.message.ErrorEnum; /** * @author tanghc @@ -20,4 +21,16 @@ public class RouteRepositoryContext { RouteRepositoryContext.routeRepository = routeRepository; } + /** + * 检查路由是否存在,不存在报错 + * @param routeId 路由id + * @param errorEnum 报错信息 + */ + public static void checkExist(String routeId, ErrorEnum errorEnum) { + TargetRoute targetRoute = routeRepository.get(routeId); + if (targetRoute == null) { + throw errorEnum.getErrorMeta().getException(); + } + } + } diff --git a/sop-common/sop-gateway-common/src/main/java/com/gitee/sop/gatewaycommon/param/BaseParamBuilder.java b/sop-common/sop-gateway-common/src/main/java/com/gitee/sop/gatewaycommon/param/BaseParamBuilder.java index 2d38fad2..3fb0613a 100644 --- a/sop-common/sop-gateway-common/src/main/java/com/gitee/sop/gatewaycommon/param/BaseParamBuilder.java +++ b/sop-common/sop-gateway-common/src/main/java/com/gitee/sop/gatewaycommon/param/BaseParamBuilder.java @@ -7,7 +7,6 @@ import com.gitee.sop.gatewaycommon.manager.RouteRepositoryContext; import com.gitee.sop.gatewaycommon.message.ErrorEnum; import lombok.extern.slf4j.Slf4j; import org.apache.commons.lang3.BooleanUtils; -import org.apache.commons.lang3.StringUtils; import java.util.Map; import java.util.Optional; @@ -36,23 +35,20 @@ public abstract class BaseParamBuilder implements ParamBuilder { } protected void initOtherProperty(ApiParam apiParam) { - String nameVersion = apiParam.fetchNameVersion(); - if (StringUtils.isBlank(nameVersion)) { - throw ErrorEnum.ISV_INVALID_METHOD.getErrorMeta().getException(); - } RouteRepository routeRepository = RouteRepositoryContext.getRouteRepository(); if (routeRepository == null) { log.error("RouteRepositoryContext.setRouteRepository()方法未使用"); throw ErrorEnum.AOP_UNKNOW_ERROR.getErrorMeta().getException(); } + + String nameVersion = Optional.ofNullable(apiParam.fetchNameVersion()).orElse(String.valueOf(System.currentTimeMillis())); TargetRoute targetRoute = routeRepository.get(nameVersion); - BaseRouteDefinition routeDefinition = Optional.ofNullable(targetRoute) + Integer ignoreValidate = Optional.ofNullable(targetRoute) .map(t -> t.getRouteDefinition()) - .orElse(null); - if (routeDefinition == null) { - throw ErrorEnum.ISV_INVALID_METHOD.getErrorMeta().getException(); - } - apiParam.setIgnoreValidate(BooleanUtils.toBoolean(routeDefinition.getIgnoreValidate())); + .map(BaseRouteDefinition::getIgnoreValidate) + // 默认不忽略 + .orElse(BooleanUtils.toInteger(false)); + apiParam.setIgnoreValidate(BooleanUtils.toBoolean(ignoreValidate)); } } diff --git a/sop-common/sop-gateway-common/src/main/java/com/gitee/sop/gatewaycommon/util/RouteUtil.java b/sop-common/sop-gateway-common/src/main/java/com/gitee/sop/gatewaycommon/util/RouteUtil.java index d763a7a6..773e1486 100644 --- a/sop-common/sop-gateway-common/src/main/java/com/gitee/sop/gatewaycommon/util/RouteUtil.java +++ b/sop-common/sop-gateway-common/src/main/java/com/gitee/sop/gatewaycommon/util/RouteUtil.java @@ -1,35 +1,17 @@ package com.gitee.sop.gatewaycommon.util; -import com.gitee.sop.gatewaycommon.bean.ApiConfig; -import com.gitee.sop.gatewaycommon.bean.RouteConfig; -import com.gitee.sop.gatewaycommon.manager.RouteConfigManager; -import com.gitee.sop.gatewaycommon.message.ErrorEnum; -import com.gitee.sop.gatewaycommon.param.ApiParam; - /** * @author tanghc */ public class RouteUtil { - private RouteUtil(){} + private RouteUtil() { + } private static final String REGEX = "\\#"; public static final String PROTOCOL_LOAD_BALANCE = "lb://"; - /** - * 检测能否访问 - * @param param 接口参数 - */ - public static void checkEnable(ApiParam param) { - String routeId = param.fetchNameVersion(); - RouteConfigManager routeConfigManager = ApiConfig.getInstance().getRouteConfigManager(); - RouteConfig routeConfig = routeConfigManager.get(routeId); - if (!routeConfig.enable()) { - throw ErrorEnum.ISP_API_DISABLED.getErrorMeta().getException(); - } - } - public static String findPath(String uri) { // #后面是对应的path String[] uriArr = uri.split(REGEX); diff --git a/sop-common/sop-gateway-common/src/main/java/com/gitee/sop/gatewaycommon/validate/ApiValidator.java b/sop-common/sop-gateway-common/src/main/java/com/gitee/sop/gatewaycommon/validate/ApiValidator.java index 12b5eede..f9bffe6d 100644 --- a/sop-common/sop-gateway-common/src/main/java/com/gitee/sop/gatewaycommon/validate/ApiValidator.java +++ b/sop-common/sop-gateway-common/src/main/java/com/gitee/sop/gatewaycommon/validate/ApiValidator.java @@ -3,6 +3,9 @@ package com.gitee.sop.gatewaycommon.validate; import com.gitee.sop.gatewaycommon.bean.ApiConfig; import com.gitee.sop.gatewaycommon.bean.ApiContext; import com.gitee.sop.gatewaycommon.bean.Isv; +import com.gitee.sop.gatewaycommon.bean.RouteConfig; +import com.gitee.sop.gatewaycommon.manager.RouteConfigManager; +import com.gitee.sop.gatewaycommon.manager.RouteRepositoryContext; import com.gitee.sop.gatewaycommon.message.ErrorEnum; import com.gitee.sop.gatewaycommon.param.ApiParam; import com.gitee.sop.gatewaycommon.param.ParamNames; @@ -39,25 +42,38 @@ public class ApiValidator implements Validator { @Override public void validate(ApiParam param) { - try { - ApiConfig apiConfig = ApiContext.getApiConfig(); - if (apiConfig.isIgnoreValidate() || param.fetchIgnoreValidate()) { - if (logger.isDebugEnabled()) { - logger.debug("忽略所有验证(ignoreValidate=true), name:{}, version:{}", param.fetchName(), param.fetchVersion()); - } - return; + checkEnable(param); + + ApiConfig apiConfig = ApiContext.getApiConfig(); + if (apiConfig.isIgnoreValidate() || param.fetchIgnoreValidate()) { + if (logger.isDebugEnabled()) { + logger.debug("忽略所有验证(ignoreValidate=true), name:{}, version:{}", param.fetchName(), param.fetchVersion()); } - // 需要验证签名,先校验appKey,后校验签名,顺序不能变 - checkAppKey(param); - checkSign(param); - checkTimeout(param); - checkFormat(param); - checkUploadFile(param); - } finally { - param.fitNameVersion(); + return; } + // 需要验证签名,先校验appKey,后校验签名,顺序不能变 + checkAppKey(param); + checkSign(param); + checkTimeout(param); + checkFormat(param); + checkUploadFile(param); } + /** + * 检测能否访问 + * @param param 接口参数 + */ + protected void checkEnable(ApiParam param) { + String routeId = param.fetchNameVersion(); + // 检查路由是否存在 + RouteRepositoryContext.checkExist(routeId, ErrorEnum.ISV_INVALID_METHOD); + // 检查路由是否启用 + RouteConfigManager routeConfigManager = ApiConfig.getInstance().getRouteConfigManager(); + RouteConfig routeConfig = routeConfigManager.get(routeId); + if (!routeConfig.enable()) { + throw ErrorEnum.ISP_API_DISABLED.getErrorMeta().getException(); + } + } /** * 校验上传文件内容 diff --git a/sop-common/sop-gateway-common/src/main/java/com/gitee/sop/gatewaycommon/zuul/filter/PreValidateFilter.java b/sop-common/sop-gateway-common/src/main/java/com/gitee/sop/gatewaycommon/zuul/filter/PreValidateFilter.java index 02040d68..a0a8b6ac 100644 --- a/sop-common/sop-gateway-common/src/main/java/com/gitee/sop/gatewaycommon/zuul/filter/PreValidateFilter.java +++ b/sop-common/sop-gateway-common/src/main/java/com/gitee/sop/gatewaycommon/zuul/filter/PreValidateFilter.java @@ -4,7 +4,6 @@ import com.gitee.sop.gatewaycommon.bean.ApiConfig; import com.gitee.sop.gatewaycommon.bean.ApiContext; import com.gitee.sop.gatewaycommon.exception.ApiException; import com.gitee.sop.gatewaycommon.param.ApiParam; -import com.gitee.sop.gatewaycommon.util.RouteUtil; import com.gitee.sop.gatewaycommon.validate.Validator; import com.gitee.sop.gatewaycommon.zuul.ZuulContext; import com.netflix.zuul.context.RequestContext; @@ -12,6 +11,7 @@ import com.netflix.zuul.exception.ZuulException; /** * 前置校验 + * * @author tanghc */ public class PreValidateFilter extends BaseZuulFilter { @@ -31,7 +31,6 @@ public class PreValidateFilter extends BaseZuulFilter { // 解析参数 ApiParam param = apiConfig.getZuulParamBuilder().build(requestContext); ZuulContext.setApiParam(param); - RouteUtil.checkEnable(param); // 验证操作,这里有负责验证签名参数 Validator validator = apiConfig.getValidator(); try { @@ -39,6 +38,8 @@ public class PreValidateFilter extends BaseZuulFilter { } catch (ApiException e) { log.error("验证失败,params:{}", param.toJSONString(), e); throw e; + } finally { + param.fitNameVersion(); } return null; } diff --git a/sop-common/sop-gateway-common/src/main/java/com/gitee/sop/gatewaycommon/zuul/route/ZuulRouteRepository.java b/sop-common/sop-gateway-common/src/main/java/com/gitee/sop/gatewaycommon/zuul/route/ZuulRouteRepository.java index fe051d20..77681110 100644 --- a/sop-common/sop-gateway-common/src/main/java/com/gitee/sop/gatewaycommon/zuul/route/ZuulRouteRepository.java +++ b/sop-common/sop-gateway-common/src/main/java/com/gitee/sop/gatewaycommon/zuul/route/ZuulRouteRepository.java @@ -21,6 +21,9 @@ public class ZuulRouteRepository implements RouteRepository { @Override public ZuulTargetRoute get(String id) { + if (id == null) { + return null; + } return nameVersionTargetRouteMap.get(id); }