Rename management.server.accesslog.prefix

Closes gh-44196
This commit is contained in:
Moritz Halbritter
2025-02-17 14:34:04 +01:00
parent 8ff1e631fe
commit dc4dbd1bf2
6 changed files with 182 additions and 26 deletions

View File

@@ -56,6 +56,7 @@ import org.springframework.web.server.adapter.WebHttpHandlerBuilder;
*
* @author Andy Wilkinson
* @author Phillip Webb
* @author Moritz Halbritter
* @since 2.0.0
*/
@EnableWebFlux
@@ -99,18 +100,23 @@ public class ReactiveManagementChildContextConfiguration {
abstract static class AccessLogCustomizer implements Ordered {
private final ManagementServerProperties properties;
private final String prefix;
AccessLogCustomizer(ManagementServerProperties properties) {
this.properties = properties;
AccessLogCustomizer(String prefix) {
this.prefix = prefix;
}
protected String customizePrefix(String prefix) {
prefix = (prefix != null) ? prefix : "";
if (prefix.startsWith(this.properties.getAccesslog().getPrefix())) {
return prefix;
protected String customizePrefix(String existingPrefix) {
if (this.prefix == null) {
return existingPrefix;
}
return this.properties.getAccesslog().getPrefix() + prefix;
if (existingPrefix == null) {
return this.prefix;
}
if (existingPrefix.startsWith(this.prefix)) {
return existingPrefix;
}
return this.prefix + existingPrefix;
}
@Override
@@ -124,7 +130,7 @@ public class ReactiveManagementChildContextConfiguration {
implements WebServerFactoryCustomizer<TomcatReactiveWebServerFactory> {
TomcatAccessLogCustomizer(ManagementServerProperties properties) {
super(properties);
super(properties.getTomcat().getAccesslog().getPrefix());
}
@Override
@@ -151,7 +157,7 @@ public class ReactiveManagementChildContextConfiguration {
implements WebServerFactoryCustomizer<UndertowReactiveWebServerFactory> {
UndertowAccessLogCustomizer(ManagementServerProperties properties) {
super(properties);
super(properties.getUndertow().getAccesslog().getPrefix());
}
@Override
@@ -165,7 +171,7 @@ public class ReactiveManagementChildContextConfiguration {
implements WebServerFactoryCustomizer<JettyReactiveWebServerFactory> {
JettyAccessLogCustomizer(ManagementServerProperties properties) {
super(properties);
super(properties.getJetty().getAccesslog().getPrefix());
}
@Override

View File

@@ -30,6 +30,7 @@ import org.springframework.util.StringUtils;
* @author Dave Syer
* @author Stephane Nicoll
* @author Vedran Pavic
* @author Moritz Halbritter
* @since 2.0.0
* @see ServerProperties
*/
@@ -57,7 +58,11 @@ public class ManagementServerProperties {
@NestedConfigurationProperty
private Ssl ssl;
private final Accesslog accesslog = new Accesslog();
private final Jetty jetty = new Jetty();
private final Tomcat tomcat = new Tomcat();
private final Undertow undertow = new Undertow();
/**
* Returns the management port or {@code null} if the
@@ -103,6 +108,18 @@ public class ManagementServerProperties {
this.ssl = ssl;
}
public Jetty getJetty() {
return this.jetty;
}
public Tomcat getTomcat() {
return this.tomcat;
}
public Undertow getUndertow() {
return this.undertow;
}
private String cleanBasePath(String basePath) {
String candidate = null;
if (StringUtils.hasLength(basePath)) {
@@ -119,8 +136,34 @@ public class ManagementServerProperties {
return candidate;
}
public Accesslog getAccesslog() {
return this.accesslog;
public static class Jetty {
private final Accesslog accesslog = new Accesslog();
public Accesslog getAccesslog() {
return this.accesslog;
}
}
public static class Tomcat {
private final Accesslog accesslog = new Accesslog();
public Accesslog getAccesslog() {
return this.accesslog;
}
}
public static class Undertow {
private final Accesslog accesslog = new Accesslog();
public Accesslog getAccesslog() {
return this.accesslog;
}
}
public static class Accesslog {

View File

@@ -63,6 +63,7 @@ import org.springframework.util.StringUtils;
* @author Andy Wilkinson
* @author Eddú Meléndez
* @author Phillip Webb
* @author Moritz Halbritter
*/
@ManagementContextConfiguration(value = ManagementContextType.CHILD, proxyBeanMethods = false)
@ConditionalOnWebApplication(type = Type.SERVLET)
@@ -136,18 +137,23 @@ class ServletManagementChildContextConfiguration {
abstract static class AccessLogCustomizer implements Ordered {
private final ManagementServerProperties properties;
private final String prefix;
AccessLogCustomizer(ManagementServerProperties properties) {
this.properties = properties;
AccessLogCustomizer(String prefix) {
this.prefix = prefix;
}
protected String customizePrefix(String prefix) {
prefix = (prefix != null) ? prefix : "";
if (prefix.startsWith(this.properties.getAccesslog().getPrefix())) {
return prefix;
protected String customizePrefix(String existingPrefix) {
if (this.prefix == null) {
return existingPrefix;
}
return this.properties.getAccesslog().getPrefix() + prefix;
if (existingPrefix == null) {
return this.prefix;
}
if (existingPrefix.startsWith(this.prefix)) {
return existingPrefix;
}
return this.prefix + existingPrefix;
}
@Override
@@ -161,7 +167,7 @@ class ServletManagementChildContextConfiguration {
implements WebServerFactoryCustomizer<TomcatServletWebServerFactory> {
TomcatAccessLogCustomizer(ManagementServerProperties properties) {
super(properties);
super(properties.getTomcat().getAccesslog().getPrefix());
}
@Override
@@ -188,7 +194,7 @@ class ServletManagementChildContextConfiguration {
implements WebServerFactoryCustomizer<UndertowServletWebServerFactory> {
UndertowAccessLogCustomizer(ManagementServerProperties properties) {
super(properties);
super(properties.getUndertow().getAccesslog().getPrefix());
}
@Override
@@ -202,7 +208,7 @@ class ServletManagementChildContextConfiguration {
implements WebServerFactoryCustomizer<JettyServletWebServerFactory> {
JettyAccessLogCustomizer(ManagementServerProperties properties) {
super(properties);
super(properties.getJetty().getAccesslog().getPrefix());
}
@Override

View File

@@ -0,0 +1,49 @@
/*
* 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.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.boot.actuate.autoconfigure.web.reactive;
import org.junit.jupiter.api.Test;
import org.springframework.boot.actuate.autoconfigure.web.reactive.ReactiveManagementChildContextConfiguration.AccessLogCustomizer;
import static org.assertj.core.api.Assertions.assertThat;
/**
* Tests for {@link ReactiveManagementChildContextConfiguration}.
*
* @author Moritz Halbritter
*/
class ReactiveManagementChildContextConfigurationTests {
@Test
void accessLogCustomizer() {
AccessLogCustomizer customizer = new AccessLogCustomizer("prefix") {
};
assertThat(customizer.customizePrefix(null)).isEqualTo("prefix");
assertThat(customizer.customizePrefix("existing")).isEqualTo("prefixexisting");
assertThat(customizer.customizePrefix("prefixexisting")).isEqualTo("prefixexisting");
}
@Test
void accessLogCustomizerWithNullPrefix() {
AccessLogCustomizer customizer = new AccessLogCustomizer(null) {
};
assertThat(customizer.customizePrefix(null)).isEqualTo(null);
assertThat(customizer.customizePrefix("existing")).isEqualTo("existing");
}
}

View File

@@ -25,6 +25,7 @@ import static org.assertj.core.api.Assertions.assertThat;
*
* @author Phillip Webb
* @author Stephane Nicoll
* @author Moritz Halbritter
*/
class ManagementServerPropertiesTests {
@@ -71,7 +72,9 @@ class ManagementServerPropertiesTests {
@Test
void accessLogsArePrefixedByDefault() {
ManagementServerProperties properties = new ManagementServerProperties();
assertThat(properties.getAccesslog().getPrefix()).isEqualTo("management_");
assertThat(properties.getTomcat().getAccesslog().getPrefix()).isEqualTo("management_");
assertThat(properties.getJetty().getAccesslog().getPrefix()).isEqualTo("management_");
assertThat(properties.getUndertow().getAccesslog().getPrefix()).isEqualTo("management_");
}
}

View File

@@ -0,0 +1,49 @@
/*
* 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.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.boot.actuate.autoconfigure.web.servlet;
import org.junit.jupiter.api.Test;
import org.springframework.boot.actuate.autoconfigure.web.servlet.ServletManagementChildContextConfiguration.AccessLogCustomizer;
import static org.assertj.core.api.Assertions.assertThat;
/**
* Tests for {@link ServletManagementChildContextConfiguration}.
*
* @author Moritz Halbritter
*/
class ServletManagementChildContextConfigurationTests {
@Test
void accessLogCustomizer() {
AccessLogCustomizer customizer = new AccessLogCustomizer("prefix") {
};
assertThat(customizer.customizePrefix(null)).isEqualTo("prefix");
assertThat(customizer.customizePrefix("existing")).isEqualTo("prefixexisting");
assertThat(customizer.customizePrefix("prefixexisting")).isEqualTo("prefixexisting");
}
@Test
void accessLogCustomizerWithNullPrefix() {
AccessLogCustomizer customizer = new AccessLogCustomizer(null) {
};
assertThat(customizer.customizePrefix(null)).isEqualTo(null);
assertThat(customizer.customizePrefix("existing")).isEqualTo("existing");
}
}