17
17
package org .springframework .boot .autoconfigure .security .reactive ;
18
18
19
19
import java .time .Duration ;
20
+ import java .util .function .Function ;
20
21
21
22
import org .junit .jupiter .api .Test ;
22
23
28
29
import org .springframework .boot .test .context .FilteredClassLoader ;
29
30
import org .springframework .boot .test .context .runner .ApplicationContextRunner ;
30
31
import org .springframework .boot .test .context .runner .ReactiveWebApplicationContextRunner ;
32
+ import org .springframework .boot .test .context .runner .WebApplicationContextRunner ;
31
33
import org .springframework .context .annotation .Bean ;
32
34
import org .springframework .context .annotation .Configuration ;
33
35
import org .springframework .context .annotation .Import ;
52
54
*
53
55
* @author Madhura Bhave
54
56
* @author HaiTao Zhang
57
+ * @author Lasse Wulff
55
58
*/
56
59
class ReactiveUserDetailsServiceAutoConfigurationTests {
57
60
58
61
private final ReactiveWebApplicationContextRunner contextRunner = new ReactiveWebApplicationContextRunner ()
59
62
.withConfiguration (AutoConfigurations .of (ReactiveUserDetailsServiceAutoConfiguration .class ));
60
63
64
+ @ Test
65
+ void shouldSupplyUserDetailsServiceInReactiveApp () {
66
+ this .contextRunner .withUserConfiguration (TestSecurityConfiguration .class )
67
+ .with (AuthenticationExclude .reactiveApp ())
68
+ .run ((context ) -> assertThat (context ).hasSingleBean (ReactiveUserDetailsService .class ));
69
+ }
70
+
71
+ @ Test
72
+ void shouldNotSupplyUserDetailsServiceInServletApp () {
73
+ new WebApplicationContextRunner ()
74
+ .withConfiguration (AutoConfigurations .of (ReactiveUserDetailsServiceAutoConfiguration .class ))
75
+ .with (AuthenticationExclude .servletApp ())
76
+ .run ((context ) -> assertThat (context ).doesNotHaveBean (ReactiveUserDetailsService .class ));
77
+ }
78
+
79
+ @ Test
80
+ void shouldNotSupplyUserDetailsServiceInNonWebApp () {
81
+ new ApplicationContextRunner ()
82
+ .withConfiguration (AutoConfigurations .of (ReactiveUserDetailsServiceAutoConfiguration .class ))
83
+ .with (AuthenticationExclude .noWebApp ())
84
+ .run ((context ) -> assertThat (context ).doesNotHaveBean (ReactiveUserDetailsService .class ));
85
+ }
86
+
61
87
@ Test
62
88
void configuresADefaultUser () {
63
89
this .contextRunner
@@ -72,7 +98,7 @@ void configuresADefaultUser() {
72
98
73
99
@ Test
74
100
void userDetailsServiceWhenRSocketConfigured () {
75
- new ApplicationContextRunner ()
101
+ this . contextRunner
76
102
.withClassLoader (
77
103
new FilteredClassLoader (ClientRegistrationRepository .class , ReactiveOpaqueTokenIntrospector .class ))
78
104
.withConfiguration (AutoConfigurations .of (ReactiveUserDetailsServiceAutoConfiguration .class ,
@@ -98,7 +124,7 @@ void doesNotConfigureDefaultUserIfUserDetailsServiceAvailable() {
98
124
void doesNotConfigureDefaultUserIfAuthenticationManagerAvailable () {
99
125
this .contextRunner .withUserConfiguration (AuthenticationManagerConfig .class , TestSecurityConfiguration .class )
100
126
.withConfiguration (AutoConfigurations .of (ReactiveSecurityAutoConfiguration .class ))
101
- .run ((context ) -> assertThat (context ).getBean (ReactiveUserDetailsService .class ). isNull ( ));
127
+ .run ((context ) -> assertThat (context ).doesNotHaveBean (ReactiveUserDetailsService .class ));
102
128
}
103
129
104
130
@ Test
@@ -175,6 +201,25 @@ private void testPasswordEncoding(Class<?> configClass, String providedPassword,
175
201
}));
176
202
}
177
203
204
+ private static final class AuthenticationExclude {
205
+
206
+ private static final FilteredClassLoader filteredClassLoader = new FilteredClassLoader (
207
+ ClientRegistrationRepository .class , ReactiveOpaqueTokenIntrospector .class );
208
+
209
+ static Function <WebApplicationContextRunner , WebApplicationContextRunner > servletApp () {
210
+ return (contextRunner ) -> contextRunner .withClassLoader (filteredClassLoader );
211
+ }
212
+
213
+ static Function <ReactiveWebApplicationContextRunner , ReactiveWebApplicationContextRunner > reactiveApp () {
214
+ return (contextRunner ) -> contextRunner .withClassLoader (filteredClassLoader );
215
+ }
216
+
217
+ static Function <ApplicationContextRunner , ApplicationContextRunner > noWebApp () {
218
+ return (contextRunner ) -> contextRunner .withClassLoader (filteredClassLoader );
219
+ }
220
+
221
+ }
222
+
178
223
@ Configuration (proxyBeanMethods = false )
179
224
@ EnableWebFluxSecurity
180
225
@ EnableConfigurationProperties (SecurityProperties .class )
0 commit comments