|
|
@@ -1,5 +1,6 @@
|
|
|
package com.jzg.filter;
|
|
|
|
|
|
+import lombok.extern.slf4j.Slf4j;
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
import org.springframework.cloud.gateway.filter.GatewayFilterChain;
|
|
|
import org.springframework.cloud.gateway.filter.GlobalFilter;
|
|
|
@@ -8,8 +9,13 @@ import org.springframework.core.annotation.Order;
|
|
|
import org.springframework.stereotype.Component;
|
|
|
import org.springframework.web.server.ServerWebExchange;
|
|
|
import reactor.core.publisher.Mono;
|
|
|
+
|
|
|
+import java.time.Duration;
|
|
|
+import java.time.Instant;
|
|
|
+
|
|
|
@Component
|
|
|
@Order(0)
|
|
|
+@Slf4j
|
|
|
public class AuthFilter implements GlobalFilter {
|
|
|
|
|
|
@Autowired
|
|
|
@@ -20,8 +26,17 @@ public class AuthFilter implements GlobalFilter {
|
|
|
// 获取请求路径和方法
|
|
|
String path = exchange.getRequest().getPath().value();
|
|
|
|
|
|
- // 如果权限验证通过,继续请求链
|
|
|
- return chain.filter(exchange);
|
|
|
+ return chain.filter(exchange)
|
|
|
+ .doOnSuccess(v -> {
|
|
|
+ long cost = Duration.between(Instant.now(), Instant.now()).toMillis();
|
|
|
+ int statusCode = exchange.getResponse().getStatusCode().value();
|
|
|
+ log.info("【Gateway响应出口】 请求地址:{} , 耗时: {}ms , 状态码: {} ", path, statusCode, cost);
|
|
|
+ })
|
|
|
+ // 发生异常时记录日志(ERROR级别)
|
|
|
+ .doOnError(e -> {
|
|
|
+ long cost = Duration.between(Instant.now(), Instant.now()).toMillis();
|
|
|
+ log.error("【Gateway请求异常】 请求地址:{} , 耗时: {}ms, 异常信息:{} ",path,cost, e);
|
|
|
+ });
|
|
|
}
|
|
|
|
|
|
}
|