From 4194baad91d786a8ec4337f75a1dbfa1b90c227b Mon Sep 17 00:00:00 2001 From: Madhura Bhave Date: Thu, 7 Jun 2018 12:40:06 -0700 Subject: [PATCH] Don't log p/w when AuthenticationManagerBuilder configured Fixes gh-12872 --- .../UserDetailsServiceAutoConfiguration.java | 2 ++ ...rDetailsServiceAutoConfigurationTests.java | 29 +++++++++++++++++++ 2 files changed, 31 insertions(+) diff --git a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/security/servlet/UserDetailsServiceAutoConfiguration.java b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/security/servlet/UserDetailsServiceAutoConfiguration.java index 92570aee64..3beedfbb39 100644 --- a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/security/servlet/UserDetailsServiceAutoConfiguration.java +++ b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/security/servlet/UserDetailsServiceAutoConfiguration.java @@ -30,6 +30,7 @@ import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean import org.springframework.boot.autoconfigure.security.SecurityProperties; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; +import org.springframework.context.annotation.Lazy; import org.springframework.security.authentication.AuthenticationManager; import org.springframework.security.authentication.AuthenticationProvider; import org.springframework.security.config.annotation.ObjectPostProcessor; @@ -67,6 +68,7 @@ public class UserDetailsServiceAutoConfiguration { @Bean @ConditionalOnMissingBean(type = "org.springframework.security.oauth2.client.registration.ClientRegistrationRepository") + @Lazy public InMemoryUserDetailsManager inMemoryUserDetailsManager( SecurityProperties properties, ObjectProvider passwordEncoder) { diff --git a/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/security/servlet/UserDetailsServiceAutoConfigurationTests.java b/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/security/servlet/UserDetailsServiceAutoConfigurationTests.java index fd9e38f1ef..e734b291c7 100644 --- a/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/security/servlet/UserDetailsServiceAutoConfigurationTests.java +++ b/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/security/servlet/UserDetailsServiceAutoConfigurationTests.java @@ -34,7 +34,9 @@ import org.springframework.security.authentication.AuthenticationProvider; import org.springframework.security.authentication.ProviderManager; import org.springframework.security.authentication.TestingAuthenticationProvider; import org.springframework.security.authentication.TestingAuthenticationToken; +import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder; import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity; +import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter; import org.springframework.security.core.userdetails.User; import org.springframework.security.core.userdetails.UserDetailsService; import org.springframework.security.crypto.password.PasswordEncoder; @@ -149,6 +151,14 @@ public class UserDetailsServiceAutoConfigurationTests { .doesNotHaveBean(InMemoryUserDetailsManager.class))); } + @Test + public void generatedPasswordShouldNotBePrintedIfAuthenticationManagerBuilderIsUsed() { + this.contextRunner + .withUserConfiguration(TestConfigWithAuthenticationManagerBuilder.class) + .run(((context) -> assertThat(this.outputCapture.toString()) + .doesNotContain("Using generated security password: "))); + } + private void testPasswordEncoding(Class configClass, String providedPassword, String expectedPassword) { this.contextRunner.withUserConfiguration(configClass) @@ -227,4 +237,23 @@ public class UserDetailsServiceAutoConfigurationTests { } + @Configuration + @Import(TestSecurityConfiguration.class) + protected static class TestConfigWithAuthenticationManagerBuilder { + + @Bean + public WebSecurityConfigurerAdapter webSecurityConfigurerAdapter() { + return new WebSecurityConfigurerAdapter() { + @Override + protected void configure(AuthenticationManagerBuilder auth) + throws Exception { + auth.inMemoryAuthentication().withUser("hero").password("{noop}hero") + .roles("HERO", "USER").and().withUser("user") + .password("{noop}user").roles("USER"); + } + }; + } + + } + }