27
27
import org .springframework .boot .context .properties .EnableConfigurationProperties ;
28
28
import org .springframework .boot .test .context .FilteredClassLoader ;
29
29
import org .springframework .boot .test .context .runner .ApplicationContextRunner ;
30
+ import org .springframework .boot .test .context .runner .ReactiveWebApplicationContextRunner ;
31
+ import org .springframework .boot .test .context .runner .WebApplicationContextRunner ;
30
32
import org .springframework .boot .test .system .CapturedOutput ;
31
33
import org .springframework .boot .test .system .OutputCaptureExtension ;
32
34
import org .springframework .context .annotation .Bean ;
56
58
*
57
59
* @author Madhura Bhave
58
60
* @author HaiTao Zhang
61
+ * @author Lasse Wulff
62
+ * @author Moritz Halbritter
59
63
*/
60
64
@ ExtendWith (OutputCaptureExtension .class )
61
65
class UserDetailsServiceAutoConfigurationTests {
62
66
63
- private final ApplicationContextRunner contextRunner = new ApplicationContextRunner ()
67
+ private final WebApplicationContextRunner contextRunner = new WebApplicationContextRunner ()
64
68
.withUserConfiguration (TestSecurityConfiguration .class )
65
69
.withConfiguration (AutoConfigurations .of (UserDetailsServiceAutoConfiguration .class ));
66
70
71
+ @ Test
72
+ void shouldSupplyUserDetailsServiceInServletApp () {
73
+ this .contextRunner .with (AuthenticationExclude .servletApp ())
74
+ .run ((context ) -> assertThat (context ).hasSingleBean (UserDetailsService .class ));
75
+ }
76
+
77
+ @ Test
78
+ void shouldNotSupplyUserDetailsServiceInReactiveApp () {
79
+ new ReactiveWebApplicationContextRunner ().withUserConfiguration (TestSecurityConfiguration .class )
80
+ .withConfiguration (AutoConfigurations .of (UserDetailsServiceAutoConfiguration .class ))
81
+ .with (AuthenticationExclude .reactiveApp ())
82
+ .run ((context ) -> assertThat (context ).doesNotHaveBean (UserDetailsService .class ));
83
+ }
84
+
85
+ @ Test
86
+ void shouldNotSupplyUserDetailsServiceInNonWebApp () {
87
+ new ApplicationContextRunner ().withUserConfiguration (TestSecurityConfiguration .class )
88
+ .withConfiguration (AutoConfigurations .of (UserDetailsServiceAutoConfiguration .class ))
89
+ .with (AuthenticationExclude .noWebApp ())
90
+ .run ((context ) -> assertThat (context ).doesNotHaveBean (UserDetailsService .class ));
91
+ }
92
+
67
93
@ Test
68
94
void testDefaultUsernamePassword (CapturedOutput output ) {
69
- this .contextRunner .with (noOtherFormsOfAuthenticationOnTheClasspath ()).run ((context ) -> {
95
+ this .contextRunner .with (AuthenticationExclude . servletApp ()).run ((context ) -> {
70
96
UserDetailsService manager = context .getBean (UserDetailsService .class );
71
97
assertThat (output ).contains ("Using generated security password:" );
72
98
assertThat (manager .loadUserByUsername ("user" )).isNotNull ();
@@ -128,7 +154,7 @@ void defaultUserNotCreatedIfResourceServerWithJWTIsUsed() {
128
154
129
155
@ Test
130
156
void userDetailsServiceWhenPasswordEncoderAbsentAndDefaultPassword () {
131
- this .contextRunner .with (noOtherFormsOfAuthenticationOnTheClasspath ())
157
+ this .contextRunner .with (AuthenticationExclude . servletApp ())
132
158
.withUserConfiguration (TestSecurityConfiguration .class )
133
159
.run (((context ) -> {
134
160
InMemoryUserDetailsManager userDetailsService = context .getBean (InMemoryUserDetailsManager .class );
@@ -192,14 +218,8 @@ void userDetailsServiceWhenRelyingPartyRegistrationRepositoryPresentAndPasswordC
192
218
.run (((context ) -> assertThat (context ).hasSingleBean (InMemoryUserDetailsManager .class )));
193
219
}
194
220
195
- private Function <ApplicationContextRunner , ApplicationContextRunner > noOtherFormsOfAuthenticationOnTheClasspath () {
196
- return (contextRunner ) -> contextRunner
197
- .withClassLoader (new FilteredClassLoader (ClientRegistrationRepository .class , OpaqueTokenIntrospector .class ,
198
- RelyingPartyRegistrationRepository .class ));
199
- }
200
-
201
221
private void testPasswordEncoding (Class <?> configClass , String providedPassword , String expectedPassword ) {
202
- this .contextRunner .with (noOtherFormsOfAuthenticationOnTheClasspath ())
222
+ this .contextRunner .with (AuthenticationExclude . servletApp ())
203
223
.withClassLoader (new FilteredClassLoader (ClientRegistrationRepository .class , OpaqueTokenIntrospector .class ,
204
224
RelyingPartyRegistrationRepository .class ))
205
225
.withUserConfiguration (configClass )
@@ -211,6 +231,26 @@ private void testPasswordEncoding(Class<?> configClass, String providedPassword,
211
231
}));
212
232
}
213
233
234
+ private static final class AuthenticationExclude {
235
+
236
+ private static final FilteredClassLoader filteredClassLoader = new FilteredClassLoader (
237
+ ClientRegistrationRepository .class , OpaqueTokenIntrospector .class ,
238
+ RelyingPartyRegistrationRepository .class );
239
+
240
+ static Function <WebApplicationContextRunner , WebApplicationContextRunner > servletApp () {
241
+ return (contextRunner ) -> contextRunner .withClassLoader (filteredClassLoader );
242
+ }
243
+
244
+ static Function <ReactiveWebApplicationContextRunner , ReactiveWebApplicationContextRunner > reactiveApp () {
245
+ return (contextRunner ) -> contextRunner .withClassLoader (filteredClassLoader );
246
+ }
247
+
248
+ static Function <ApplicationContextRunner , ApplicationContextRunner > noWebApp () {
249
+ return (contextRunner ) -> contextRunner .withClassLoader (filteredClassLoader );
250
+ }
251
+
252
+ }
253
+
214
254
@ Configuration (proxyBeanMethods = false )
215
255
static class TestAuthenticationManagerConfiguration {
216
256
0 commit comments