diff --git a/doc/docs/files/10089_自定义校验token.md b/doc/docs/files/10089_自定义校验token.md index c4840eba..6716ec55 100644 --- a/doc/docs/files/10089_自定义校验token.md +++ b/doc/docs/files/10089_自定义校验token.md @@ -29,7 +29,6 @@ public StoryResult token(StoryParam story, HttpServletRequest request) { 自己实现步骤: -- 在ZuulConfig类中重写`doAfter`方法 - 设置`ApiConfig中的tokenValidator属性` `TokenValidator`是一个函数式接口,可以直接使用Lambda表达式,示例代码如下: @@ -47,7 +46,7 @@ public class MyConfig { // TODO: 校验token有效性,可以从redis中读取 // 返回true表示这个token真实、有效 -}); + }); } } ``` diff --git a/sop-common/sop-gateway-common/src/main/java/com/gitee/sop/gatewaycommon/bean/SopConstants.java b/sop-common/sop-gateway-common/src/main/java/com/gitee/sop/gatewaycommon/bean/SopConstants.java index c6d56b4c..7af00f96 100644 --- a/sop-common/sop-gateway-common/src/main/java/com/gitee/sop/gatewaycommon/bean/SopConstants.java +++ b/sop-common/sop-gateway-common/src/main/java/com/gitee/sop/gatewaycommon/bean/SopConstants.java @@ -56,4 +56,6 @@ public class SopConstants { public static final String METADATA_KEY_TIME_STARTUP = "server.startup-time"; + public static final String CACHE_ROUTE_INFO = "cacheRouteInfo"; + } diff --git a/sop-common/sop-gateway-common/src/main/java/com/gitee/sop/gatewaycommon/gateway/handler/GatewayExceptionHandler.java b/sop-common/sop-gateway-common/src/main/java/com/gitee/sop/gatewaycommon/gateway/handler/GatewayExceptionHandler.java index 0f623358..4c992a9c 100644 --- a/sop-common/sop-gateway-common/src/main/java/com/gitee/sop/gatewaycommon/gateway/handler/GatewayExceptionHandler.java +++ b/sop-common/sop-gateway-common/src/main/java/com/gitee/sop/gatewaycommon/gateway/handler/GatewayExceptionHandler.java @@ -1,6 +1,8 @@ package com.gitee.sop.gatewaycommon.gateway.handler; import com.gitee.sop.gatewaycommon.bean.ApiContext; +import com.gitee.sop.gatewaycommon.bean.SopConstants; +import com.gitee.sop.gatewaycommon.bean.TargetRoute; import com.gitee.sop.gatewaycommon.gateway.ServerWebExchangeUtil; import com.gitee.sop.gatewaycommon.param.ApiParam; import com.gitee.sop.gatewaycommon.result.ResultExecutor; @@ -55,7 +57,8 @@ public class GatewayExceptionHandler implements ErrorWebExceptionHandler { String errorResult = resultExecutor.buildErrorResult(exchange, ex); ApiParam apiParam = ServerWebExchangeUtil.getApiParam(exchange); // 错误记录 - log.error("gateway网关报错,params:{}, errorMsg:{}", apiParam, ex.getMessage(), ex); + Object routeDefinition = exchange.getAttribute(SopConstants.CACHE_ROUTE_INFO); + log.error("网关报错,参数:{}, 错误信息:{}, 路由信息:{}", apiParam, ex.getMessage(), routeDefinition, ex); // 参考AbstractErrorWebExceptionHandler if (exchange.getResponse().isCommitted()) { return Mono.error(ex); diff --git a/sop-common/sop-gateway-common/src/main/java/com/gitee/sop/gatewaycommon/gateway/route/GatewayForwardChooser.java b/sop-common/sop-gateway-common/src/main/java/com/gitee/sop/gatewaycommon/gateway/route/GatewayForwardChooser.java index bb9b474d..85ced0da 100644 --- a/sop-common/sop-gateway-common/src/main/java/com/gitee/sop/gatewaycommon/gateway/route/GatewayForwardChooser.java +++ b/sop-common/sop-gateway-common/src/main/java/com/gitee/sop/gatewaycommon/gateway/route/GatewayForwardChooser.java @@ -1,5 +1,7 @@ package com.gitee.sop.gatewaycommon.gateway.route; +import com.gitee.sop.gatewaycommon.bean.SopConstants; +import com.gitee.sop.gatewaycommon.bean.TargetRoute; import com.gitee.sop.gatewaycommon.gateway.ServerWebExchangeUtil; import com.gitee.sop.gatewaycommon.param.ApiParam; import com.gitee.sop.gatewaycommon.route.BaseForwardChooser; @@ -22,6 +24,9 @@ public class GatewayForwardChooser extends BaseForwardChooser if (ServerWebExchangeUtil.getThrowable(exchange) != null) { return ForwardInfo.getErrorForwardInfo(); } - return super.getForwardInfo(exchange); + ForwardInfo forwardInfo = super.getForwardInfo(exchange); + TargetRoute targetRoute = forwardInfo.getTargetRoute(); + exchange.getAttributes().put(SopConstants.CACHE_ROUTE_INFO, targetRoute.getRouteDefinition()); + return forwardInfo; } }