Skip to content

Commit 19ba2ec

Browse files
committed
Merge pull request #44467 from nosan
* pr/44467: Close InputStream when loading Log4j2 Configuration from a Resource Closes gh-44467
2 parents 21a5319 + be7e952 commit 19ba2ec

File tree

3 files changed

+19
-12
lines changed

3 files changed

+19
-12
lines changed

spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/security/oauth2/server/servlet/OAuth2AuthorizationServerWebSecurityConfiguration.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2023 the original author or authors.
2+
* Copyright 2012-2024 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.

spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/security/oauth2/server/servlet/OAuth2AuthorizationServerWebSecurityConfigurationTests.java

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2023 the original author or authors.
2+
* Copyright 2012-2024 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -28,14 +28,15 @@
2828
import org.springframework.context.annotation.Import;
2929
import org.springframework.core.annotation.Order;
3030
import org.springframework.security.config.BeanIds;
31+
import org.springframework.security.config.Customizer;
3132
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
3233
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
3334
import org.springframework.security.oauth2.core.AuthorizationGrantType;
3435
import org.springframework.security.oauth2.core.ClientAuthenticationMethod;
3536
import org.springframework.security.oauth2.server.authorization.client.InMemoryRegisteredClientRepository;
3637
import org.springframework.security.oauth2.server.authorization.client.RegisteredClient;
3738
import org.springframework.security.oauth2.server.authorization.client.RegisteredClientRepository;
38-
import org.springframework.security.oauth2.server.authorization.config.annotation.web.configuration.OAuth2AuthorizationServerConfiguration;
39+
import org.springframework.security.oauth2.server.authorization.config.annotation.web.configurers.OAuth2AuthorizationServerConfigurer;
3940
import org.springframework.security.oauth2.server.authorization.oidc.web.OidcClientRegistrationEndpointFilter;
4041
import org.springframework.security.oauth2.server.authorization.oidc.web.OidcProviderConfigurationEndpointFilter;
4142
import org.springframework.security.oauth2.server.authorization.oidc.web.OidcUserInfoEndpointFilter;
@@ -163,7 +164,11 @@ static class TestSecurityFilterChainConfiguration {
163164
@Bean
164165
@Order(1)
165166
SecurityFilterChain authServerSecurityFilterChain(HttpSecurity http) throws Exception {
166-
OAuth2AuthorizationServerConfiguration.applyDefaultSecurity(http);
167+
OAuth2AuthorizationServerConfigurer authorizationServer = OAuth2AuthorizationServerConfigurer
168+
.authorizationServer();
169+
http.securityMatcher(authorizationServer.getEndpointsMatcher())
170+
.with(authorizationServer, Customizer.withDefaults());
171+
http.authorizeHttpRequests((authorize) -> authorize.anyRequest().authenticated());
167172
return http.build();
168173
}
169174

spring-boot-project/spring-boot/src/main/java/org/springframework/boot/logging/log4j2/Log4J2LoggingSystem.java

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2024 the original author or authors.
2+
* Copyright 2012-2025 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -17,6 +17,7 @@
1717
package org.springframework.boot.logging.log4j2;
1818

1919
import java.io.IOException;
20+
import java.io.InputStream;
2021
import java.net.URL;
2122
import java.net.URLConnection;
2223
import java.util.ArrayList;
@@ -278,13 +279,11 @@ protected void loadConfiguration(String location, LogFile logFile, List<String>
278279

279280
private Configuration load(String location, LoggerContext context) throws IOException {
280281
Resource resource = new ApplicationResourceLoader().getResource(location);
281-
ConfigurationSource source = getConfigurationSource(resource);
282-
return ConfigurationFactory.getInstance().getConfiguration(context, source);
283-
}
284-
285-
private ConfigurationSource getConfigurationSource(Resource resource) throws IOException {
282+
ConfigurationFactory factory = ConfigurationFactory.getInstance();
286283
if (resource.isFile()) {
287-
return new ConfigurationSource(resource.getInputStream(), resource.getFile());
284+
try (InputStream inputStream = resource.getInputStream()) {
285+
return factory.getConfiguration(context, new ConfigurationSource(inputStream, resource.getFile()));
286+
}
288287
}
289288
URL url = resource.getURL();
290289
AuthorizationProvider authorizationProvider = ConfigurationFactory
@@ -293,7 +292,10 @@ private ConfigurationSource getConfigurationSource(Resource resource) throws IOE
293292
? SslConfigurationFactory.getSslConfiguration() : null;
294293
URLConnection connection = UrlConnectionFactory.createConnection(url, 0, sslConfiguration,
295294
authorizationProvider);
296-
return new ConfigurationSource(connection.getInputStream(), url, connection.getLastModified());
295+
try (InputStream inputStream = connection.getInputStream()) {
296+
return factory.getConfiguration(context,
297+
new ConfigurationSource(inputStream, url, connection.getLastModified()));
298+
}
297299
}
298300

299301
private CompositeConfiguration createComposite(List<Configuration> configurations) {

0 commit comments

Comments
 (0)