pull/10447/head
Phillip Webb 7 years ago
parent c592e3b67d
commit bec2e97b95

@ -42,8 +42,8 @@ import org.springframework.security.provisioning.InMemoryUserDetailsManager;
/**
* Configuration for a Spring Security in-memory {@link AuthenticationManager}. Adds an
* {@link InMemoryUserDetailsManager} with a default user and generated password.
* This can be disabled by providing a bean of type {@link AuthenticationManager},
* {@link InMemoryUserDetailsManager} with a default user and generated password. This can
* be disabled by providing a bean of type {@link AuthenticationManager},
* {@link AuthenticationProvider} or {@link UserDetailsService}.
*
* @author Dave Syer
@ -63,9 +63,9 @@ public class AuthenticationManagerConfiguration {
UserDetailsService.class })
public InMemoryUserDetailsManager inMemoryUserDetailsManager() throws Exception {
String password = UUID.randomUUID().toString();
logger.info(
String.format("%n%nUsing default security password: %s%n", password));
return new InMemoryUserDetailsManager(User.withUsername("user").password(password).roles().build());
logger.info(String.format("%n%nUsing default security password: %s%n", password));
return new InMemoryUserDetailsManager(
User.withUsername("user").password(password).roles().build());
}
@Bean

@ -150,15 +150,15 @@ public class SecurityAutoConfigurationTests {
public void testEventPublisherInjected() throws Exception {
this.context = new AnnotationConfigWebApplicationContext();
this.context.setServletContext(new MockServletContext());
this.context.register(TestAuthenticationManagerConfiguration.class, SecurityAutoConfiguration.class,
this.context.register(TestAuthenticationManagerConfiguration.class,
SecurityAutoConfiguration.class,
PropertyPlaceholderAutoConfiguration.class);
this.context.refresh();
AuthenticationListener listener = new AuthenticationListener();
this.context.addApplicationListener(listener);
AuthenticationManager manager = this.context.getBean(AuthenticationManager.class);
manager.authenticate(new TestingAuthenticationToken("foo", "wrong"));
assertThat(listener.event)
.isInstanceOf(AuthenticationSuccessEvent.class);
assertThat(listener.event).isInstanceOf(AuthenticationSuccessEvent.class);
}
@Test
@ -168,12 +168,14 @@ public class SecurityAutoConfigurationTests {
this.context.register(SecurityAutoConfiguration.class);
this.context.refresh();
UserDetailsService manager = this.context.getBean(UserDetailsService.class);
assertThat(this.outputCapture.toString()).contains("Using default security password:");
assertThat(this.outputCapture.toString())
.contains("Using default security password:");
assertThat(manager.loadUserByUsername("user")).isNotNull();
}
@Test
public void defaultUserNotCreatedIfAuthenticationManagerBeanPresent() throws Exception {
public void defaultUserNotCreatedIfAuthenticationManagerBeanPresent()
throws Exception {
this.context = new AnnotationConfigWebApplicationContext();
this.context.setServletContext(new MockServletContext());
this.context.register(TestAuthenticationManagerConfiguration.class,
@ -181,13 +183,11 @@ public class SecurityAutoConfigurationTests {
PropertyPlaceholderAutoConfiguration.class);
this.context.refresh();
AuthenticationManager manager = this.context.getBean(AuthenticationManager.class);
assertThat(manager)
.isEqualTo(this.context.getBean(
assertThat(manager).isEqualTo(this.context.getBean(
TestAuthenticationManagerConfiguration.class).authenticationManager);
assertThat(this.outputCapture.toString())
.doesNotContain("Using default security password: ");
TestingAuthenticationToken token = new TestingAuthenticationToken(
"foo", "bar");
TestingAuthenticationToken token = new TestingAuthenticationToken("foo", "bar");
assertThat(manager.authenticate(token)).isNotNull();
}
@ -199,25 +199,27 @@ public class SecurityAutoConfigurationTests {
SecurityAutoConfiguration.class,
PropertyPlaceholderAutoConfiguration.class);
this.context.refresh();
UserDetailsService userDetailsService = this.context.getBean(UserDetailsService.class);
UserDetailsService userDetailsService = this.context
.getBean(UserDetailsService.class);
assertThat(this.outputCapture.toString())
.doesNotContain("Using default security password: ");
assertThat(userDetailsService.loadUserByUsername("foo")).isNotNull();
}
@Test
public void defaultUserNotCreatedIfAuthenticationProviderBeanPresent() throws Exception {
public void defaultUserNotCreatedIfAuthenticationProviderBeanPresent()
throws Exception {
this.context = new AnnotationConfigWebApplicationContext();
this.context.setServletContext(new MockServletContext());
this.context.register(TestAuthenticationProviderConfiguration.class,
SecurityAutoConfiguration.class,
PropertyPlaceholderAutoConfiguration.class);
this.context.refresh();
AuthenticationProvider provider = this.context.getBean(AuthenticationProvider.class);
AuthenticationProvider provider = this.context
.getBean(AuthenticationProvider.class);
assertThat(this.outputCapture.toString())
.doesNotContain("Using default security password: ");
TestingAuthenticationToken token = new TestingAuthenticationToken(
"foo", "bar");
TestingAuthenticationToken token = new TestingAuthenticationToken("foo", "bar");
assertThat(provider.authenticate(token)).isNotNull();
}
@ -311,7 +313,8 @@ public class SecurityAutoConfigurationTests {
@Bean
public AuthenticationManager myAuthenticationManager() {
AuthenticationProvider authenticationProvider = new TestingAuthenticationProvider();
this.authenticationManager = new ProviderManager(Collections.singletonList(authenticationProvider));
this.authenticationManager = new ProviderManager(
Collections.singletonList(authenticationProvider));
return this.authenticationManager;
}
@ -322,7 +325,8 @@ public class SecurityAutoConfigurationTests {
@Bean
public InMemoryUserDetailsManager myUserDetailsService() {
return new InMemoryUserDetailsManager(User.withUsername("foo").password("bar").roles("USER").build());
return new InMemoryUserDetailsManager(
User.withUsername("foo").password("bar").roles("USER").build());
}
}

Loading…
Cancel
Save