Align reactive EndpointRequest with servlet equivalent

Closes gh-44189
This commit is contained in:
Andy Wilkinson
2025-02-10 09:28:28 +00:00
parent 3603cb4ad9
commit 1c0253b380
2 changed files with 36 additions and 9 deletions

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2012-2024 the original author or authors.
* Copyright 2012-2025 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -37,6 +37,7 @@ import org.springframework.boot.actuate.endpoint.annotation.Endpoint;
import org.springframework.boot.actuate.endpoint.web.PathMappedEndpoints;
import org.springframework.boot.actuate.endpoint.web.WebServerNamespace;
import org.springframework.boot.security.reactive.ApplicationContextServerWebExchangeMatcher;
import org.springframework.boot.web.context.WebServerApplicationContext;
import org.springframework.context.ApplicationContext;
import org.springframework.core.annotation.MergedAnnotation;
import org.springframework.core.annotation.MergedAnnotations;
@@ -211,11 +212,15 @@ public final class EndpointRequest {
protected final boolean hasWebServerNamespace(ApplicationContext applicationContext,
WebServerNamespace webServerNamespace) {
if (applicationContext.getParent() == null) {
return WebServerNamespace.SERVER.equals(webServerNamespace);
}
String parentContextId = applicationContext.getParent().getId();
return applicationContext.getId().equals(parentContextId + ":" + webServerNamespace);
return WebServerApplicationContext.hasServerNamespace(applicationContext, webServerNamespace.getValue())
|| hasImplicitServerNamespace(applicationContext, webServerNamespace);
}
private boolean hasImplicitServerNamespace(ApplicationContext applicationContext,
WebServerNamespace webServerNamespace) {
return WebServerNamespace.SERVER.equals(webServerNamespace)
&& WebServerApplicationContext.getServerNamespace(applicationContext) == null
&& applicationContext.getParent() == null;
}
protected final String toString(List<Object> endpoints, String emptyValue) {

View File

@@ -32,12 +32,15 @@ import org.springframework.boot.actuate.endpoint.annotation.Endpoint;
import org.springframework.boot.actuate.endpoint.web.PathMappedEndpoint;
import org.springframework.boot.actuate.endpoint.web.PathMappedEndpoints;
import org.springframework.boot.actuate.endpoint.web.WebServerNamespace;
import org.springframework.boot.web.context.WebServerApplicationContext;
import org.springframework.boot.web.server.WebServer;
import org.springframework.context.support.StaticApplicationContext;
import org.springframework.http.server.reactive.ServerHttpRequest;
import org.springframework.http.server.reactive.ServerHttpResponse;
import org.springframework.mock.http.server.reactive.MockServerHttpRequest;
import org.springframework.mock.http.server.reactive.MockServerHttpResponse;
import org.springframework.security.web.server.util.matcher.ServerWebExchangeMatcher;
import org.springframework.web.context.support.StaticWebApplicationContext;
import org.springframework.web.server.ServerWebExchange;
import org.springframework.web.server.WebHandler;
import org.springframework.web.server.adapter.HttpWebHandlerAdapter;
@@ -315,10 +318,8 @@ class EndpointRequestTests {
PathMappedEndpoints pathMappedEndpoints, WebServerNamespace namespace) {
StaticApplicationContext context = new StaticApplicationContext();
if (namespace != null && !WebServerNamespace.SERVER.equals(namespace)) {
StaticApplicationContext parentContext = new StaticApplicationContext();
parentContext.setId("app");
NamedStaticWebApplicationContext parentContext = new NamedStaticWebApplicationContext(namespace);
context.setParent(parentContext);
context.setId(parentContext.getId() + ":" + namespace);
}
context.registerBean(WebEndpointProperties.class);
if (pathMappedEndpoints != null) {
@@ -351,6 +352,27 @@ class EndpointRequestTests {
return endpoint;
}
static class NamedStaticWebApplicationContext extends StaticWebApplicationContext
implements WebServerApplicationContext {
private final WebServerNamespace webServerNamespace;
NamedStaticWebApplicationContext(WebServerNamespace webServerNamespace) {
this.webServerNamespace = webServerNamespace;
}
@Override
public WebServer getWebServer() {
return null;
}
@Override
public String getServerNamespace() {
return (this.webServerNamespace != null) ? this.webServerNamespace.getValue() : null;
}
}
static class RequestMatcherAssert implements AssertDelegateTarget {
private final StaticApplicationContext context;