Skip to content

Commit

Permalink
fix RemoteAddrKeyResolver.
Browse files Browse the repository at this point in the history
  • Loading branch information
wlngo committed Apr 7, 2024
1 parent d64f213 commit 41aa916
Showing 1 changed file with 8 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,26 +26,29 @@
@Join
public class RemoteAddrKeyResolver implements RateLimiterKeyResolver {

private static final String[] HEADERS = {"X-Forwarded-For", "X-Real-IP", "Proxy-Client-IP", "WL-Proxy-Client-IP", "HTTP_CLIENT_IP", "HTTP_X_FORWARDED_FOR"};

private static final String UNKNOWN = "unknown";

@Override
public String getKeyResolverName() {
return "REMOTE_ADDRESS_KEY_RESOLVER";
}

@Override
public String resolve(final ServerWebExchange exchange) {
String[] headers = {"X-Forwarded-For", "X-Real-IP", "Proxy-Client-IP", "WL-Proxy-Client-IP", "HTTP_CLIENT_IP", "HTTP_X_FORWARDED_FOR"};
String ip;
for (String header : headers) {
for (String header : HEADERS) {
ip = exchange.getRequest().getHeaders().getFirst(header);
boolean isUnknown = StringUtils.isBlank(ip) || "unknown".equalsIgnoreCase(ip);
boolean isUnknown = StringUtils.isBlank(ip) || UNKNOWN.equalsIgnoreCase(ip);
if (!isUnknown) {
if (StringUtils.indexOf(ip, ',') > 0) {
String[] split = StringUtils.split(ip, ',');
for (int i = 0; i < split.length; i++) {
split[i] = split[i].trim();
}
for (final String subIp : split) {
boolean isUnknownSubIp = StringUtils.isBlank(subIp) || "unknown".equalsIgnoreCase(subIp);
for (String subIp : split) {
boolean isUnknownSubIp = StringUtils.isBlank(subIp) || UNKNOWN.equalsIgnoreCase(subIp);
if (!isUnknownSubIp) {
ip = subIp;
break;
Expand Down

0 comments on commit 41aa916

Please sign in to comment.