|
1 | 1 | package org.gridsuite.gateway;
|
2 | 2 |
|
| 3 | +import lombok.extern.slf4j.Slf4j; |
3 | 4 | import org.assertj.core.api.InstanceOfAssertFactories;
|
4 | 5 | import org.assertj.core.api.WithAssertions;
|
5 | 6 | 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; |
6 | 10 | import org.junit.jupiter.api.Test;
|
7 | 11 | import org.springframework.beans.factory.annotation.Autowired;
|
8 | 12 | import org.springframework.boot.test.context.SpringBootTest;
|
| 13 | +import org.springframework.cloud.gateway.filter.GlobalFilter; |
| 14 | +import org.springframework.cloud.gateway.filter.WebsocketRoutingFilter; |
9 | 15 | import org.springframework.cloud.gateway.route.RouteLocator;
|
10 | 16 | import org.springframework.context.ApplicationContext;
|
| 17 | +import org.springframework.core.annotation.AnnotationAwareOrderComparator; |
11 | 18 | import reactor.test.StepVerifier;
|
12 | 19 |
|
13 | 20 | import java.util.Map;
|
14 | 21 |
|
| 22 | +@Slf4j |
15 | 23 | @SpringBootTest
|
16 | 24 | class GatewayApplicationTest implements WithAssertions {
|
17 | 25 | @Autowired
|
@@ -66,4 +74,33 @@ void testRoutesInitialized() {
|
66 | 74 | .expectNextCount(28)
|
67 | 75 | .verifyComplete();
|
68 | 76 | }
|
| 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 | + } |
69 | 106 | }
|
0 commit comments