31
31
import org .springframework .boot .test .EnvironmentTestUtils ;
32
32
import org .springframework .context .ApplicationEvent ;
33
33
import org .springframework .context .ApplicationListener ;
34
+ import org .springframework .context .annotation .AnnotationConfigApplicationContext ;
34
35
import org .springframework .context .annotation .Bean ;
35
36
import org .springframework .context .annotation .Configuration ;
36
37
import org .springframework .core .annotation .Order ;
45
46
import org .springframework .security .config .annotation .authentication .builders .AuthenticationManagerBuilder ;
46
47
import org .springframework .security .config .annotation .authentication .configurers .GlobalAuthenticationConfigurerAdapter ;
47
48
import org .springframework .security .config .annotation .web .builders .HttpSecurity ;
49
+ import org .springframework .security .config .annotation .web .configuration .EnableWebSecurity ;
48
50
import org .springframework .security .config .annotation .web .configuration .WebSecurityConfigurerAdapter ;
49
51
import org .springframework .security .core .Authentication ;
50
52
import org .springframework .security .core .AuthenticationException ;
55
57
import org .springframework .web .context .support .AnnotationConfigWebApplicationContext ;
56
58
57
59
import static org .junit .Assert .assertEquals ;
60
+ import static org .junit .Assert .assertFalse ;
58
61
import static org .junit .Assert .assertNotNull ;
59
62
import static org .junit .Assert .assertTrue ;
60
63
import static org .junit .Assert .fail ;
63
66
* Tests for {@link SecurityAutoConfiguration}.
64
67
*
65
68
* @author Dave Syer
69
+ * @author Rob Winch
66
70
*/
67
71
public class SecurityAutoConfigurationTests {
68
72
@@ -90,11 +94,43 @@ public void testWebConfiguration() throws Exception {
90
94
assertEquals (5 , filterChains .size ());
91
95
}
92
96
97
+ @ Test
98
+ public void testDefaultFilterOrderWithSecurityAdapter () throws Exception {
99
+ this .context = new AnnotationConfigWebApplicationContext ();
100
+ this .context .setServletContext (new MockServletContext ());
101
+ this .context .register (WebSecurity .class , SecurityAutoConfiguration .class ,
102
+ SecurityFilterAutoConfiguration .class ,
103
+ ServerPropertiesAutoConfiguration .class ,
104
+ PropertyPlaceholderAutoConfiguration .class );
105
+ this .context .refresh ();
106
+ assertEquals (
107
+ 0 ,
108
+ this .context .getBean ("securityFilterChainRegistration" ,
109
+ FilterRegistrationBean .class ).getOrder ());
110
+ }
111
+
112
+ @ Test
113
+ public void testFilterIsNotRegisteredInNonWeb () throws Exception {
114
+ AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext ();
115
+ context .register (SecurityAutoConfiguration .class ,
116
+ SecurityFilterAutoConfiguration .class ,
117
+ ServerPropertiesAutoConfiguration .class ,
118
+ PropertyPlaceholderAutoConfiguration .class );
119
+ try {
120
+ context .refresh ();
121
+ assertFalse (context .containsBean ("securityFilterChainRegistration" ));
122
+ }
123
+ finally {
124
+ context .close ();
125
+ }
126
+ }
127
+
93
128
@ Test
94
129
public void testDefaultFilterOrder () throws Exception {
95
130
this .context = new AnnotationConfigWebApplicationContext ();
96
131
this .context .setServletContext (new MockServletContext ());
97
132
this .context .register (SecurityAutoConfiguration .class ,
133
+ SecurityFilterAutoConfiguration .class ,
98
134
ServerPropertiesAutoConfiguration .class ,
99
135
PropertyPlaceholderAutoConfiguration .class );
100
136
this .context .refresh ();
@@ -110,6 +146,7 @@ public void testCustomFilterOrder() throws Exception {
110
146
EnvironmentTestUtils .addEnvironment (this .context , "security.filter-order:12345" );
111
147
this .context .setServletContext (new MockServletContext ());
112
148
this .context .register (SecurityAutoConfiguration .class ,
149
+ SecurityFilterAutoConfiguration .class ,
113
150
ServerPropertiesAutoConfiguration .class ,
114
151
PropertyPlaceholderAutoConfiguration .class );
115
152
this .context .refresh ();
@@ -411,4 +448,10 @@ public UserDetailsService getUserDetails() {
411
448
412
449
}
413
450
451
+ @ Configuration
452
+ @ EnableWebSecurity
453
+ static class WebSecurity extends WebSecurityConfigurerAdapter {
454
+
455
+ }
456
+
414
457
}
0 commit comments