https://docs.spring.io/spring-boot/docs/3.2.0/reference/htmlsingle/#actuator.cloud-foundry
当你部署到兼容的 Cloud Foundry 实例时,Spring Boot 的 actuator 模块会包含额外的支持功能。/cloudfoundryapplication 路
径为所有带有 @Endpoint
注解的 beans 提供了一个替代的安全路由。
这种扩展支持允许 Cloud Foundry 管理 UI(例如,你可以用来查看已部署应用程序的 web 应用程序)通过 Spring Boot actuator 信息来增强功能。例如,应用程序状态页面可以包含完整的健康状况信息,而不是典型的“运行中”或“已停止”状态。
注意:对于常规用户来说,无法直接访问 /cloudfoundryapplication
路径。要使用该端点,必须在请求中传递有效的 UAA token。
UAA(User Account and Authentication)是一个用于管理账户、OAuth2客户端和用户鉴权问题的Web服务。在用户登录时,UAA会颁发一个token给客户端,这个token通常是一个JSON Web Token(JWT)。
禁用扩展的 Cloud Foundry Actuator 支持
如果你想要完全禁用 /cloudfoundryapplication
端点,可以在你的 application.properties
文件中添加以下设置:
management.cloudfoundry.enabled=false
Cloud Foundry 自签名证书
默认情况下,对于 /cloudfoundryapplication
端点的安全验证会进行 SSL 调用,以连接到各种 Cloud Foundry 服务。如果你的 Cloud Foundry UAA 或 Cloud Controller 服务使用自签名证书,你需要设置以下属性:
management.cloudfoundry.skip-ssl-validation=true
自定义上下文路径
如果服务器的上下文路径(context-path)被配置为除“/
”之外的其它任何内容,则 Cloud Foundry 端点将不会在应用程序的根目录下可用。例如,如果 server.servlet.context-path=/app
,则 Cloud Foundry 端点将在 /app/cloudfoundryapplication/*
下可用。
如果你希望 Cloud Foundry 端点始终在 /cloudfoundryapplication/*
下可用,而不管服务器的上下文路径是什么,你需要在你的应用程序中明确配置这一点。配置的具体方式取决于所使用的 Web 服务器。对于 Tomcat,你可以添加以下配置:
import java.io.IOException;
import java.util.Collections;
import jakarta.servlet.GenericServlet;
import jakarta.servlet.Servlet;
import jakarta.servlet.ServletContainerInitializer;
import jakarta.servlet.ServletContext;
import jakarta.servlet.ServletException;
import jakarta.servlet.ServletRequest;
import jakarta.servlet.ServletResponse;
import org.apache.catalina.Host;
import org.apache.catalina.core.StandardContext;
import org.apache.catalina.startup.Tomcat;
import org.springframework.boot.web.embedded.tomcat.TomcatServletWebServerFactory;
import org.springframework.boot.web.servlet.ServletContextInitializer;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration(proxyBeanMethods = false)
public class MyCloudFoundryConfiguration {
@Bean
public TomcatServletWebServerFactory servletWebServerFactory() {
return new TomcatServletWebServerFactory() {
@Override
protected void prepareContext(Host host, ServletContextInitializer[] initializers) {
super.prepareContext(host, initializers);
StandardContext child = new StandardContext();
child.addLifecycleListener(new Tomcat.FixContextListener());
child.setPath("/cloudfoundryapplication");
ServletContainerInitializer initializer = getServletContextInitializer(getContextPath());
child.addServletContainerInitializer(initializer, Collections.emptySet());
child.setCrossContext(true);
host.addChild(child);
}
};
}
private ServletContainerInitializer getServletContextInitializer(String contextPath) {
return (classes, context) -> {
Servlet servlet = new GenericServlet() {
@Override
public void service(ServletRequest req, ServletResponse res) throws ServletException, IOException {
ServletContext context = req.getServletContext().getContext(contextPath);
context.getRequestDispatcher("/cloudfoundryapplication").forward(req, res);
}
};
context.addServlet("cloudfoundry", servlet).addMapping("/*");
};
}
}
如果你使用的是基于 Webflux 的应用程序,你可以使用以下配置:
import java.util.Map;
import reactor.core.publisher.Mono;
import org.springframework.boot.autoconfigure.web.reactive.WebFluxProperties;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.server.reactive.ContextPathCompositeHandler;
import org.springframework.http.server.reactive.HttpHandler;
import org.springframework.http.server.reactive.ServerHttpRequest;
import org.springframework.http.server.reactive.ServerHttpResponse;
import org.springframework.web.server.adapter.WebHttpHandlerBuilder;
@Configuration(proxyBeanMethods = false)
@EnableConfigurationProperties(WebFluxProperties.class)
public class MyReactiveCloudFoundryConfiguration {
@Bean
public HttpHandler httpHandler(ApplicationContext applicationContext, WebFluxProperties properties) {
HttpHandler httpHandler = WebHttpHandlerBuilder.applicationContext(applicationContext).build();
return new CloudFoundryHttpHandler(properties.getBasePath(), httpHandler);
}
private static final class CloudFoundryHttpHandler implements HttpHandler {
private final HttpHandler delegate;
private final ContextPathCompositeHandler contextPathDelegate;
private CloudFoundryHttpHandler(String basePath, HttpHandler delegate) {
this.delegate = delegate;
this.contextPathDelegate = new ContextPathCompositeHandler(Map.of(basePath, delegate));
}
@Override
public Mono<Void> handle(ServerHttpRequest request, ServerHttpResponse response) {
// Remove underlying context path first (e.g. Servlet container)
String path = request.getPath().pathWithinApplication().value();
if (path.startsWith("/cloudfoundryapplication")) {
return this.delegate.handle(request, response);
}
else {
return this.contextPathDelegate.handle(request, response);
}
}
}
}