diff --git a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/web/ServerProperties.java b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/web/ServerProperties.java index ba36a8b251f..be11fbfb010 100644 --- a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/web/ServerProperties.java +++ b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/web/ServerProperties.java @@ -949,15 +949,16 @@ public class ServerProperties { private boolean logLatency; /** - * Set request paths that will not be logged. - */ - private List ignorePaths; - - /** - * true - IP address from header will be logged, false - IP address from the connection will be logged + * Whether to log IP address from the "X-Forwarded-For" header rather than the + * one from the connection. */ private boolean preferProxiedForAddress = false; + /** + * Request paths that should not be logged. + */ + private List ignorePaths; + public boolean isEnabled() { return this.enabled; } @@ -1054,21 +1055,22 @@ public class ServerProperties { this.logLatency = logLatency; } + public boolean isPreferProxiedForAddress() { + return this.preferProxiedForAddress; + } + + public void setPreferProxiedForAddress(boolean preferProxiedForAddress) { + this.preferProxiedForAddress = preferProxiedForAddress; + } + public List getIgnorePaths() { - return ignorePaths; + return this.ignorePaths; } public void setIgnorePaths(List ignorePaths) { this.ignorePaths = ignorePaths; } - public boolean getPreferProxiedForAddress(){ - return preferProxiedForAddress; - } - - public void setPreferProxiedForAddress(boolean preferProxiedForAddress){ - this.preferProxiedForAddress = preferProxiedForAddress; - } } } diff --git a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/web/embedded/JettyWebServerFactoryCustomizer.java b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/web/embedded/JettyWebServerFactoryCustomizer.java index 33e23b77594..5268153f531 100644 --- a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/web/embedded/JettyWebServerFactoryCustomizer.java +++ b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/web/embedded/JettyWebServerFactoryCustomizer.java @@ -37,6 +37,7 @@ import org.springframework.boot.web.embedded.jetty.JettyServerCustomizer; import org.springframework.boot.web.server.WebServerFactoryCustomizer; import org.springframework.core.Ordered; import org.springframework.core.env.Environment; +import org.springframework.util.CollectionUtils; import org.springframework.util.unit.DataSize; /** @@ -167,13 +168,13 @@ public class JettyWebServerFactoryCustomizer implements if (properties.getTimeZone() != null) { log.setLogTimeZone(properties.getTimeZone().getID()); } - if (properties.getIgnorePaths() != null) { - log.setIgnorePaths(properties.getIgnorePaths().toArray(new String[0])); - } log.setLogCookies(properties.isLogCookies()); log.setLogServer(properties.isLogServer()); log.setLogLatency(properties.isLogLatency()); - log.setPreferProxiedForAddress(properties.getPreferProxiedForAddress()); + log.setPreferProxiedForAddress(properties.isPreferProxiedForAddress()); + if (!CollectionUtils.isEmpty(properties.getIgnorePaths())) { + log.setIgnorePaths(properties.getIgnorePaths().toArray(new String[0])); + } server.setRequestLog(log); }); } diff --git a/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/web/ServerPropertiesTests.java b/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/web/ServerPropertiesTests.java index 337509afda3..8160bd4e7f5 100644 --- a/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/web/ServerPropertiesTests.java +++ b/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/web/ServerPropertiesTests.java @@ -231,8 +231,8 @@ public class ServerPropertiesTests { map.put("server.jetty.accesslog.file-date-format", "yyyymmdd"); map.put("server.jetty.accesslog.retention-period", "4"); map.put("server.jetty.accesslog.append", "true"); - map.put("server.jetty.accesslog.ignore-paths[0]", "/a/path"); - map.put("server.jetty.accesslog.ignore-paths[1]", "/b/path"); + map.put("server.jetty.accesslog.prefer-proxied-for-address", "true"); + map.put("server.jetty.accesslog.ignore-paths", "/a/path,/b/path"); bind(map); ServerProperties.Jetty jetty = this.properties.getJetty(); assertThat(jetty.getAccesslog().isEnabled()).isTrue(); @@ -240,9 +240,9 @@ public class ServerPropertiesTests { assertThat(jetty.getAccesslog().getFileDateFormat()).isEqualTo("yyyymmdd"); assertThat(jetty.getAccesslog().getRetentionPeriod()).isEqualTo(4); assertThat(jetty.getAccesslog().isAppend()).isTrue(); - assertThat(jetty.getAccesslog().getIgnorePaths().size()).isEqualTo(2); - assertThat(jetty.getAccesslog().getIgnorePaths().get(0)).isEqualTo("/a/path"); - assertThat(jetty.getAccesslog().getIgnorePaths().get(1)).isEqualTo("/b/path"); + assertThat(jetty.getAccesslog().isPreferProxiedForAddress()).isTrue(); + assertThat(jetty.getAccesslog().getIgnorePaths()).containsExactly("/a/path", + "/b/path"); } @Test diff --git a/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/web/embedded/JettyWebServerFactoryCustomizerTests.java b/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/web/embedded/JettyWebServerFactoryCustomizerTests.java index 099c246e38b..a5988e6ad43 100644 --- a/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/web/embedded/JettyWebServerFactoryCustomizerTests.java +++ b/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/web/embedded/JettyWebServerFactoryCustomizerTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2012-2018 the original author or authors. + * Copyright 2012-2019 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. @@ -16,16 +16,13 @@ package org.springframework.boot.autoconfigure.web.embedded; -import static org.assertj.core.api.Assertions.assertThat; -import static org.mockito.Mockito.mock; -import static org.mockito.Mockito.verify; - import java.io.File; import java.io.IOException; import java.util.ArrayList; import java.util.List; import java.util.Locale; import java.util.TimeZone; + import org.eclipse.jetty.server.Connector; import org.eclipse.jetty.server.HttpConfiguration; import org.eclipse.jetty.server.HttpConfiguration.ConnectionFactory; @@ -33,6 +30,7 @@ import org.eclipse.jetty.server.NCSARequestLog; import org.eclipse.jetty.server.RequestLog; import org.junit.Before; import org.junit.Test; + import org.springframework.boot.autoconfigure.web.ServerProperties; import org.springframework.boot.context.properties.bind.Bindable; import org.springframework.boot.context.properties.bind.Binder; @@ -43,6 +41,10 @@ import org.springframework.boot.web.embedded.jetty.JettyWebServer; import org.springframework.mock.env.MockEnvironment; import org.springframework.test.context.support.TestPropertySourceUtils; +import static org.assertj.core.api.Assertions.assertThat; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.verify; + /** * Tests for {@link JettyWebServerFactoryCustomizer}. * @@ -101,8 +103,7 @@ public class JettyWebServerFactoryCustomizerTests { "server.jetty.accesslog.log-server=true", "server.jetty.accesslog.log-latency=true", "server.jetty.accesslog.prefer-proxied-for-address=true", - "server.jetty.accesslog.ignore-paths[0]=/a/path", - "server.jetty.accesslog.ignore-paths[1]=/b/path"); + "server.jetty.accesslog.ignore-paths=/a/path,/b/path"); JettyWebServer server = customizeAndGetServer(); NCSARequestLog requestLog = getNCSARequestLog(server); assertThat(requestLog.getFilename()).isEqualTo(logFile.getAbsolutePath()); @@ -118,8 +119,7 @@ public class JettyWebServerFactoryCustomizerTests { assertThat(requestLog.getLogLatency()).isTrue(); assertThat(requestLog.getPreferProxiedForAddress()).isTrue(); assertThat(requestLog.getIgnorePaths().length).isEqualTo(2); - assertThat(requestLog.getIgnorePaths()[0]).isEqualTo("/a/path"); - assertThat(requestLog.getIgnorePaths()[1]).isEqualTo("/b/path"); + assertThat(requestLog.getIgnorePaths()).containsExactly("/a/path", "/b/path"); } @Test @@ -133,8 +133,8 @@ public class JettyWebServerFactoryCustomizerTests { assertThat(requestLog.getLogCookies()).isFalse(); assertThat(requestLog.getLogServer()).isFalse(); assertThat(requestLog.getLogLatency()).isFalse(); - assertThat(requestLog.getIgnorePaths().length).isZero(); assertThat(requestLog.getPreferProxiedForAddress()).isFalse(); + assertThat(requestLog.getIgnorePaths()).isNull(); } private NCSARequestLog getNCSARequestLog(JettyWebServer server) {