优化验证逻辑

1.x
tanghc 6 years ago
parent d4ce16ff3a
commit 8fd88052c5
  1. 2
      sop-common/sop-gateway-common/src/main/java/com/gitee/sop/gatewaycommon/gateway/filter/ValidateFilter.java
  2. 4
      sop-common/sop-gateway-common/src/main/java/com/gitee/sop/gatewaycommon/gateway/route/GatewayRouteRepository.java
  3. 13
      sop-common/sop-gateway-common/src/main/java/com/gitee/sop/gatewaycommon/manager/RouteRepositoryContext.java
  4. 18
      sop-common/sop-gateway-common/src/main/java/com/gitee/sop/gatewaycommon/param/BaseParamBuilder.java
  5. 22
      sop-common/sop-gateway-common/src/main/java/com/gitee/sop/gatewaycommon/util/RouteUtil.java
  6. 24
      sop-common/sop-gateway-common/src/main/java/com/gitee/sop/gatewaycommon/validate/ApiValidator.java
  7. 5
      sop-common/sop-gateway-common/src/main/java/com/gitee/sop/gatewaycommon/zuul/filter/PreValidateFilter.java
  8. 3
      sop-common/sop-gateway-common/src/main/java/com/gitee/sop/gatewaycommon/zuul/route/ZuulRouteRepository.java

@ -5,7 +5,6 @@ import com.gitee.sop.gatewaycommon.bean.ApiContext;
import com.gitee.sop.gatewaycommon.bean.SopConstants; import com.gitee.sop.gatewaycommon.bean.SopConstants;
import com.gitee.sop.gatewaycommon.exception.ApiException; import com.gitee.sop.gatewaycommon.exception.ApiException;
import com.gitee.sop.gatewaycommon.param.ApiParam; 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.validate.Validator;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import org.springframework.cloud.gateway.filter.GatewayFilterChain; import org.springframework.cloud.gateway.filter.GatewayFilterChain;
@ -26,7 +25,6 @@ public class ValidateFilter implements GlobalFilter, Ordered {
// 解析参数 // 解析参数
ApiParam param = apiConfig.getGatewayParamBuilder().build(exchange); ApiParam param = apiConfig.getGatewayParamBuilder().build(exchange);
exchange.getAttributes().put(SopConstants.CACHE_API_PARAM, param); exchange.getAttributes().put(SopConstants.CACHE_API_PARAM, param);
RouteUtil.checkEnable(param);
// 验证操作,这里有负责验证签名参数 // 验证操作,这里有负责验证签名参数
Validator validator = apiConfig.getValidator(); Validator validator = apiConfig.getValidator();
try { try {

@ -11,7 +11,6 @@ import org.springframework.cloud.gateway.event.PredicateArgsEvent;
import org.springframework.cloud.gateway.event.RefreshRoutesEvent; import org.springframework.cloud.gateway.event.RefreshRoutesEvent;
import org.springframework.cloud.gateway.handler.predicate.PredicateDefinition; import org.springframework.cloud.gateway.handler.predicate.PredicateDefinition;
import org.springframework.cloud.gateway.handler.predicate.RoutePredicateFactory; 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.RouteDefinition;
import org.springframework.cloud.gateway.route.RouteDefinitionRepository; import org.springframework.cloud.gateway.route.RouteDefinitionRepository;
import org.springframework.cloud.gateway.support.ConfigurationUtils; import org.springframework.cloud.gateway.support.ConfigurationUtils;
@ -80,6 +79,9 @@ public class GatewayRouteRepository implements ApplicationEventPublisherAware,
*/ */
@Override @Override
public GatewayTargetRoute get(String id) { public GatewayTargetRoute get(String id) {
if (id == null) {
return null;
}
return routes.get(id); return routes.get(id);
} }

@ -1,6 +1,7 @@
package com.gitee.sop.gatewaycommon.manager; package com.gitee.sop.gatewaycommon.manager;
import com.gitee.sop.gatewaycommon.bean.TargetRoute; import com.gitee.sop.gatewaycommon.bean.TargetRoute;
import com.gitee.sop.gatewaycommon.message.ErrorEnum;
/** /**
* @author tanghc * @author tanghc
@ -20,4 +21,16 @@ public class RouteRepositoryContext {
RouteRepositoryContext.routeRepository = routeRepository; 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();
}
}
} }

@ -7,7 +7,6 @@ import com.gitee.sop.gatewaycommon.manager.RouteRepositoryContext;
import com.gitee.sop.gatewaycommon.message.ErrorEnum; import com.gitee.sop.gatewaycommon.message.ErrorEnum;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.BooleanUtils; import org.apache.commons.lang3.BooleanUtils;
import org.apache.commons.lang3.StringUtils;
import java.util.Map; import java.util.Map;
import java.util.Optional; import java.util.Optional;
@ -36,23 +35,20 @@ public abstract class BaseParamBuilder<T> implements ParamBuilder<T> {
} }
protected void initOtherProperty(ApiParam apiParam) { protected void initOtherProperty(ApiParam apiParam) {
String nameVersion = apiParam.fetchNameVersion();
if (StringUtils.isBlank(nameVersion)) {
throw ErrorEnum.ISV_INVALID_METHOD.getErrorMeta().getException();
}
RouteRepository<? extends TargetRoute> routeRepository = RouteRepositoryContext.getRouteRepository(); RouteRepository<? extends TargetRoute> routeRepository = RouteRepositoryContext.getRouteRepository();
if (routeRepository == null) { if (routeRepository == null) {
log.error("RouteRepositoryContext.setRouteRepository()方法未使用"); log.error("RouteRepositoryContext.setRouteRepository()方法未使用");
throw ErrorEnum.AOP_UNKNOW_ERROR.getErrorMeta().getException(); throw ErrorEnum.AOP_UNKNOW_ERROR.getErrorMeta().getException();
} }
String nameVersion = Optional.ofNullable(apiParam.fetchNameVersion()).orElse(String.valueOf(System.currentTimeMillis()));
TargetRoute targetRoute = routeRepository.get(nameVersion); TargetRoute targetRoute = routeRepository.get(nameVersion);
BaseRouteDefinition routeDefinition = Optional.ofNullable(targetRoute) Integer ignoreValidate = Optional.ofNullable(targetRoute)
.map(t -> t.getRouteDefinition()) .map(t -> t.getRouteDefinition())
.orElse(null); .map(BaseRouteDefinition::getIgnoreValidate)
if (routeDefinition == null) { // 默认不忽略
throw ErrorEnum.ISV_INVALID_METHOD.getErrorMeta().getException(); .orElse(BooleanUtils.toInteger(false));
} apiParam.setIgnoreValidate(BooleanUtils.toBoolean(ignoreValidate));
apiParam.setIgnoreValidate(BooleanUtils.toBoolean(routeDefinition.getIgnoreValidate()));
} }
} }

@ -1,35 +1,17 @@
package com.gitee.sop.gatewaycommon.util; 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 * @author tanghc
*/ */
public class RouteUtil { public class RouteUtil {
private RouteUtil(){} private RouteUtil() {
}
private static final String REGEX = "\\#"; private static final String REGEX = "\\#";
public static final String PROTOCOL_LOAD_BALANCE = "lb://"; 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) { public static String findPath(String uri) {
// #后面是对应的path // #后面是对应的path
String[] uriArr = uri.split(REGEX); String[] uriArr = uri.split(REGEX);

@ -3,6 +3,9 @@ package com.gitee.sop.gatewaycommon.validate;
import com.gitee.sop.gatewaycommon.bean.ApiConfig; import com.gitee.sop.gatewaycommon.bean.ApiConfig;
import com.gitee.sop.gatewaycommon.bean.ApiContext; import com.gitee.sop.gatewaycommon.bean.ApiContext;
import com.gitee.sop.gatewaycommon.bean.Isv; 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.message.ErrorEnum;
import com.gitee.sop.gatewaycommon.param.ApiParam; import com.gitee.sop.gatewaycommon.param.ApiParam;
import com.gitee.sop.gatewaycommon.param.ParamNames; import com.gitee.sop.gatewaycommon.param.ParamNames;
@ -39,7 +42,8 @@ public class ApiValidator implements Validator {
@Override @Override
public void validate(ApiParam param) { public void validate(ApiParam param) {
try { checkEnable(param);
ApiConfig apiConfig = ApiContext.getApiConfig(); ApiConfig apiConfig = ApiContext.getApiConfig();
if (apiConfig.isIgnoreValidate() || param.fetchIgnoreValidate()) { if (apiConfig.isIgnoreValidate() || param.fetchIgnoreValidate()) {
if (logger.isDebugEnabled()) { if (logger.isDebugEnabled()) {
@ -53,11 +57,23 @@ public class ApiValidator implements Validator {
checkTimeout(param); checkTimeout(param);
checkFormat(param); checkFormat(param);
checkUploadFile(param); checkUploadFile(param);
} finally {
param.fitNameVersion();
}
} }
/**
* 检测能否访问
* @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();
}
}
/** /**
* 校验上传文件内容 * 校验上传文件内容

@ -4,7 +4,6 @@ import com.gitee.sop.gatewaycommon.bean.ApiConfig;
import com.gitee.sop.gatewaycommon.bean.ApiContext; import com.gitee.sop.gatewaycommon.bean.ApiContext;
import com.gitee.sop.gatewaycommon.exception.ApiException; import com.gitee.sop.gatewaycommon.exception.ApiException;
import com.gitee.sop.gatewaycommon.param.ApiParam; 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.validate.Validator;
import com.gitee.sop.gatewaycommon.zuul.ZuulContext; import com.gitee.sop.gatewaycommon.zuul.ZuulContext;
import com.netflix.zuul.context.RequestContext; import com.netflix.zuul.context.RequestContext;
@ -12,6 +11,7 @@ import com.netflix.zuul.exception.ZuulException;
/** /**
* 前置校验 * 前置校验
*
* @author tanghc * @author tanghc
*/ */
public class PreValidateFilter extends BaseZuulFilter { public class PreValidateFilter extends BaseZuulFilter {
@ -31,7 +31,6 @@ public class PreValidateFilter extends BaseZuulFilter {
// 解析参数 // 解析参数
ApiParam param = apiConfig.getZuulParamBuilder().build(requestContext); ApiParam param = apiConfig.getZuulParamBuilder().build(requestContext);
ZuulContext.setApiParam(param); ZuulContext.setApiParam(param);
RouteUtil.checkEnable(param);
// 验证操作,这里有负责验证签名参数 // 验证操作,这里有负责验证签名参数
Validator validator = apiConfig.getValidator(); Validator validator = apiConfig.getValidator();
try { try {
@ -39,6 +38,8 @@ public class PreValidateFilter extends BaseZuulFilter {
} catch (ApiException e) { } catch (ApiException e) {
log.error("验证失败,params:{}", param.toJSONString(), e); log.error("验证失败,params:{}", param.toJSONString(), e);
throw e; throw e;
} finally {
param.fitNameVersion();
} }
return null; return null;
} }

@ -21,6 +21,9 @@ public class ZuulRouteRepository implements RouteRepository<ZuulTargetRoute> {
@Override @Override
public ZuulTargetRoute get(String id) { public ZuulTargetRoute get(String id) {
if (id == null) {
return null;
}
return nameVersionTargetRouteMap.get(id); return nameVersionTargetRouteMap.get(id);
} }

Loading…
Cancel
Save