|
17 | 17 | package org.springframework.boot.autoconfigure.security;
|
18 | 18 |
|
19 | 19 | import java.util.List;
|
| 20 | +import java.util.concurrent.atomic.AtomicReference; |
20 | 21 |
|
21 | 22 | import org.junit.Test;
|
22 | 23 | import org.springframework.boot.autoconfigure.PropertyPlaceholderAutoConfiguration;
|
|
26 | 27 | import org.springframework.boot.autoconfigure.orm.jpa.test.City;
|
27 | 28 | import org.springframework.boot.autoconfigure.web.ServerPropertiesAutoConfiguration;
|
28 | 29 | import org.springframework.boot.test.EnvironmentTestUtils;
|
| 30 | +import org.springframework.context.ApplicationEvent; |
| 31 | +import org.springframework.context.ApplicationListener; |
29 | 32 | import org.springframework.context.annotation.Bean;
|
30 | 33 | import org.springframework.context.annotation.Configuration;
|
31 | 34 | import org.springframework.mock.web.MockServletContext;
|
32 | 35 | import org.springframework.orm.jpa.JpaTransactionManager;
|
33 | 36 | import org.springframework.security.authentication.AuthenticationManager;
|
| 37 | +import org.springframework.security.authentication.BadCredentialsException; |
34 | 38 | import org.springframework.security.authentication.TestingAuthenticationToken;
|
| 39 | +import org.springframework.security.authentication.UsernamePasswordAuthenticationToken; |
| 40 | +import org.springframework.security.authentication.event.AuthenticationFailureBadCredentialsEvent; |
35 | 41 | import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
|
36 | 42 | import org.springframework.security.core.Authentication;
|
37 | 43 | import org.springframework.security.core.AuthenticationException;
|
|
41 | 47 |
|
42 | 48 | import static org.junit.Assert.assertEquals;
|
43 | 49 | import static org.junit.Assert.assertNotNull;
|
| 50 | +import static org.junit.Assert.assertTrue; |
| 51 | +import static org.junit.Assert.fail; |
44 | 52 |
|
45 | 53 | /**
|
46 | 54 | * Tests for {@link SecurityAutoConfiguration}.
|
@@ -104,6 +112,27 @@ public void testAuthenticationManagerCreated() throws Exception {
|
104 | 112 | assertNotNull(this.context.getBean(AuthenticationManager.class));
|
105 | 113 | }
|
106 | 114 |
|
| 115 | + @Test |
| 116 | + public void testEventPublisherInjected() throws Exception { |
| 117 | + testAuthenticationManagerCreated(); |
| 118 | + final AtomicReference<ApplicationEvent> wrapper = new AtomicReference<ApplicationEvent>(); |
| 119 | + this.context.addApplicationListener(new ApplicationListener<ApplicationEvent>() { |
| 120 | + @Override |
| 121 | + public void onApplicationEvent(ApplicationEvent event) { |
| 122 | + wrapper.set(event); |
| 123 | + }; |
| 124 | + }); |
| 125 | + AuthenticationManager manager = this.context.getBean(AuthenticationManager.class); |
| 126 | + try { |
| 127 | + manager.authenticate(new UsernamePasswordAuthenticationToken("foo", "bar")); |
| 128 | + fail("Expected BadCredentialsException"); |
| 129 | + } |
| 130 | + catch (BadCredentialsException e) { |
| 131 | + // expected |
| 132 | + } |
| 133 | + assertTrue(wrapper.get() instanceof AuthenticationFailureBadCredentialsEvent); |
| 134 | + } |
| 135 | + |
107 | 136 | @Test
|
108 | 137 | public void testOverrideAuthenticationManager() throws Exception {
|
109 | 138 | this.context = new AnnotationConfigWebApplicationContext();
|
|
0 commit comments