Merge branch 'master' into eureka

eureka
tanghc 4 years ago
commit 6a22223e8b
  1. 3
      doc/docs/files/10089_自定义校验token.md
  2. 2
      sop-common/sop-gateway-common/src/main/java/com/gitee/sop/gatewaycommon/bean/SopConstants.java
  3. 5
      sop-common/sop-gateway-common/src/main/java/com/gitee/sop/gatewaycommon/gateway/handler/GatewayExceptionHandler.java
  4. 7
      sop-common/sop-gateway-common/src/main/java/com/gitee/sop/gatewaycommon/gateway/route/GatewayForwardChooser.java

@ -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真实、有效
});
});
}
}
```

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

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

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

Loading…
Cancel
Save