diff --git a/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/logging/log4j2/Log4J2LoggingSystem.java b/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/logging/log4j2/Log4J2LoggingSystem.java index 38c19b4689..2dc0600417 100644 --- a/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/logging/log4j2/Log4J2LoggingSystem.java +++ b/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/logging/log4j2/Log4J2LoggingSystem.java @@ -17,8 +17,8 @@ package org.springframework.boot.logging.log4j2; import java.io.IOException; -import java.io.InputStream; import java.net.URL; +import java.net.URLConnection; import java.util.ArrayList; import java.util.Collections; import java.util.LinkedHashMap; @@ -43,6 +43,10 @@ import org.apache.logging.log4j.core.config.ConfigurationSource; import org.apache.logging.log4j.core.config.LoggerConfig; import org.apache.logging.log4j.core.config.composite.CompositeConfiguration; import org.apache.logging.log4j.core.filter.AbstractFilter; +import org.apache.logging.log4j.core.net.UrlConnectionFactory; +import org.apache.logging.log4j.core.net.ssl.SslConfiguration; +import org.apache.logging.log4j.core.net.ssl.SslConfigurationFactory; +import org.apache.logging.log4j.core.util.AuthorizationProvider; import org.apache.logging.log4j.core.util.NameUtil; import org.apache.logging.log4j.jul.Log4jBridgeHandler; import org.apache.logging.log4j.message.Message; @@ -300,11 +304,16 @@ public class Log4J2LoggingSystem extends AbstractLoggingSystem { } private ConfigurationSource getConfigurationSource(URL url) throws IOException { - InputStream stream = url.openStream(); if (FILE_PROTOCOL.equals(url.getProtocol())) { - return new ConfigurationSource(stream, ResourceUtils.getFile(url)); - } - return new ConfigurationSource(stream, url); + return new ConfigurationSource(url.openStream(), ResourceUtils.getFile(url)); + } + AuthorizationProvider authorizationProvider = ConfigurationFactory + .authorizationProvider(PropertiesUtil.getProperties()); + SslConfiguration sslConfiguration = url.getProtocol().equals("https") + ? SslConfigurationFactory.getSslConfiguration() : null; + URLConnection connection = UrlConnectionFactory.createConnection(url, 0, sslConfiguration, + authorizationProvider); + return new ConfigurationSource(connection.getInputStream(), url, connection.getLastModified()); } private CompositeConfiguration createComposite(List configurations) { diff --git a/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/logging/log4j2/Log4J2LoggingSystemTests.java b/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/logging/log4j2/Log4J2LoggingSystemTests.java index 8d0955e54f..e8f5fbe5ea 100644 --- a/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/logging/log4j2/Log4J2LoggingSystemTests.java +++ b/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/logging/log4j2/Log4J2LoggingSystemTests.java @@ -19,6 +19,7 @@ package org.springframework.boot.logging.log4j2; import java.beans.PropertyChangeEvent; import java.beans.PropertyChangeListener; import java.io.File; +import java.net.ProtocolException; import java.util.EnumSet; import java.util.LinkedHashMap; import java.util.List; @@ -470,6 +471,15 @@ class Log4J2LoggingSystemTests extends AbstractLoggingSystemTests { assertThat(properties.getStringProperty("spring")).isEqualTo("boot"); } + @Test + void nonFileUrlsAreResolvedUsingLog4J2UrlConnectionFactory() { + this.loggingSystem.beforeInitialize(); + assertThatIllegalStateException() + .isThrownBy(() -> this.loggingSystem.initialize(this.initializationContext, + "http://localhost:8080/shouldnotwork", null)) + .havingCause().isInstanceOf(ProtocolException.class).withMessageContaining("http has not been enabled"); + } + private String getRelativeClasspathLocation(String fileName) { String defaultPath = ClassUtils.getPackageName(getClass()); defaultPath = defaultPath.replace('.', '/');