如何禁用管理上下文访问登录 Spring Boot using undertow

How to disable management context access log in Spring Boot using undertow

我正在使用 Spring 带有嵌入式 Undertow 的 Boot 2.2.4。 我已经使用 server.underdow.accesslog.enabled=true 启用了访问日志,一切都按预期工作。

我在设置子上下文的不同端口上使用执行器端点。我想要记录对执行器的请求。目前他们会自动转到 management_access.log,其中 access. 是我的主要访问日志的前缀。 关于如何禁用该访问日志的任何想法?我知道 Spring 正在通过 Factory 为执行器上下文创建一个单独的 WebServer,但我还没有找到自定义工厂的方法。

我找到了自己的答案(花了太多时间)。 这有点 hack,但它有效:

新配置class:foo.ManagementConfig

package foo;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.actuate.autoconfigure.web.ManagementContextConfiguration;
import org.springframework.boot.web.embedded.undertow.UndertowServletWebServerFactory;
import org.springframework.boot.web.server.WebServerFactoryCustomizer;
import org.springframework.context.annotation.Bean;
@ManagementContextConfiguration
public class ManagementConfig {

    @Bean
    WebServerFactoryCustomizer<UndertowServletWebServerFactory> actuatorCustomizer(@Value("${management.server.port}") int managementPort) {
        return factory -> {
            if (managementPort == factory.getPort()) {
                factory.setAccessLogEnabled(false);
            }
        };
    }
}

已创建 resources/META-INF/spring.factories,以便它被 ManagementContext 拾取:

org.springframework.boot.actuate.autoconfigure.web.ManagementContextConfiguration=foo.ManagementConfig

if 语句中有点 hack 的部分。如果它仅适用于管理环境,那就太好了,但出于某种原因,它正试图同时适用于两者。使用 if 语句,它不会对主要上下文执行任何操作。

如果 management.server.port 未定义或与主要上下文相同,这将产生意想不到的后果。