org.springframework.cloud.gateway.filter.GlobalFilter Java Examples

The following examples show how to use org.springframework.cloud.gateway.filter.GlobalFilter. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: GatewayConfig.java    From iot-dc3 with Apache License 2.0 6 votes vote down vote up
@Bean
@Order(-100)
public GlobalFilter globalFilter() {
    return (exchange, chain) -> {
        //调用请求之前统计时间
        Long startTime = System.currentTimeMillis();

        ServerHttpRequest request = exchange.getRequest();
        String remoteIp = Objects.requireNonNull(request.getRemoteAddress()).getHostString();
        R<Boolean> blackIpValid = blackIpClient.checkBlackIpValid(remoteIp);
        if (blackIpValid.isOk()) {
            log.error("Forbidden Ip: {}", remoteIp);
            exchange.getResponse().setStatusCode(HttpStatus.FORBIDDEN);
            return exchange.getResponse().setComplete();
        }
        return chain.filter(exchange).then().then(Mono.fromRunnable(() -> {
            //调用请求之后统计时间
            Long endTime = System.currentTimeMillis();

            log.info("Remote Ip: {}; Request url: {}; Response code: {}; Time: {}ms", remoteIp, request.getURI().getRawPath(), exchange.getResponse().getStatusCode(), (endTime - startTime));
        }));
    };
}
 
Example #2
Source File: BaseWebClientTests.java    From spring-cloud-gateway with Apache License 2.0 6 votes vote down vote up
@Bean
@Order(500)
public GlobalFilter modifyResponseFilter() {
	return (exchange, chain) -> {
		log.info("modifyResponseFilter start");
		String value = exchange.getAttributeOrDefault(GATEWAY_HANDLER_MAPPER_ATTR,
				"N/A");
		if (!exchange.getResponse().isCommitted()) {
			exchange.getResponse().getHeaders().add(HANDLER_MAPPER_HEADER, value);
		}
		Route route = exchange.getAttributeOrDefault(GATEWAY_ROUTE_ATTR, null);
		if (route != null) {
			if (!exchange.getResponse().isCommitted()) {
				exchange.getResponse().getHeaders().add(ROUTE_ID_HEADER,
						route.getId());
			}
		}
		return chain.filter(exchange);
	};
}
 
Example #3
Source File: LoggingGlobalFiltersConfigurations.java    From tutorials with MIT License 5 votes vote down vote up
@Bean
public GlobalFilter postGlobalFilter() {
    return (exchange, chain) -> {
        return chain.filter(exchange)
            .then(Mono.fromRunnable(() -> {
                logger.info("Global Post Filter executed");
            }));
    };
}
 
Example #4
Source File: NonStandardHeadersInResponseTests.java    From spring-cloud-gateway with Apache License 2.0 5 votes vote down vote up
@Bean
@Order(5001)
public GlobalFilter addNonStandardHeaderFilter() {
	return (exchange, chain) -> {
		log.info("addNonStandardHeaderFilter pre phase");
		return chain.filter(exchange).then(Mono.fromRunnable(() -> {
			log.info("addNonStandardHeaderFilter post phase");
			exchange.getResponse().getHeaders().set(HttpHeaders.CONTENT_TYPE,
					CONTENT_TYPE_IMAGE);
		}));
	};
}
 
Example #5
Source File: GatewayIntegrationTests.java    From spring-cloud-gateway with Apache License 2.0 5 votes vote down vote up
@Bean
@Order(-1)
public GlobalFilter postFilter() {
	return (exchange, chain) -> {
		log.info("postFilter start");
		return chain.filter(exchange).then(postFilterWork(exchange));
	};
}
 
Example #6
Source File: AbstractGatewayControllerEndpoint.java    From spring-cloud-gateway with Apache License 2.0 5 votes vote down vote up
public AbstractGatewayControllerEndpoint(
		RouteDefinitionLocator routeDefinitionLocator,
		List<GlobalFilter> globalFilters, List<GatewayFilterFactory> gatewayFilters,
		List<RoutePredicateFactory> routePredicates,
		RouteDefinitionWriter routeDefinitionWriter, RouteLocator routeLocator) {
	this.routeDefinitionLocator = routeDefinitionLocator;
	this.globalFilters = globalFilters;
	this.GatewayFilters = gatewayFilters;
	this.routePredicates = routePredicates;
	this.routeDefinitionWriter = routeDefinitionWriter;
	this.routeLocator = routeLocator;
}
 
Example #7
Source File: GatewayControllerEndpoint.java    From spring-cloud-gateway with Apache License 2.0 5 votes vote down vote up
public GatewayControllerEndpoint(List<GlobalFilter> globalFilters,
		List<GatewayFilterFactory> gatewayFilters,
		List<RoutePredicateFactory> routePredicates,
		RouteDefinitionWriter routeDefinitionWriter, RouteLocator routeLocator) {
	super(null, globalFilters, gatewayFilters, routePredicates, routeDefinitionWriter,
			routeLocator);
}
 
Example #8
Source File: GatewayLegacyControllerEndpoint.java    From spring-cloud-gateway with Apache License 2.0 5 votes vote down vote up
public GatewayLegacyControllerEndpoint(RouteDefinitionLocator routeDefinitionLocator,
		List<GlobalFilter> globalFilters, List<GatewayFilterFactory> GatewayFilters,
		List<RoutePredicateFactory> routePredicates,
		RouteDefinitionWriter routeDefinitionWriter, RouteLocator routeLocator) {
	super(routeDefinitionLocator, globalFilters, GatewayFilters, routePredicates,
			routeDefinitionWriter, routeLocator);
}
 
Example #9
Source File: GatewayAutoConfiguration.java    From spring-cloud-gateway with Apache License 2.0 5 votes vote down vote up
@Bean
@Conditional(OnVerboseDisabledCondition.class)
@ConditionalOnAvailableEndpoint
public GatewayLegacyControllerEndpoint gatewayLegacyControllerEndpoint(
		RouteDefinitionLocator routeDefinitionLocator,
		List<GlobalFilter> globalFilters,
		List<GatewayFilterFactory> gatewayFilters,
		List<RoutePredicateFactory> routePredicates,
		RouteDefinitionWriter routeDefinitionWriter, RouteLocator routeLocator) {
	return new GatewayLegacyControllerEndpoint(routeDefinitionLocator,
			globalFilters, gatewayFilters, routePredicates, routeDefinitionWriter,
			routeLocator);
}
 
Example #10
Source File: FilteringWebHandler.java    From spring-cloud-gateway with Apache License 2.0 5 votes vote down vote up
private static List<GatewayFilter> loadFilters(List<GlobalFilter> filters) {
	return filters.stream().map(filter -> {
		GatewayFilterAdapter gatewayFilter = new GatewayFilterAdapter(filter);
		if (filter instanceof Ordered) {
			int order = ((Ordered) filter).getOrder();
			return new OrderedGatewayFilter(gatewayFilter, order);
		}
		return gatewayFilter;
	}).collect(Collectors.toList());
}
 
Example #11
Source File: GatewayAutoConfiguration.java    From spring-cloud-gateway with Apache License 2.0 5 votes vote down vote up
@Bean
@ConditionalOnProperty(name = "spring.cloud.gateway.actuator.verbose.enabled",
		matchIfMissing = true)
@ConditionalOnAvailableEndpoint
public GatewayControllerEndpoint gatewayControllerEndpoint(
		List<GlobalFilter> globalFilters,
		List<GatewayFilterFactory> gatewayFilters,
		List<RoutePredicateFactory> routePredicates,
		RouteDefinitionWriter routeDefinitionWriter, RouteLocator routeLocator) {
	return new GatewayControllerEndpoint(globalFilters, gatewayFilters,
			routePredicates, routeDefinitionWriter, routeLocator);
}
 
Example #12
Source File: FilteringWebHandler.java    From spring-cloud-gateway with Apache License 2.0 4 votes vote down vote up
public FilteringWebHandler(List<GlobalFilter> globalFilters) {
	this.globalFilters = loadFilters(globalFilters);
}
 
Example #13
Source File: GatewayAutoConfiguration.java    From spring-cloud-gateway with Apache License 2.0 4 votes vote down vote up
@Bean
public FilteringWebHandler filteringWebHandler(List<GlobalFilter> globalFilters) {
	return new FilteringWebHandler(globalFilters);
}
 
Example #14
Source File: FilteringWebHandler.java    From spring-cloud-gateway with Apache License 2.0 4 votes vote down vote up
GatewayFilterAdapter(GlobalFilter delegate) {
	this.delegate = delegate;
}
 
Example #15
Source File: GatewayApplication.java    From WeEvent with Apache License 2.0 4 votes vote down vote up
@Bean
public static GlobalFilter getLogGlobalFilter() {
    return new LogGlobalFilter();
}
 
Example #16
Source File: SentinelConfig.java    From lion with Apache License 2.0 4 votes vote down vote up
/**
 * 配置SentinelGatewayFilter
 */
@Bean
@Order(-1)
public GlobalFilter sentinelGatewayFilter() {
    return new SentinelGatewayFilter();
}
 
Example #17
Source File: GatewayConfiguration.java    From Sentinel with Apache License 2.0 4 votes vote down vote up
@Bean
@Order(-1)
public GlobalFilter sentinelGatewayFilter() {
    return new SentinelGatewayFilter();
}
 
Example #18
Source File: GatewayConfiguration.java    From spring-cloud-sofastack-samples with Apache License 2.0 4 votes vote down vote up
/**
 * 配置SentinelGatewayFilter
 * @return
 */
@Bean
@Order(-1)
public GlobalFilter sentinelGatewayFilter() {
    return new SentinelGatewayFilter();
}
 
Example #19
Source File: GatewayConfiguration.java    From Sentinel-Dashboard-Nacos with Apache License 2.0 4 votes vote down vote up
@Bean
@Order(-1)
public GlobalFilter sentinelGatewayFilter() {
    return new SentinelGatewayFilter();
}