Skip to content

Commit 0a19edb

Browse files
control filters order
1 parent 47c8a7c commit 0a19edb

File tree

2 files changed

+39
-2
lines changed

2 files changed

+39
-2
lines changed

src/main/java/org/gridsuite/gateway/filters/AbstractGlobalPreFilter.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,12 @@
1616
/**
1717
* @author Slimane Amar <slimane.amar at rte-france.com>
1818
*/
19-
public abstract class AbstractGlobalPreFilter implements GlobalFilter, Ordered {
19+
abstract class AbstractGlobalPreFilter implements GlobalFilter, Ordered {
2020

2121
protected Mono<Void> completeWithCode(ServerWebExchange exchange, HttpStatus code) {
2222
exchange.getResponse().setStatusCode(code);
2323
if ("websocket".equalsIgnoreCase(exchange.getRequest().getHeaders().getUpgrade())) {
24-
// Force the connection to close for websockets handshakes to workaround apache
24+
// Force the connection to close for websockets handshakes to work around apache
2525
// httpd reusing the connection for all subsequent requests in this connection.
2626
exchange.getResponse().getHeaders().set(HttpHeaders.CONNECTION, "close");
2727
}

src/test/java/org/gridsuite/gateway/GatewayApplicationTest.java

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,25 @@
11
package org.gridsuite.gateway;
22

3+
import lombok.extern.slf4j.Slf4j;
34
import org.assertj.core.api.InstanceOfAssertFactories;
45
import org.assertj.core.api.WithAssertions;
56
import org.gridsuite.gateway.endpoints.*;
7+
import org.gridsuite.gateway.filters.ElementAccessControllerGlobalPreFilter;
8+
import org.gridsuite.gateway.filters.TokenValidatorGlobalPreFilter;
9+
import org.gridsuite.gateway.filters.UserAdminControlGlobalPreFilter;
610
import org.junit.jupiter.api.Test;
711
import org.springframework.beans.factory.annotation.Autowired;
812
import org.springframework.boot.test.context.SpringBootTest;
13+
import org.springframework.cloud.gateway.filter.GlobalFilter;
14+
import org.springframework.cloud.gateway.filter.WebsocketRoutingFilter;
915
import org.springframework.cloud.gateway.route.RouteLocator;
1016
import org.springframework.context.ApplicationContext;
17+
import org.springframework.core.annotation.AnnotationAwareOrderComparator;
1118
import reactor.test.StepVerifier;
1219

1320
import java.util.Map;
1421

22+
@Slf4j
1523
@SpringBootTest
1624
class GatewayApplicationTest implements WithAssertions {
1725
@Autowired
@@ -66,4 +74,33 @@ void testRoutesInitialized() {
6674
.expectNextCount(28)
6775
.verifyComplete();
6876
}
77+
78+
@Test
79+
void testFiltersOrder() {
80+
assertThat(applicationContext.getBeansOfType(GlobalFilter.class)
81+
.values()
82+
.stream()
83+
.sorted(AnnotationAwareOrderComparator.INSTANCE) //sort work only on bean instances
84+
.peek(f -> log.info("p={} ; o={} ; {}", AAOC.INSTANCE.getPriority(f), AAOC.INSTANCE.getOrder(f), f.getClass().getName()))
85+
.map(GlobalFilter::getClass)
86+
.toList()).as("global filters found")
87+
// Before ElementAccessControllerGlobalPreFilter to enforce authentication
88+
.containsSubsequence(TokenValidatorGlobalPreFilter.class, ElementAccessControllerGlobalPreFilter.class)
89+
// Before WebsocketRoutingFilter to control access
90+
.containsSubsequence(ElementAccessControllerGlobalPreFilter.class, WebsocketRoutingFilter.class)
91+
.containsSubsequence(
92+
TokenValidatorGlobalPreFilter.class, //Ordered.LOWEST_PRECEDENCE - 4
93+
UserAdminControlGlobalPreFilter.class, //Ordered.LOWEST_PRECEDENCE - 3
94+
ElementAccessControllerGlobalPreFilter.class //Ordered.LOWEST_PRECEDENCE - 2
95+
);
96+
}
97+
98+
private static class AAOC extends AnnotationAwareOrderComparator {
99+
public static final AAOC INSTANCE = new AAOC();
100+
101+
@Override
102+
public int getOrder(final Object obj) {
103+
return super.getOrder(obj);
104+
}
105+
}
69106
}

0 commit comments

Comments
 (0)