Add JettyAccessLogCustomizer

Add a `JettyAccessLogCustomizer` to customize management access logs
when the management server is running on a different port.

Closes gh-15041
This commit is contained in:
Phillip Webb 2018-11-15 13:22:55 -08:00
parent c64a007e12
commit e87664f295
2 changed files with 53 additions and 3 deletions

View File

@ -214,6 +214,11 @@
<artifactId>aspectjweaver</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-server</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.elasticsearch</groupId>
<artifactId>elasticsearch</artifactId>

View File

@ -16,10 +16,15 @@
package org.springframework.boot.actuate.autoconfigure.web.servlet;
import java.io.File;
import javax.servlet.Filter;
import org.apache.catalina.Valve;
import org.apache.catalina.valves.AccessLogValve;
import org.eclipse.jetty.server.NCSARequestLog;
import org.eclipse.jetty.server.RequestLog;
import org.eclipse.jetty.server.Server;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.HierarchicalBeanFactory;
@ -39,6 +44,7 @@ import org.springframework.boot.autoconfigure.web.embedded.TomcatWebServerFactor
import org.springframework.boot.autoconfigure.web.embedded.UndertowWebServerFactoryCustomizer;
import org.springframework.boot.autoconfigure.web.servlet.ServletWebServerFactoryCustomizer;
import org.springframework.boot.autoconfigure.web.servlet.TomcatServletWebServerFactoryCustomizer;
import org.springframework.boot.web.embedded.jetty.JettyServletWebServerFactory;
import org.springframework.boot.web.embedded.tomcat.TomcatServletWebServerFactory;
import org.springframework.boot.web.embedded.undertow.UndertowServletWebServerFactory;
import org.springframework.boot.web.server.WebServerFactoryCustomizer;
@ -48,6 +54,7 @@ import org.springframework.context.annotation.Configuration;
import org.springframework.core.Ordered;
import org.springframework.security.config.BeanIds;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.util.StringUtils;
/**
* {@link ManagementContextConfiguration} for Servlet web endpoint infrastructure when a
@ -71,16 +78,22 @@ class ServletManagementChildContextConfiguration {
@Bean
@ConditionalOnClass(name = "io.undertow.Undertow")
public UndertowAccessLogCustomizer undertowAccessLogCustomizer() {
public UndertowAccessLogCustomizer undertowManagementAccessLogCustomizer() {
return new UndertowAccessLogCustomizer();
}
@Bean
@ConditionalOnClass(name = "org.apache.catalina.valves.AccessLogValve")
public TomcatAccessLogCustomizer tomcatAccessLogCustomizer() {
public TomcatAccessLogCustomizer tomcatManagementAccessLogCustomizer() {
return new TomcatAccessLogCustomizer();
}
@Bean
@ConditionalOnClass(name = "org.eclipse.jetty.server.Server")
public JettyAccessLogCustomizer jettyManagementAccessLogCustomizer() {
return new JettyAccessLogCustomizer();
}
@Configuration
@ConditionalOnClass({ EnableWebSecurity.class, Filter.class })
@ConditionalOnBean(name = BeanIds.SPRING_SECURITY_FILTER_CHAIN, search = SearchStrategy.ANCESTORS)
@ -119,8 +132,14 @@ class ServletManagementChildContextConfiguration {
abstract static class AccessLogCustomizer implements Ordered {
private static final String MANAGEMENT_PREFIX = "management_";
protected String customizePrefix(String prefix) {
return "management_" + prefix;
prefix = (prefix != null) ? prefix : "";
if (prefix.startsWith(MANAGEMENT_PREFIX)) {
return prefix;
}
return MANAGEMENT_PREFIX + prefix;
}
@Override
@ -163,4 +182,30 @@ class ServletManagementChildContextConfiguration {
}
static class JettyAccessLogCustomizer extends AccessLogCustomizer
implements WebServerFactoryCustomizer<JettyServletWebServerFactory> {
@Override
public void customize(JettyServletWebServerFactory factory) {
factory.addServerCustomizers(this::customizeServer);
}
private void customizeServer(Server server) {
RequestLog requestLog = server.getRequestLog();
if (requestLog != null && requestLog instanceof NCSARequestLog) {
customizeRequestLog((NCSARequestLog) requestLog);
}
}
private void customizeRequestLog(NCSARequestLog requestLog) {
String filename = requestLog.getFilename();
if (StringUtils.hasLength(filename)) {
File file = new File(filename);
file = new File(file.getParentFile(), customizePrefix(file.getName()));
requestLog.setFilename(file.getPath());
}
}
}
}