|
1 | 1 | /*
|
2 |
| - * Copyright 2012-2014 the original author or authors. |
| 2 | + * Copyright 2012-2015 the original author or authors. |
3 | 3 | *
|
4 | 4 | * Licensed under the Apache License, Version 2.0 (the "License");
|
5 | 5 | * you may not use this file except in compliance with the License.
|
|
40 | 40 | import org.springframework.security.authentication.BadCredentialsException;
|
41 | 41 | import org.springframework.security.authentication.TestingAuthenticationToken;
|
42 | 42 | import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
|
| 43 | +import org.springframework.security.authentication.event.AbstractAuthenticationEvent; |
43 | 44 | import org.springframework.security.authentication.event.AuthenticationFailureBadCredentialsEvent;
|
44 | 45 | import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
|
45 | 46 | import org.springframework.security.config.annotation.authentication.configurers.GlobalAuthenticationConfigurerAdapter;
|
@@ -226,6 +227,60 @@ public void testJpaCoexistsHappily() throws Exception {
|
226 | 227 | assertNotNull(this.context.getBean(JpaTransactionManager.class));
|
227 | 228 | }
|
228 | 229 |
|
| 230 | + @Test |
| 231 | + public void testDefaultUsernamePassword() throws Exception { |
| 232 | + this.context = new AnnotationConfigWebApplicationContext(); |
| 233 | + this.context.setServletContext(new MockServletContext()); |
| 234 | + |
| 235 | + this.context.register(SecurityAutoConfiguration.class, |
| 236 | + ServerPropertiesAutoConfiguration.class); |
| 237 | + this.context.refresh(); |
| 238 | + |
| 239 | + SecurityProperties security = this.context.getBean(SecurityProperties.class); |
| 240 | + AuthenticationManager manager = this.context.getBean(AuthenticationManager.class); |
| 241 | + |
| 242 | + UsernamePasswordAuthenticationToken token = new UsernamePasswordAuthenticationToken( |
| 243 | + security.getUser().getName(), security.getUser().getPassword()); |
| 244 | + assertNotNull(manager.authenticate(token)); |
| 245 | + } |
| 246 | + |
| 247 | + @Test |
| 248 | + public void testCustomAuthenticationDoesNotAuthenticateWithBootSecurityUser() |
| 249 | + throws Exception { |
| 250 | + this.context = new AnnotationConfigWebApplicationContext(); |
| 251 | + this.context.setServletContext(new MockServletContext()); |
| 252 | + |
| 253 | + this.context.register(AuthenticationManagerCustomizer.class, |
| 254 | + SecurityAutoConfiguration.class, ServerPropertiesAutoConfiguration.class); |
| 255 | + this.context.refresh(); |
| 256 | + |
| 257 | + SecurityProperties security = this.context.getBean(SecurityProperties.class); |
| 258 | + AuthenticationManager manager = this.context.getBean(AuthenticationManager.class); |
| 259 | + |
| 260 | + UsernamePasswordAuthenticationToken token = new UsernamePasswordAuthenticationToken( |
| 261 | + security.getUser().getName(), security.getUser().getPassword()); |
| 262 | + try { |
| 263 | + manager.authenticate(token); |
| 264 | + fail("Expected Exception"); |
| 265 | + } |
| 266 | + catch (AuthenticationException success) { |
| 267 | + } |
| 268 | + |
| 269 | + token = new UsernamePasswordAuthenticationToken("foo", "bar"); |
| 270 | + assertNotNull(manager.authenticate(token)); |
| 271 | + } |
| 272 | + |
| 273 | + private static final class AuthenticationListener implements |
| 274 | + ApplicationListener<AbstractAuthenticationEvent> { |
| 275 | + |
| 276 | + private ApplicationEvent event; |
| 277 | + |
| 278 | + @Override |
| 279 | + public void onApplicationEvent(AbstractAuthenticationEvent event) { |
| 280 | + this.event = event; |
| 281 | + } |
| 282 | + } |
| 283 | + |
229 | 284 | @Configuration
|
230 | 285 | @TestAutoConfigurationPackage(City.class)
|
231 | 286 | protected static class EntityConfiguration {
|
|
0 commit comments