From ea8f2a7276be4977f74dfeb5e49cfeb29ae2b19b Mon Sep 17 00:00:00 2001 From: Madhura Bhave Date: Wed, 26 Feb 2020 17:05:16 -0800 Subject: [PATCH] Fix tests following changes to EndpointRequest See gh-20329 --- .../security/reactive/EndpointRequest.java | 59 ++++++++++--------- .../security/servlet/EndpointRequest.java | 8 +-- ...cationContextServerWebExchangeMatcher.java | 15 +++++ 3 files changed, 51 insertions(+), 31 deletions(-) diff --git a/spring-boot-project/spring-boot-actuator-autoconfigure/src/main/java/org/springframework/boot/actuate/autoconfigure/security/reactive/EndpointRequest.java b/spring-boot-project/spring-boot-actuator-autoconfigure/src/main/java/org/springframework/boot/actuate/autoconfigure/security/reactive/EndpointRequest.java index de585dbb02c..fe4d50759b2 100644 --- a/spring-boot-project/spring-boot-actuator-autoconfigure/src/main/java/org/springframework/boot/actuate/autoconfigure/security/reactive/EndpointRequest.java +++ b/spring-boot-project/spring-boot-actuator-autoconfigure/src/main/java/org/springframework/boot/actuate/autoconfigure/security/reactive/EndpointRequest.java @@ -115,12 +115,41 @@ public final class EndpointRequest { return new LinksServerWebExchangeMatcher(); } + /** + * Base class for supported request matchers. + */ + private abstract static class AbstractWebExchangeMatcher extends ApplicationContextServerWebExchangeMatcher { + + private ManagementPortType managementPortType; + + AbstractWebExchangeMatcher(Class contextClass) { + super(contextClass); + } + + @Override + protected boolean ignoreApplicationContext(ApplicationContext applicationContext) { + if (this.managementPortType == null) { + this.managementPortType = ManagementPortType.get(applicationContext.getEnvironment()); + } + if (this.managementPortType == ManagementPortType.DIFFERENT) { + if (applicationContext.getParent() == null) { + return true; + } + String managementContextId = applicationContext.getParent().getId() + ":management"; + if (!managementContextId.equals(applicationContext.getId())) { + return true; + } + } + return false; + } + + } + /** * The {@link ServerWebExchangeMatcher} used to match against {@link Endpoint actuator * endpoints}. */ - public static final class EndpointServerWebExchangeMatcher - extends ApplicationContextServerWebExchangeMatcher { + public static final class EndpointServerWebExchangeMatcher extends AbstractWebExchangeMatcher { private final List includes; @@ -225,36 +254,15 @@ public final class EndpointRequest { @Override protected Mono matches(ServerWebExchange exchange, Supplier context) { - if (!isManagementContext(exchange)) { - return MatchResult.notMatch(); - } return this.delegate.matches(exchange); } - static boolean isManagementContext(ServerWebExchange exchange) { - ApplicationContext applicationContext = exchange.getApplicationContext(); - if (managementPortType == null) { - managementPortType = ManagementPortType.get(applicationContext.getEnvironment()); - } - if (managementPortType == ManagementPortType.DIFFERENT) { - if (applicationContext.getParent() == null) { - return false; - } - String managementContextId = applicationContext.getParent().getId() + ":management"; - if (!managementContextId.equals(applicationContext.getId())) { - return false; - } - } - return true; - } - } /** * The {@link ServerWebExchangeMatcher} used to match against the links endpoint. */ - public static final class LinksServerWebExchangeMatcher - extends ApplicationContextServerWebExchangeMatcher { + public static final class LinksServerWebExchangeMatcher extends AbstractWebExchangeMatcher { private volatile ServerWebExchangeMatcher delegate; @@ -276,9 +284,6 @@ public final class EndpointRequest { @Override protected Mono matches(ServerWebExchange exchange, Supplier context) { - if (!EndpointServerWebExchangeMatcher.isManagementContext(exchange)) { - return MatchResult.notMatch(); - } return this.delegate.matches(exchange); } diff --git a/spring-boot-project/spring-boot-actuator-autoconfigure/src/main/java/org/springframework/boot/actuate/autoconfigure/security/servlet/EndpointRequest.java b/spring-boot-project/spring-boot-actuator-autoconfigure/src/main/java/org/springframework/boot/actuate/autoconfigure/security/servlet/EndpointRequest.java index d169021212c..ac102a4f550 100644 --- a/spring-boot-project/spring-boot-actuator-autoconfigure/src/main/java/org/springframework/boot/actuate/autoconfigure/security/servlet/EndpointRequest.java +++ b/spring-boot-project/spring-boot-actuator-autoconfigure/src/main/java/org/springframework/boot/actuate/autoconfigure/security/servlet/EndpointRequest.java @@ -124,7 +124,7 @@ public final class EndpointRequest { private volatile RequestMatcher delegate; - private static ManagementPortType managementPortType; + private ManagementPortType managementPortType; AbstractRequestMatcher() { super(WebApplicationContext.class); @@ -132,10 +132,10 @@ public final class EndpointRequest { @Override protected boolean ignoreApplicationContext(WebApplicationContext applicationContext) { - if (managementPortType == null) { - managementPortType = ManagementPortType.get(applicationContext.getEnvironment()); + if (this.managementPortType == null) { + this.managementPortType = ManagementPortType.get(applicationContext.getEnvironment()); } - return managementPortType == ManagementPortType.DIFFERENT + return this.managementPortType == ManagementPortType.DIFFERENT && !WebServerApplicationContext.hasServerNamespace(applicationContext, "management"); } diff --git a/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/security/reactive/ApplicationContextServerWebExchangeMatcher.java b/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/security/reactive/ApplicationContextServerWebExchangeMatcher.java index 599ac4ae103..a4a2cb72eb0 100644 --- a/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/security/reactive/ApplicationContextServerWebExchangeMatcher.java +++ b/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/security/reactive/ApplicationContextServerWebExchangeMatcher.java @@ -53,6 +53,9 @@ public abstract class ApplicationContextServerWebExchangeMatcher implements S @Override public final Mono matches(ServerWebExchange exchange) { + if (ignoreApplicationContext(exchange.getApplicationContext())) { + return MatchResult.notMatch(); + } return matches(exchange, getContext(exchange)); } @@ -64,6 +67,18 @@ public abstract class ApplicationContextServerWebExchangeMatcher implements S */ protected abstract Mono matches(ServerWebExchange exchange, Supplier context); + /** + * Returns if the {@link ApplicationContext} should be ignored and not used for + * matching. If this method returns {@code true} then the context will not be used and + * the {@link #matches(ServerWebExchange) matches} method will return {@code false}. + * @param applicationContext the candidate application context + * @return if the application context should be ignored + * @since 2.2.5 + */ + protected boolean ignoreApplicationContext(ApplicationContext applicationContext) { + return false; + } + protected Supplier getContext(ServerWebExchange exchange) { if (this.context == null) { synchronized (this.contextLock) {