你好,我在gateway自定义了一个简单的全局过滤器,但是为什么我通过gateway调用微服务时,没有经过这个全局过滤器的?
server:
port: 8100
max-http-header-size: 4048576
spring:
cloud:
nacos:
discovery:
server-addr: 127.0.0.1:8848
sentinel:
transport:
port: 8720
dashboard: 127.0.0.1:8080
gateway:
discovery:
locator:
# 让gateway通过服务发现组件找到其他的微服务
enabled: true
lower-case-service-id: true
application:
name: gateway
management:
endpoints:
web:
exposure:
include: '*'
endpoint:
health:
show-details: always
@Slf4j
@Configuration
// @EnableWebFluxSecurity
public class GatewaySecurityConfig {
@Bean
@Order(-1)
public GlobalFilter PreLog() {
return (exchange, chain) -> {
log.info("==============================first pre filter");
return chain.filter(exchange).then(Mono.fromRunnable(() -> {
ServerHttpRequest modifiedRequest = exchange.getRequest()
.mutate()
.build();
HttpHeaders headers = modifiedRequest.getHeaders();
List<String> authorization = headers.get("Authorization");
log.info("=======================token inof================={}", authorization.toString());
ServerWebExchange modifiedExchange = exchange.mutate()
.request(modifiedRequest)
.build();
log.info("third post filter");
}));
};
}
}
@Slf4j
@Component
public class PreLogGlobalGatewayFilter implements GlobalFilter, Ordered{
@Override
public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {
List<String> authorization = exchange.getRequest().getHeaders().get("Authorization");
log.info("=======================token info================={}", authorization.toString());
if (authorization == null) {
exchange.getResponse().setStatusCode(HttpStatus.UNAUTHORIZED);
return exchange.getResponse().setComplete();
}
return chain.filter(exchange);
}
/**
* Get the order value of this object.
* <p>Higher values are interpreted as lower priority. As a consequence,
* the object with the lowest value has the highest priority (somewhat
* analogous to Servlet {@code load-on-startup} values).
* <p>Same order values will result in arbitrary sort positions for the
* affected objects.
* @return the order value
* @see #HIGHEST_PRECEDENCE
* @see #LOWEST_PRECEDENCE
*/
@Override
public int getOrder() {
return 0;
}
}
@GetMapping("/getDemoInfo/{demoInfo}")
@ResponseBody
public SimpleResponse<String> getInfo(@PathVariable String demoInfo,HttpServletRequest request) throws IOException {
String access_token = CookieUtil.getToken(request, "access_token");
String demoGetwAYUrl = "http://gateway.troylc.cn:8100/sso-demo/demo/testInfo/{demoInfo}";
HttpHeaders headers = new HttpHeaders();
headers.add("Authorization", "bearer " + access_token);
ResponseEntity<SimpleResponse> returnDemoInfo = restTemplate.exchange(demoGetwAYUrl,HttpMethod.GET, new HttpEntity<>(headers),SimpleResponse.class, demoInfo);
return returnDemoInfo.getBody();
}
我从A应用中发一个调用B微服务的链接,经过gateway,但是没有进入这两个全局过滤器,这是为啥??
我直接访问gateway放开的全局过滤器的端点,也没有发现我自定义的这两个过滤器,这是为啥?
#url: http://gateway.troylc.cn:8100/actuator/gateway/globalfilters
{
org.springframework.cloud.gateway.filter.RemoveCachedBodyFilter@4978777f: -2147483648,
org.springframework.cloud.gateway.filter.ForwardRoutingFilter@67bd351e: 2147483647,
org.springframework.cloud.gateway.filter.NettyRoutingFilter@1b90fee4: 2147483647,
org.springframework.cloud.gateway.filter.RouteToRequestUrlFilter@7bc6935c: 10000,
org.springframework.cloud.gateway.filter.ForwardPathFilter@4cc7d00d: 0,
org.springframework.cloud.gateway.filter.NettyWriteResponseFilter@3513c84c: -1,
org.springframework.cloud.gateway.filter.LoadBalancerClientFilter@2e4389ed: 10100,
org.springframework.cloud.gateway.filter.WebsocketRoutingFilter@468e8565: 2147483646,
org.springframework.cloud.gateway.filter.GatewayMetricsFilter@91da29b: 0,
org.springframework.cloud.gateway.filter.AdaptCachedBodyGlobalFilter@3610f277: -2147482648
}
登录后可查看更多问答,登录/注册
面向未来微服务:熟练掌握Spring Cloud Alibaba
了解课程