Skip to content

Commit

Permalink
[type:feat] cross add originRegex type (apache#3785)
Browse files Browse the repository at this point in the history
  • Loading branch information
yunlongn authored Aug 3, 2022
1 parent ff3ba3b commit 96098bf
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 0 deletions.
1 change: 1 addition & 0 deletions shenyu-bootstrap/src/main/resources/application.yml
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,7 @@ shenyu:
# - c.apache.org
# - d.apache.org
# - http://e.apache.org
# originRegex: ^http(|s)://(.*\.|)abc.com$
allowedExpose: ""
maxAge: "18000"
allowCredentials: true
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1148,6 +1148,8 @@ public static class AllowedOriginConfig {

private Set<String> origins;

private String originRegex;

/**
* Gets the spacer.
*
Expand Down Expand Up @@ -1219,6 +1221,24 @@ public Set<String> getOrigins() {
public void setOrigins(final Set<String> origins) {
this.origins = origins;
}

/**
* Gets the originRegex.
*
* @return the value of originRegex
*/
public String getOriginRegex() {
return originRegex;
}

/**
* Sets the originRegex.
*
* @param originRegex originRegex
*/
public void setOriginRegex(final String originRegex) {
this.originRegex = originRegex;
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
import java.util.Objects;
import java.util.Optional;
import java.util.Set;
import java.util.regex.Pattern;
import java.util.stream.Collectors;
import java.util.stream.Stream;

Expand Down Expand Up @@ -87,6 +88,11 @@ public Mono<Void> filter(@NonNull final ServerWebExchange exchange, @NonNull fin
})
.collect(Collectors.toSet()));
allowCors = allowedOrigin.contains(origin) || allowedOrigin.contains(ALL);
// if the origin is not allow check match origin again
String originRegex;
if (!allowCors && StringUtils.isNotBlank(originRegex = this.filterConfig.getAllowedOrigin().getOriginRegex())) {
allowCors = Pattern.matches(originRegex.trim(), origin);
}
}
if (allowCors) {
// "Access-Control-Allow-Origin"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
package org.apache.shenyu.web.filter;

import org.apache.shenyu.common.config.ShenyuConfig.CrossFilterConfig;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import org.springframework.mock.http.server.reactive.MockServerHttpRequest;
import org.springframework.mock.web.server.MockServerWebExchange;
Expand All @@ -27,6 +28,7 @@
import reactor.test.StepVerifier;

import java.util.HashSet;
import java.util.regex.Pattern;

import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
Expand Down Expand Up @@ -107,4 +109,13 @@ public void testCorsWhitelist() {
.expectSubscription()
.verifyComplete();
}

@Test
public void testOriginRegex() {
final String regex = "^http(|s)://(.*\\.|)abc.com$";
Assertions.assertTrue(Pattern.matches(regex, "http://console.ada.abc.com"));
Assertions.assertTrue(Pattern.matches(regex, "http://console.abc.com"));
Assertions.assertTrue(Pattern.matches(regex, "http://abc.com"));
Assertions.assertFalse(Pattern.matches(regex, "http://aabc.com"));
}
}

0 comments on commit 96098bf

Please sign in to comment.