-
Notifications
You must be signed in to change notification settings - Fork 3.4k
QuotaFilter for Spring-Cloud-Gateway #1433
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
HappyTobi
wants to merge
9
commits into
spring-cloud:main
Choose a base branch
from
HappyTobi:quota_filter
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 3 commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
d23e431
New quota filter for spring cloud gateway
HappyTobi e48c52f
Move rate limiter to redis package
HappyTobi a24a81c
New Documentation how to use the quota filter
HappyTobi b2c0046
Move filters back to filters package because of backward compatibility
HappyTobi 0fe3ae7
Merge branch 'master' into quota_filter
HappyTobi 2b8aef0
Add quotafilter documentation
HappyTobi 0970bda
Merge branch 'master' of https://github.com/spring-cloud/spring-cloud…
HappyTobi 6fa79ec
UPDATE change check and return value for remain token values.
HappyTobi 10a1d91
UPDATE change import for NotNull annotation
HappyTobi File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
207 changes: 207 additions & 0 deletions
207
...va/org/springframework/cloud/gateway/filter/factory/RequestQuotaGatewayFilterFactory.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,207 @@ | ||
/* | ||
* Copyright 2013-2019 the original author or authors. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package org.springframework.cloud.gateway.filter.factory; | ||
|
||
import java.util.Map; | ||
|
||
import org.springframework.boot.context.properties.ConfigurationProperties; | ||
import org.springframework.cloud.gateway.filter.GatewayFilter; | ||
import org.springframework.cloud.gateway.filter.redis.KeyResolver; | ||
import org.springframework.cloud.gateway.filter.redis.quota.QuotaFilter; | ||
import org.springframework.cloud.gateway.route.Route; | ||
import org.springframework.cloud.gateway.support.HasRouteId; | ||
import org.springframework.cloud.gateway.support.HttpStatusHolder; | ||
import org.springframework.cloud.gateway.support.ServerWebExchangeUtils; | ||
import org.springframework.http.HttpStatus; | ||
|
||
import static org.springframework.cloud.gateway.support.ServerWebExchangeUtils.setResponseStatus; | ||
|
||
/** | ||
* @author Tobias Schug | ||
*/ | ||
@ConfigurationProperties("spring.cloud.gateway.filter.request-quota-filter") | ||
public class RequestQuotaGatewayFilterFactory | ||
extends AbstractGatewayFilterFactory<RequestQuotaGatewayFilterFactory.Config> { | ||
|
||
/** | ||
* Key-Resolver key. | ||
*/ | ||
public static final String KEY_RESOLVER_KEY = "keyResolver"; | ||
|
||
private static final String EMPTY_KEY = "____EMPTY_KEY__"; | ||
|
||
private final QuotaFilter defaultQuotaFilter; | ||
|
||
private final KeyResolver defaultKeyResolver; | ||
|
||
/** | ||
* Switch to deny requests if the Key Resolver returns an empty key, defaults to true. | ||
*/ | ||
private boolean denyEmptyKey = true; | ||
|
||
/** HttpStatus to return when denyEmptyKey is true, defaults to FORBIDDEN. */ | ||
private String emptyKeyStatusCode = HttpStatus.FORBIDDEN.name(); | ||
|
||
public RequestQuotaGatewayFilterFactory(QuotaFilter defaultQuotaFilter, | ||
KeyResolver defaultKeyResolver) { | ||
super(Config.class); | ||
this.defaultQuotaFilter = defaultQuotaFilter; | ||
this.defaultKeyResolver = defaultKeyResolver; | ||
} | ||
|
||
public KeyResolver getDefaultKeyResolver() { | ||
return defaultKeyResolver; | ||
} | ||
|
||
public QuotaFilter getDefaultQuotaFilter() { | ||
return defaultQuotaFilter; | ||
} | ||
|
||
public boolean isDenyEmptyKey() { | ||
return denyEmptyKey; | ||
} | ||
|
||
public void setDenyEmptyKey(boolean denyEmptyKey) { | ||
this.denyEmptyKey = denyEmptyKey; | ||
} | ||
|
||
public String getEmptyKeyStatusCode() { | ||
return emptyKeyStatusCode; | ||
} | ||
|
||
public void setEmptyKeyStatusCode(String emptyKeyStatusCode) { | ||
this.emptyKeyStatusCode = emptyKeyStatusCode; | ||
} | ||
|
||
@SuppressWarnings("unchecked") | ||
@Override | ||
public GatewayFilter apply(Config config) { | ||
KeyResolver resolver = getOrDefault(config.keyResolver, defaultKeyResolver); | ||
QuotaFilter<Object> limiter = getOrDefault(config.quotaFilter, | ||
defaultQuotaFilter); | ||
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(); | ||
}); | ||
}); | ||
} | ||
|
||
private <T> T getOrDefault(T configValue, T defaultValue) { | ||
return (configValue != null) ? configValue : defaultValue; | ||
} | ||
|
||
public static class Config implements HasRouteId { | ||
|
||
private KeyResolver keyResolver; | ||
|
||
private QuotaFilter quotaFilter; | ||
|
||
private HttpStatus statusCode = HttpStatus.TOO_MANY_REQUESTS; | ||
|
||
private Boolean denyEmptyKey; | ||
|
||
private String emptyKeyStatus; | ||
|
||
private String routeId; | ||
|
||
public KeyResolver getKeyResolver() { | ||
return keyResolver; | ||
} | ||
|
||
public Config setKeyResolver(KeyResolver keyResolver) { | ||
this.keyResolver = keyResolver; | ||
return this; | ||
} | ||
|
||
public QuotaFilter getQuotaFilter() { | ||
return quotaFilter; | ||
} | ||
|
||
public Config setQuotaFilter(QuotaFilter quotaFilter) { | ||
this.quotaFilter = quotaFilter; | ||
return this; | ||
} | ||
|
||
public HttpStatus getStatusCode() { | ||
return statusCode; | ||
} | ||
|
||
public Config setStatusCode(HttpStatus statusCode) { | ||
this.statusCode = statusCode; | ||
return this; | ||
} | ||
|
||
public Boolean getDenyEmptyKey() { | ||
return denyEmptyKey; | ||
} | ||
|
||
public Config setDenyEmptyKey(Boolean denyEmptyKey) { | ||
this.denyEmptyKey = denyEmptyKey; | ||
return this; | ||
} | ||
|
||
public String getEmptyKeyStatus() { | ||
return emptyKeyStatus; | ||
} | ||
|
||
public Config setEmptyKeyStatus(String emptyKeyStatus) { | ||
this.emptyKeyStatus = emptyKeyStatus; | ||
return this; | ||
} | ||
|
||
@Override | ||
public void setRouteId(String routeId) { | ||
this.routeId = routeId; | ||
} | ||
|
||
@Override | ||
public String getRouteId() { | ||
return this.routeId; | ||
} | ||
|
||
} | ||
|
||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.