17
17
package org .springframework .integration .webflux .dsl ;
18
18
19
19
import static org .hamcrest .Matchers .instanceOf ;
20
+ import static org .hamcrest .Matchers .is ;
20
21
import static org .junit .Assert .assertNotNull ;
21
22
import static org .junit .Assert .assertThat ;
22
23
import static org .springframework .security .test .web .servlet .request .SecurityMockMvcRequestPostProcessors .httpBasic ;
23
- import static org .springframework .security .test .web .servlet .setup .SecurityMockMvcConfigurers .springSecurity ;
24
24
import static org .springframework .test .web .servlet .request .MockMvcRequestBuilders .get ;
25
25
import static org .springframework .test .web .servlet .result .MockMvcResultMatchers .content ;
26
+ import static org .springframework .web .reactive .function .client .ExchangeFilterFunctions .Credentials .basicAuthenticationCredentials ;
27
+ import static org .springframework .web .reactive .function .client .ExchangeFilterFunctions .basicAuthentication ;
26
28
29
+ import java .security .Principal ;
27
30
import java .util .Collections ;
28
31
29
32
import javax .annotation .Resource ;
48
51
import org .springframework .integration .config .EnableIntegration ;
49
52
import org .springframework .integration .dsl .IntegrationFlow ;
50
53
import org .springframework .integration .dsl .IntegrationFlows ;
54
+ import org .springframework .integration .http .HttpHeaders ;
51
55
import org .springframework .integration .http .dsl .Http ;
52
56
import org .springframework .integration .support .MessageBuilder ;
53
57
import org .springframework .integration .webflux .outbound .WebFluxRequestExecutingMessageHandler ;
60
64
import org .springframework .security .config .annotation .web .builders .HttpSecurity ;
61
65
import org .springframework .security .config .annotation .web .configuration .EnableWebSecurity ;
62
66
import org .springframework .security .config .annotation .web .configuration .WebSecurityConfigurerAdapter ;
67
+ import org .springframework .security .config .annotation .web .reactive .EnableWebFluxSecurity ;
68
+ import org .springframework .security .config .web .server .ServerHttpSecurity ;
69
+ import org .springframework .security .core .userdetails .MapReactiveUserDetailsService ;
70
+ import org .springframework .security .core .userdetails .ReactiveUserDetailsService ;
63
71
import org .springframework .security .core .userdetails .User ;
72
+ import org .springframework .security .core .userdetails .UserDetails ;
64
73
import org .springframework .security .core .userdetails .UserDetailsService ;
65
74
import org .springframework .security .crypto .factory .PasswordEncoderFactories ;
66
75
import org .springframework .security .provisioning .InMemoryUserDetailsManager ;
76
+ import org .springframework .security .test .web .reactive .server .SecurityMockServerConfigurers ;
77
+ import org .springframework .security .test .web .servlet .setup .SecurityMockMvcConfigurers ;
78
+ import org .springframework .security .web .server .SecurityWebFilterChain ;
67
79
import org .springframework .test .annotation .DirtiesContext ;
68
80
import org .springframework .test .context .junit4 .SpringRunner ;
69
81
import org .springframework .test .context .web .WebAppConfiguration ;
@@ -116,11 +128,14 @@ public class WebFluxDslTests {
116
128
public void setup () {
117
129
this .mockMvc =
118
130
MockMvcBuilders .webAppContextSetup (this .wac )
119
- .apply (springSecurity ())
131
+ .apply (SecurityMockMvcConfigurers . springSecurity ())
120
132
.build ();
121
133
122
134
this .webTestClient =
123
135
WebTestClient .bindToApplicationContext (this .wac )
136
+ .apply (SecurityMockServerConfigurers .springSecurity ())
137
+ .configureClient ()
138
+ .filter (basicAuthentication ())
124
139
.build ();
125
140
}
126
141
@@ -198,6 +213,7 @@ public void testHttpReactiveProxyFlow() throws Exception {
198
213
@ SuppressWarnings ("unchecked" )
199
214
public void testHttpReactivePost () {
200
215
this .webTestClient .post ().uri ("/reactivePost" )
216
+ .attributes (basicAuthenticationCredentials ("guest" , "guest" ))
201
217
.body (Mono .just ("foo\n bar\n baz" ), String .class )
202
218
.exchange ()
203
219
.expectStatus ().isAccepted ();
@@ -206,6 +222,8 @@ public void testHttpReactivePost() {
206
222
assertNotNull (store );
207
223
assertThat (store .getPayload (), instanceOf (Flux .class ));
208
224
225
+ assertThat (store .getHeaders ().get (HttpHeaders .USER_PRINCIPAL , Principal .class ).getName (), is ("guest" ));
226
+
209
227
StepVerifier
210
228
.create ((Publisher <String >) store .getPayload ())
211
229
.expectNext ("foo" , "bar" , "baz" )
@@ -217,6 +235,7 @@ public void testHttpReactivePost() {
217
235
public void testSse () {
218
236
Flux <String > responseBody =
219
237
this .webTestClient .get ().uri ("/sse" )
238
+ .attributes (basicAuthenticationCredentials ("guest" , "guest" ))
220
239
.exchange ()
221
240
.returnResult (String .class )
222
241
.getResponseBody ();
@@ -230,22 +249,40 @@ public void testSse() {
230
249
@ Configuration
231
250
@ EnableWebFlux
232
251
@ EnableWebSecurity
252
+ @ EnableWebFluxSecurity
233
253
@ EnableIntegration
234
254
public static class ContextConfiguration extends WebSecurityConfigurerAdapter {
235
255
256
+ @ Bean
257
+ public UserDetails userDetails () {
258
+ return User .withUsername ("guest" )
259
+ .passwordEncoder (PasswordEncoderFactories .createDelegatingPasswordEncoder ()::encode )
260
+ .password ("guest" )
261
+ .roles ("ADMIN" )
262
+ .build ();
263
+ }
264
+
236
265
@ Override
237
266
@ Bean
238
267
public UserDetailsService userDetailsService () {
239
- InMemoryUserDetailsManager manager = new InMemoryUserDetailsManager ();
268
+ return new InMemoryUserDetailsManager (userDetails ());
269
+ }
240
270
241
- manager .createUser (
242
- User .withUsername ("guest" )
243
- .passwordEncoder (PasswordEncoderFactories .createDelegatingPasswordEncoder ()::encode )
244
- .password ("guest" )
245
- .roles ("ADMIN" )
246
- .build ());
271
+ @ Bean
272
+ public ReactiveUserDetailsService reactiveUserDetailsService () {
273
+ return new MapReactiveUserDetailsService (userDetails ());
274
+ }
247
275
248
- return manager ;
276
+
277
+ @ Bean
278
+ public SecurityWebFilterChain reactiveSpringSecurityFilterChain (ServerHttpSecurity http ) {
279
+ return http .authorizeExchange ()
280
+ .anyExchange ().hasRole ("ADMIN" )
281
+ .and ()
282
+ .httpBasic ()
283
+ .and ()
284
+ .csrf ().disable ()
285
+ .build ();
249
286
}
250
287
251
288
@ Override
0 commit comments