|
|
@@ -1,9 +1,9 @@
|
|
|
package space.anyi.openAPIGateway.filter;
|
|
|
|
|
|
+import org.apache.dubbo.config.annotation.DubboReference;
|
|
|
import org.reactivestreams.Publisher;
|
|
|
import org.slf4j.Logger;
|
|
|
import org.slf4j.LoggerFactory;
|
|
|
-import org.springframework.cloud.gateway.filter.GatewayFilter;
|
|
|
import org.springframework.cloud.gateway.filter.GatewayFilterChain;
|
|
|
import org.springframework.cloud.gateway.filter.GlobalFilter;
|
|
|
import org.springframework.core.Ordered;
|
|
|
@@ -17,11 +17,13 @@ import org.springframework.stereotype.Component;
|
|
|
import org.springframework.web.server.ServerWebExchange;
|
|
|
import reactor.core.CoreSubscriber;
|
|
|
import reactor.core.publisher.Mono;
|
|
|
+import space.anyi.openAPI.comom.model.interfaceInfo.InterfaceInfo;
|
|
|
+import space.anyi.openAPI.comom.model.interfaceInfo.InterfaceStatus;
|
|
|
+import space.anyi.openAPI.comom.service.inside.InsideInterfaceInfoService;
|
|
|
|
|
|
import java.util.HashSet;
|
|
|
import java.util.Objects;
|
|
|
import java.util.Set;
|
|
|
-import java.util.function.Function;
|
|
|
|
|
|
/**
|
|
|
* @ProjectName: open-api-gateway
|
|
|
@@ -38,7 +40,10 @@ public class ConsumerFilter implements GlobalFilter, Ordered {
|
|
|
* 自定义的过滤器名称规则:后缀一定要是GatewayFilterFactory
|
|
|
* 在配置文件配置自定义过滤器时只需要配置name为Consumer即可
|
|
|
*/
|
|
|
- public static final Logger log = LoggerFactory.getLogger(ConsumerFilter.class);
|
|
|
+ private static final Logger log = LoggerFactory.getLogger(ConsumerFilter.class);
|
|
|
+ @DubboReference
|
|
|
+ private InsideInterfaceInfoService insideInterfaceInfoService;
|
|
|
+
|
|
|
/**
|
|
|
* 记录时间戳和随机数nonce(防止重放攻击)
|
|
|
* @see Set<String>
|
|
|
@@ -76,16 +81,23 @@ public class ConsumerFilter implements GlobalFilter, Ordered {
|
|
|
return parameterExceptionHandler(response,"随机数不合法");
|
|
|
}
|
|
|
timestampAndNonceSet.add(timestamp + nonce);
|
|
|
- //todo:检查接口是否存在并且开通调用
|
|
|
- //todo:是否还有调用次数
|
|
|
+ //检查接口是否存在并且开通调用
|
|
|
+ String path = url.subPath(2).toString();
|
|
|
+ log.debug("path:{}", path);
|
|
|
+ String method = request.getMethod().name();
|
|
|
+ log.debug("method:{}", method);
|
|
|
+ InterfaceInfo interfaceInfo = insideInterfaceInfoService.getInterfaceInfoByUrlAndMethod(path, method);
|
|
|
+ log.debug("interfaceInfo:{}", interfaceInfo);
|
|
|
+ if (Objects.isNull(interfaceInfo)|| InterfaceStatus.offline.getStatus() == interfaceInfo.getStatus()) {
|
|
|
+ response.setStatusCode(HttpStatus.NOT_FOUND);
|
|
|
+ return response.setComplete();
|
|
|
+ }
|
|
|
//todo:校验accessKey和secretKey
|
|
|
+ //todo:是否还有调用次数
|
|
|
|
|
|
- return chain.filter(exchange).then(new Mono<Void>() {
|
|
|
- @Override
|
|
|
- public void subscribe(CoreSubscriber<? super Void> actual) {
|
|
|
- //todo:过滤器后置处理
|
|
|
- log.info("过滤器后置处理");
|
|
|
- }
|
|
|
+ return chain.filter(exchange).doOnSuccess(aVoid -> {
|
|
|
+ //todo:过滤器后置处理
|
|
|
+ log.info("过滤器后置处理");
|
|
|
});
|
|
|
}
|
|
|
|