26
26
import org .springframework .boot .autoconfigure .security .SecurityProperties ;
27
27
import org .springframework .boot .context .properties .EnableConfigurationProperties ;
28
28
import org .springframework .boot .test .context .FilteredClassLoader ;
29
+ import org .springframework .boot .test .context .runner .ApplicationContextRunner ;
30
+ import org .springframework .boot .test .context .runner .ReactiveWebApplicationContextRunner ;
29
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 ;
57
59
* @author Madhura Bhave
58
60
* @author HaiTao Zhang
59
61
* @author Lasse Wulff
62
+ * @author Moritz Halbritter
60
63
*/
61
64
@ ExtendWith (OutputCaptureExtension .class )
62
65
class UserDetailsServiceAutoConfigurationTests {
@@ -65,9 +68,31 @@ class UserDetailsServiceAutoConfigurationTests {
65
68
.withUserConfiguration (TestSecurityConfiguration .class )
66
69
.withConfiguration (AutoConfigurations .of (UserDetailsServiceAutoConfiguration .class ));
67
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
+
68
93
@ Test
69
94
void testDefaultUsernamePassword (CapturedOutput output ) {
70
- this .contextRunner .with (noOtherFormsOfAuthenticationOnTheClasspath ()).run ((context ) -> {
95
+ this .contextRunner .with (AuthenticationExclude . servletApp ()).run ((context ) -> {
71
96
UserDetailsService manager = context .getBean (UserDetailsService .class );
72
97
assertThat (output ).contains ("Using generated security password:" );
73
98
assertThat (manager .loadUserByUsername ("user" )).isNotNull ();
@@ -129,7 +154,7 @@ void defaultUserNotCreatedIfResourceServerWithJWTIsUsed() {
129
154
130
155
@ Test
131
156
void userDetailsServiceWhenPasswordEncoderAbsentAndDefaultPassword () {
132
- this .contextRunner .with (noOtherFormsOfAuthenticationOnTheClasspath ())
157
+ this .contextRunner .with (AuthenticationExclude . servletApp ())
133
158
.withUserConfiguration (TestSecurityConfiguration .class )
134
159
.run (((context ) -> {
135
160
InMemoryUserDetailsManager userDetailsService = context .getBean (InMemoryUserDetailsManager .class );
@@ -193,14 +218,8 @@ void userDetailsServiceWhenRelyingPartyRegistrationRepositoryPresentAndPasswordC
193
218
.run (((context ) -> assertThat (context ).hasSingleBean (InMemoryUserDetailsManager .class )));
194
219
}
195
220
196
- private Function <WebApplicationContextRunner , WebApplicationContextRunner > noOtherFormsOfAuthenticationOnTheClasspath () {
197
- return (contextRunner ) -> contextRunner
198
- .withClassLoader (new FilteredClassLoader (ClientRegistrationRepository .class , OpaqueTokenIntrospector .class ,
199
- RelyingPartyRegistrationRepository .class ));
200
- }
201
-
202
221
private void testPasswordEncoding (Class <?> configClass , String providedPassword , String expectedPassword ) {
203
- this .contextRunner .with (noOtherFormsOfAuthenticationOnTheClasspath ())
222
+ this .contextRunner .with (AuthenticationExclude . servletApp ())
204
223
.withClassLoader (new FilteredClassLoader (ClientRegistrationRepository .class , OpaqueTokenIntrospector .class ,
205
224
RelyingPartyRegistrationRepository .class ))
206
225
.withUserConfiguration (configClass )
@@ -212,6 +231,26 @@ private void testPasswordEncoding(Class<?> configClass, String providedPassword,
212
231
}));
213
232
}
214
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
+
215
254
@ Configuration (proxyBeanMethods = false )
216
255
static class TestAuthenticationManagerConfiguration {
217
256
0 commit comments