优化验证逻辑

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.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 {

@ -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);
}

@ -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();
}
}
}

@ -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<T> implements ParamBuilder<T> {
}
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();
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));
}
}

@ -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);

@ -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,7 +42,8 @@ public class ApiValidator implements Validator {
@Override
public void validate(ApiParam param) {
try {
checkEnable(param);
ApiConfig apiConfig = ApiContext.getApiConfig();
if (apiConfig.isIgnoreValidate() || param.fetchIgnoreValidate()) {
if (logger.isDebugEnabled()) {
@ -53,11 +57,23 @@ public class ApiValidator implements Validator {
checkTimeout(param);
checkFormat(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.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;
}

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

Loading…
Cancel
Save