16
16
17
17
package org .springframework .boot .autoconfigure .security ;
18
18
19
+ import static org .junit .Assert .assertEquals ;
20
+ import static org .junit .Assert .assertNotNull ;
21
+ import static org .junit .Assert .assertTrue ;
22
+ import static org .junit .Assert .fail ;
23
+
19
24
import java .util .List ;
20
25
import java .util .concurrent .atomic .AtomicReference ;
21
26
22
27
import org .junit .After ;
23
28
import org .junit .Test ;
29
+ import org .springframework .beans .factory .annotation .Autowired ;
24
30
import org .springframework .boot .autoconfigure .PropertyPlaceholderAutoConfiguration ;
25
31
import org .springframework .boot .autoconfigure .TestAutoConfigurationPackage ;
26
32
import org .springframework .boot .autoconfigure .jdbc .DataSourceAutoConfiguration ;
32
38
import org .springframework .context .ApplicationListener ;
33
39
import org .springframework .context .annotation .Bean ;
34
40
import org .springframework .context .annotation .Configuration ;
41
+ import org .springframework .core .annotation .Order ;
35
42
import org .springframework .mock .web .MockServletContext ;
36
43
import org .springframework .orm .jpa .JpaTransactionManager ;
37
44
import org .springframework .security .authentication .AuthenticationManager ;
40
47
import org .springframework .security .authentication .UsernamePasswordAuthenticationToken ;
41
48
import org .springframework .security .authentication .event .AuthenticationFailureBadCredentialsEvent ;
42
49
import org .springframework .security .config .annotation .authentication .builders .AuthenticationManagerBuilder ;
50
+ import org .springframework .security .config .annotation .authentication .configurers .GlobalAuthenticationConfigurerAdapter ;
51
+ import org .springframework .security .config .annotation .web .builders .HttpSecurity ;
52
+ import org .springframework .security .config .annotation .web .configuration .WebSecurityConfigurerAdapter ;
43
53
import org .springframework .security .core .Authentication ;
44
54
import org .springframework .security .core .AuthenticationException ;
55
+ import org .springframework .security .core .authority .AuthorityUtils ;
45
56
import org .springframework .security .web .FilterChainProxy ;
46
57
import org .springframework .security .web .SecurityFilterChain ;
47
58
import org .springframework .web .context .support .AnnotationConfigWebApplicationContext ;
48
59
49
- import static org .junit .Assert .assertEquals ;
50
- import static org .junit .Assert .assertNotNull ;
51
- import static org .junit .Assert .assertTrue ;
52
- import static org .junit .Assert .fail ;
53
-
54
60
/**
55
61
* Tests for {@link SecurityAutoConfiguration}.
56
62
*
@@ -138,7 +144,8 @@ public void onApplicationEvent(ApplicationEvent event) {
138
144
catch (BadCredentialsException e ) {
139
145
// expected
140
146
}
141
- assertTrue (wrapper .get () instanceof AuthenticationFailureBadCredentialsEvent );
147
+ assertTrue ("Wrong event type: " + wrapper .get (),
148
+ wrapper .get () instanceof AuthenticationFailureBadCredentialsEvent );
142
149
}
143
150
144
151
@ Test
@@ -154,6 +161,55 @@ public void testOverrideAuthenticationManager() throws Exception {
154
161
this .context .getBean (AuthenticationManager .class ));
155
162
}
156
163
164
+ @ Test
165
+ public void testOverrideAuthenticationManagerAndInjectIntoSecurityFilter ()
166
+ throws Exception {
167
+ this .context = new AnnotationConfigWebApplicationContext ();
168
+ this .context .setServletContext (new MockServletContext ());
169
+ this .context .register (TestAuthenticationConfiguration .class ,
170
+ SecurityCustomizer .class , SecurityAutoConfiguration .class ,
171
+ ServerPropertiesAutoConfiguration .class ,
172
+ PropertyPlaceholderAutoConfiguration .class );
173
+ this .context .refresh ();
174
+ assertEquals (
175
+ this .context .getBean (TestAuthenticationConfiguration .class ).authenticationManager ,
176
+ this .context .getBean (AuthenticationManager .class ));
177
+ }
178
+
179
+ @ Test
180
+ public void testOverrideAuthenticationManagerWithBuilderAndInjectIntoSecurityFilter ()
181
+ throws Exception {
182
+ this .context = new AnnotationConfigWebApplicationContext ();
183
+ this .context .setServletContext (new MockServletContext ());
184
+ this .context .register (AuthenticationManagerCustomizer .class ,
185
+ SecurityCustomizer .class , SecurityAutoConfiguration .class ,
186
+ ServerPropertiesAutoConfiguration .class ,
187
+ PropertyPlaceholderAutoConfiguration .class );
188
+ this .context .refresh ();
189
+ UsernamePasswordAuthenticationToken user = new UsernamePasswordAuthenticationToken (
190
+ "foo" , "bar" ,
191
+ AuthorityUtils .commaSeparatedStringToAuthorityList ("ROLE_USER" ));
192
+ assertNotNull (this .context .getBean (AuthenticationManager .class )
193
+ .authenticate (user ));
194
+ }
195
+
196
+ @ Test
197
+ public void testOverrideAuthenticationManagerWithBuilderAndInjectBuilderIntoSecurityFilter ()
198
+ throws Exception {
199
+ this .context = new AnnotationConfigWebApplicationContext ();
200
+ this .context .setServletContext (new MockServletContext ());
201
+ this .context .register (AuthenticationManagerCustomizer .class ,
202
+ WorkaroundSecurityCustomizer .class , SecurityAutoConfiguration .class ,
203
+ ServerPropertiesAutoConfiguration .class ,
204
+ PropertyPlaceholderAutoConfiguration .class );
205
+ this .context .refresh ();
206
+ UsernamePasswordAuthenticationToken user = new UsernamePasswordAuthenticationToken (
207
+ "foo" , "bar" ,
208
+ AuthorityUtils .commaSeparatedStringToAuthorityList ("ROLE_USER" ));
209
+ assertNotNull (this .context .getBean (AuthenticationManager .class )
210
+ .authenticate (user ));
211
+ }
212
+
157
213
@ Test
158
214
public void testJpaCoexistsHappily () throws Exception {
159
215
this .context = new AnnotationConfigWebApplicationContext ();
@@ -196,4 +252,48 @@ public Authentication authenticate(Authentication authentication)
196
252
197
253
}
198
254
255
+ @ Configuration
256
+ protected static class SecurityCustomizer extends WebSecurityConfigurerAdapter {
257
+
258
+ @ Autowired
259
+ private AuthenticationManager authenticationManager ;
260
+
261
+ }
262
+
263
+ @ Configuration
264
+ protected static class WorkaroundSecurityCustomizer extends
265
+ WebSecurityConfigurerAdapter {
266
+
267
+ @ Autowired
268
+ private AuthenticationManagerBuilder builder ;
269
+
270
+ @ SuppressWarnings ("unused" )
271
+ private AuthenticationManager authenticationManager ;
272
+
273
+ @ Override
274
+ protected void configure (HttpSecurity http ) throws Exception {
275
+ this .authenticationManager = new AuthenticationManager () {
276
+ @ Override
277
+ public Authentication authenticate (Authentication authentication )
278
+ throws AuthenticationException {
279
+ return WorkaroundSecurityCustomizer .this .builder .getOrBuild ()
280
+ .authenticate (authentication );
281
+ }
282
+ };
283
+ }
284
+
285
+ }
286
+
287
+ @ Configuration
288
+ @ Order (-1 )
289
+ protected static class AuthenticationManagerCustomizer extends
290
+ GlobalAuthenticationConfigurerAdapter {
291
+
292
+ @ Override
293
+ public void init (AuthenticationManagerBuilder auth ) throws Exception {
294
+ auth .inMemoryAuthentication ().withUser ("foo" ).password ("bar" ).roles ("USER" );
295
+ }
296
+
297
+ }
298
+
199
299
}
0 commit comments