org.springframework.cloud.gateway.filter.ratelimit.KeyResolver Java Examples

The following examples show how to use org.springframework.cloud.gateway.filter.ratelimit.KeyResolver. 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: GatewayApplication.java    From WeEvent with Apache License 2.0 4 votes vote down vote up
@Bean(name = UriKeyResolver.BEAN_NAME)
public KeyResolver getUriKeyResolver() {
    return new UriKeyResolver();
}
 
Example #2
Source File: RequestRateLimiterResolverConfig.java    From tutorials with MIT License 4 votes vote down vote up
@Bean
KeyResolver userKeyResolver() {
    return exchange -> Mono.just("1");
}
 
Example #3
Source File: RequestRateLimiterGatewayFilterFactoryTests.java    From spring-cloud-gateway with Apache License 2.0 4 votes vote down vote up
@Bean
KeyResolver resolver2() {
	return exchange -> Mono.just("notallowedkey");
}
 
Example #4
Source File: RequestRateLimiterGatewayFilterFactoryTests.java    From spring-cloud-gateway with Apache License 2.0 4 votes vote down vote up
@Bean
@Primary
KeyResolver resolver1() {
	return exchange -> Mono.just("allowedkey");
}
 
Example #5
Source File: RequestRateLimiterGatewayFilterFactoryTests.java    From spring-cloud-gateway with Apache License 2.0 4 votes vote down vote up
private void assertFilterFactory(KeyResolver keyResolver, String key, boolean allowed,
		HttpStatus expectedStatus) {
	assertFilterFactory(keyResolver, key, allowed, expectedStatus, null);
}
 
Example #6
Source File: RequestRateLimiterGatewayFilterFactory.java    From spring-cloud-gateway with Apache License 2.0 4 votes vote down vote up
public Config setKeyResolver(KeyResolver keyResolver) {
	this.keyResolver = keyResolver;
	return this;
}
 
Example #7
Source File: RequestRateLimiterGatewayFilterFactory.java    From spring-cloud-gateway with Apache License 2.0 4 votes vote down vote up
public KeyResolver getKeyResolver() {
	return keyResolver;
}
 
Example #8
Source File: RequestRateLimiterGatewayFilterFactory.java    From spring-cloud-gateway with Apache License 2.0 4 votes vote down vote up
@SuppressWarnings("unchecked")
@Override
public GatewayFilter apply(Config config) {
	KeyResolver resolver = getOrDefault(config.keyResolver, defaultKeyResolver);
	RateLimiter<Object> limiter = getOrDefault(config.rateLimiter,
			defaultRateLimiter);
	boolean denyEmpty = getOrDefault(config.denyEmptyKey, this.denyEmptyKey);
	HttpStatusHolder emptyKeyStatus = HttpStatusHolder
			.parse(getOrDefault(config.emptyKeyStatus, this.emptyKeyStatusCode));

	return (exchange, chain) -> resolver.resolve(exchange).defaultIfEmpty(EMPTY_KEY)
			.flatMap(key -> {
				if (EMPTY_KEY.equals(key)) {
					if (denyEmpty) {
						setResponseStatus(exchange, emptyKeyStatus);
						return exchange.getResponse().setComplete();
					}
					return chain.filter(exchange);
				}
				String routeId = config.getRouteId();
				if (routeId == null) {
					Route route = exchange
							.getAttribute(ServerWebExchangeUtils.GATEWAY_ROUTE_ATTR);
					routeId = route.getId();
				}
				return limiter.isAllowed(routeId, key).flatMap(response -> {

					for (Map.Entry<String, String> header : response.getHeaders()
							.entrySet()) {
						exchange.getResponse().getHeaders().add(header.getKey(),
								header.getValue());
					}

					if (response.isAllowed()) {
						return chain.filter(exchange);
					}

					setResponseStatus(exchange, config.getStatusCode());
					return exchange.getResponse().setComplete();
				});
			});
}
 
Example #9
Source File: RequestRateLimiterGatewayFilterFactory.java    From spring-cloud-gateway with Apache License 2.0 4 votes vote down vote up
public KeyResolver getDefaultKeyResolver() {
	return defaultKeyResolver;
}
 
Example #10
Source File: RequestRateLimiterGatewayFilterFactory.java    From spring-cloud-gateway with Apache License 2.0 4 votes vote down vote up
public RequestRateLimiterGatewayFilterFactory(RateLimiter defaultRateLimiter,
		KeyResolver defaultKeyResolver) {
	super(Config.class);
	this.defaultRateLimiter = defaultRateLimiter;
	this.defaultKeyResolver = defaultKeyResolver;
}
 
Example #11
Source File: GatewayAutoConfiguration.java    From spring-cloud-gateway with Apache License 2.0 4 votes vote down vote up
@Bean
@ConditionalOnBean({ RateLimiter.class, KeyResolver.class })
public RequestRateLimiterGatewayFilterFactory requestRateLimiterGatewayFilterFactory(
		RateLimiter rateLimiter, KeyResolver resolver) {
	return new RequestRateLimiterGatewayFilterFactory(rateLimiter, resolver);
}
 
Example #12
Source File: GatewayAutoConfiguration.java    From spring-cloud-gateway with Apache License 2.0 4 votes vote down vote up
@Bean(name = PrincipalNameKeyResolver.BEAN_NAME)
@ConditionalOnBean(RateLimiter.class)
@ConditionalOnMissingBean(KeyResolver.class)
public PrincipalNameKeyResolver principalNameKeyResolver() {
	return new PrincipalNameKeyResolver();
}
 
Example #13
Source File: RequestRateLimiterConfig.java    From SpringCloud with Apache License 2.0 4 votes vote down vote up
/**
 * ip地址限流
 *
 * @return 限流key
 */
@Bean
@Primary
public KeyResolver remoteAddressKeyResolver() {
    return exchange -> Mono.just(exchange.getRequest().getRemoteAddress().getHostName());
}
 
Example #14
Source File: RouteConfiguration.java    From microservice-integration with MIT License 4 votes vote down vote up
@Bean
public KeyResolver userKeyResolver() {
    return exchange -> Mono.just(exchange.getRequest().getQueryParams().getFirst("user"));
}
 
Example #15
Source File: CurrentLimitingConfiguration.java    From momo-cloud-permission with Apache License 2.0 4 votes vote down vote up
@Bean(name = "apiKeyResolver")
@Primary
public KeyResolver apiKeyResolver() {
    return exchange -> Mono.just(exchange.getRequest().getPath().value());
}
 
Example #16
Source File: RateLimiterConfiguration.java    From smaker with GNU Lesser General Public License v3.0 4 votes vote down vote up
@Bean(value = "remoteAddrKeyResolver")
public KeyResolver remoteAddrKeyResolver() {
	return exchange -> Mono.just(exchange.getRequest().getRemoteAddress().getAddress().getHostAddress());
}
 
Example #17
Source File: ApiConfiguration.java    From open-cloud with MIT License 4 votes vote down vote up
@Bean
public KeyResolver pathKeyResolver() {
    return exchange -> Mono.just(exchange.getRequest().getPath().value());
}
 
Example #18
Source File: GatewayConfig.java    From fw-spring-cloud with Apache License 2.0 4 votes vote down vote up
@Bean
KeyResolver userKeyResolver() {
    return exchange -> Mono.just(exchange.getRequest().getQueryParams().getFirst("fwcloud"));
}
 
Example #19
Source File: RateLimiterConfiguration.java    From jeecg-cloud with Apache License 2.0 4 votes vote down vote up
/**
 * IP限流 (通过exchange对象可以获取到请求信息,这边用了HostName)
 */
@Bean
@Primary
public KeyResolver ipKeyResolver() {
	return exchange -> Mono.just(exchange.getRequest().getRemoteAddress().getAddress().getHostAddress());
}
 
Example #20
Source File: RateLimiterConfiguration.java    From jeecg-cloud with Apache License 2.0 4 votes vote down vote up
/**
 * 用户限流 (通过exchange对象可以获取到请求信息,获取当前请求的用户 TOKEN)
 */
@Bean
public KeyResolver userKeyResolver() {
	//使用这种方式限流,请求Header中必须携带X-Access-Token参数
	return exchange -> Mono.just(exchange.getRequest().getHeaders().getFirst(GlobalAccessTokenFilter.X_ACCESS_TOKEN));
}
 
Example #21
Source File: RateLimiterConfiguration.java    From jeecg-cloud with Apache License 2.0 4 votes vote down vote up
/**
* 接口限流 (获取请求地址的uri作为限流key)
*/
@Bean
public KeyResolver apiKeyResolver() {
	return exchange -> Mono.just(exchange.getRequest().getPath().value());
}
 
Example #22
Source File: CurrentLimitingConfiguration.java    From momo-cloud-permission with Apache License 2.0 4 votes vote down vote up
@Bean(name = "userKeyResolver")
public KeyResolver userKeyResolver() {
    return exchange -> Mono.just(exchange.getRequest().getQueryParams().getFirst("userId"));
}
 
Example #23
Source File: CurrentLimitingConfiguration.java    From momo-cloud-permission with Apache License 2.0 4 votes vote down vote up
@Bean(name = "ipKeyResolver")
public KeyResolver ipKeyResolver() {
    return exchange -> Mono.just(exchange.getRequest().getRemoteAddress().getHostName());
}
 
Example #24
Source File: RedisRateLimiterConfig.java    From light-reading-cloud with MIT License 4 votes vote down vote up
/**
 * 按客户端IP限流
 * @return
 */
@Bean
public KeyResolver ipKeyResolver() {
    return exchange -> Mono.just(exchange.getRequest().getRemoteAddress().getHostName());
}
 
Example #25
Source File: RequestRateLimiterConfig.java    From JetfireCloud with Apache License 2.0 2 votes vote down vote up
/**
 * 请求路径限流
 *
 * @return 限流key
 */
@Bean
public KeyResolver apiKeyResolver() {
    return exchange -> Mono.just(exchange.getRequest().getPath().value());
}
 
Example #26
Source File: DefaultLimitStrategyConfiguration.java    From spring-cloud-sofastack-samples with Apache License 2.0 2 votes vote down vote up
/**
 * 接口限流
 *
 * @return
 */
@Bean
public KeyResolver apiKeyResolver() {
    return exchange -> Mono.just(exchange.getRequest().getPath().value());
}
 
Example #27
Source File: DefaultLimitStrategyConfiguration.java    From spring-cloud-sofastack-samples with Apache License 2.0 2 votes vote down vote up
/**
 * IP限流
 *
 * @return
 */
@Bean
public KeyResolver ipKeyResolver() {
    return exchange -> Mono.just(exchange.getRequest().getRemoteAddress().getHostName());
}
 
Example #28
Source File: RequestRateLimiterConfig.java    From SpringCloud with Apache License 2.0 2 votes vote down vote up
/**
 * username限流
 *
 * @return 限流key
 */
@Bean
public KeyResolver userKeyResolver() {
    return exchange -> Mono.just(exchange.getRequest().getQueryParams().getFirst("username"));
}
 
Example #29
Source File: RequestRateLimiterConfig.java    From SpringCloud with Apache License 2.0 2 votes vote down vote up
/**
 * 请求路径限流
 *
 * @return 限流key
 */
@Bean
public KeyResolver apiKeyResolver() {
    return exchange -> Mono.just(exchange.getRequest().getPath().value());
}
 
Example #30
Source File: RequestRateLimiterConfig.java    From JetfireCloud with Apache License 2.0 2 votes vote down vote up
/**
 * ip地址限流
 *
 * @return 限流key
 */
@Bean
public KeyResolver remoteAddressKeyResolver() {
    return exchange -> Mono.just(exchange.getRequest().getRemoteAddress().getHostName());
}