Skip to content

Commit ea8f2a7

Browse files
committed
Fix tests following changes to EndpointRequest
See gh-20329
1 parent cdae79d commit ea8f2a7

File tree

3 files changed

+51
-31
lines changed

3 files changed

+51
-31
lines changed

spring-boot-project/spring-boot-actuator-autoconfigure/src/main/java/org/springframework/boot/actuate/autoconfigure/security/reactive/EndpointRequest.java

Lines changed: 32 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -115,12 +115,41 @@ public static LinksServerWebExchangeMatcher toLinks() {
115115
return new LinksServerWebExchangeMatcher();
116116
}
117117

118+
/**
119+
* Base class for supported request matchers.
120+
*/
121+
private abstract static class AbstractWebExchangeMatcher<T> extends ApplicationContextServerWebExchangeMatcher<T> {
122+
123+
private ManagementPortType managementPortType;
124+
125+
AbstractWebExchangeMatcher(Class<? extends T> contextClass) {
126+
super(contextClass);
127+
}
128+
129+
@Override
130+
protected boolean ignoreApplicationContext(ApplicationContext applicationContext) {
131+
if (this.managementPortType == null) {
132+
this.managementPortType = ManagementPortType.get(applicationContext.getEnvironment());
133+
}
134+
if (this.managementPortType == ManagementPortType.DIFFERENT) {
135+
if (applicationContext.getParent() == null) {
136+
return true;
137+
}
138+
String managementContextId = applicationContext.getParent().getId() + ":management";
139+
if (!managementContextId.equals(applicationContext.getId())) {
140+
return true;
141+
}
142+
}
143+
return false;
144+
}
145+
146+
}
147+
118148
/**
119149
* The {@link ServerWebExchangeMatcher} used to match against {@link Endpoint actuator
120150
* endpoints}.
121151
*/
122-
public static final class EndpointServerWebExchangeMatcher
123-
extends ApplicationContextServerWebExchangeMatcher<PathMappedEndpoints> {
152+
public static final class EndpointServerWebExchangeMatcher extends AbstractWebExchangeMatcher<PathMappedEndpoints> {
124153

125154
private final List<Object> includes;
126155

@@ -225,36 +254,15 @@ private List<ServerWebExchangeMatcher> getDelegateMatchers(Set<String> paths) {
225254

226255
@Override
227256
protected Mono<MatchResult> matches(ServerWebExchange exchange, Supplier<PathMappedEndpoints> context) {
228-
if (!isManagementContext(exchange)) {
229-
return MatchResult.notMatch();
230-
}
231257
return this.delegate.matches(exchange);
232258
}
233259

234-
static boolean isManagementContext(ServerWebExchange exchange) {
235-
ApplicationContext applicationContext = exchange.getApplicationContext();
236-
if (managementPortType == null) {
237-
managementPortType = ManagementPortType.get(applicationContext.getEnvironment());
238-
}
239-
if (managementPortType == ManagementPortType.DIFFERENT) {
240-
if (applicationContext.getParent() == null) {
241-
return false;
242-
}
243-
String managementContextId = applicationContext.getParent().getId() + ":management";
244-
if (!managementContextId.equals(applicationContext.getId())) {
245-
return false;
246-
}
247-
}
248-
return true;
249-
}
250-
251260
}
252261

253262
/**
254263
* The {@link ServerWebExchangeMatcher} used to match against the links endpoint.
255264
*/
256-
public static final class LinksServerWebExchangeMatcher
257-
extends ApplicationContextServerWebExchangeMatcher<WebEndpointProperties> {
265+
public static final class LinksServerWebExchangeMatcher extends AbstractWebExchangeMatcher<WebEndpointProperties> {
258266

259267
private volatile ServerWebExchangeMatcher delegate;
260268

@@ -276,9 +284,6 @@ private ServerWebExchangeMatcher createDelegate(WebEndpointProperties properties
276284

277285
@Override
278286
protected Mono<MatchResult> matches(ServerWebExchange exchange, Supplier<WebEndpointProperties> context) {
279-
if (!EndpointServerWebExchangeMatcher.isManagementContext(exchange)) {
280-
return MatchResult.notMatch();
281-
}
282287
return this.delegate.matches(exchange);
283288
}
284289

spring-boot-project/spring-boot-actuator-autoconfigure/src/main/java/org/springframework/boot/actuate/autoconfigure/security/servlet/EndpointRequest.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -124,18 +124,18 @@ private abstract static class AbstractRequestMatcher
124124

125125
private volatile RequestMatcher delegate;
126126

127-
private static ManagementPortType managementPortType;
127+
private ManagementPortType managementPortType;
128128

129129
AbstractRequestMatcher() {
130130
super(WebApplicationContext.class);
131131
}
132132

133133
@Override
134134
protected boolean ignoreApplicationContext(WebApplicationContext applicationContext) {
135-
if (managementPortType == null) {
136-
managementPortType = ManagementPortType.get(applicationContext.getEnvironment());
135+
if (this.managementPortType == null) {
136+
this.managementPortType = ManagementPortType.get(applicationContext.getEnvironment());
137137
}
138-
return managementPortType == ManagementPortType.DIFFERENT
138+
return this.managementPortType == ManagementPortType.DIFFERENT
139139
&& !WebServerApplicationContext.hasServerNamespace(applicationContext, "management");
140140
}
141141

spring-boot-project/spring-boot/src/main/java/org/springframework/boot/security/reactive/ApplicationContextServerWebExchangeMatcher.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,9 @@ public ApplicationContextServerWebExchangeMatcher(Class<? extends C> contextClas
5353

5454
@Override
5555
public final Mono<MatchResult> matches(ServerWebExchange exchange) {
56+
if (ignoreApplicationContext(exchange.getApplicationContext())) {
57+
return MatchResult.notMatch();
58+
}
5659
return matches(exchange, getContext(exchange));
5760
}
5861

@@ -64,6 +67,18 @@ public final Mono<MatchResult> matches(ServerWebExchange exchange) {
6467
*/
6568
protected abstract Mono<MatchResult> matches(ServerWebExchange exchange, Supplier<C> context);
6669

70+
/**
71+
* Returns if the {@link ApplicationContext} should be ignored and not used for
72+
* matching. If this method returns {@code true} then the context will not be used and
73+
* the {@link #matches(ServerWebExchange) matches} method will return {@code false}.
74+
* @param applicationContext the candidate application context
75+
* @return if the application context should be ignored
76+
* @since 2.2.5
77+
*/
78+
protected boolean ignoreApplicationContext(ApplicationContext applicationContext) {
79+
return false;
80+
}
81+
6782
protected Supplier<C> getContext(ServerWebExchange exchange) {
6883
if (this.context == null) {
6984
synchronized (this.contextLock) {

0 commit comments

Comments
 (0)