1
1
/*
2
- * Copyright 2016-2021 the original author or authors.
2
+ * Copyright 2016-2022 the original author or authors.
3
3
*
4
4
* Licensed under the Apache License, Version 2.0 (the "License");
5
5
* you may not use this file except in compliance with the License.
63
63
import org .springframework .security .access .AccessDecisionManager ;
64
64
import org .springframework .security .access .vote .AffirmativeBased ;
65
65
import org .springframework .security .access .vote .RoleVoter ;
66
+ import org .springframework .security .config .annotation .authentication .builders .AuthenticationManagerBuilder ;
66
67
import org .springframework .security .config .annotation .web .builders .HttpSecurity ;
67
68
import org .springframework .security .config .annotation .web .configuration .EnableWebSecurity ;
68
- import org .springframework .security .config .annotation .web .configuration .WebSecurityConfigurerAdapter ;
69
69
import org .springframework .security .core .userdetails .User ;
70
70
import org .springframework .security .core .userdetails .UserDetailsService ;
71
71
import org .springframework .security .crypto .factory .PasswordEncoderFactories ;
72
72
import org .springframework .security .provisioning .InMemoryUserDetailsManager ;
73
+ import org .springframework .security .web .SecurityFilterChain ;
73
74
import org .springframework .test .annotation .DirtiesContext ;
74
75
import org .springframework .test .context .junit .jupiter .web .SpringJUnitWebConfig ;
75
76
import org .springframework .test .web .client .MockMvcClientHttpRequestFactory ;
@@ -124,15 +125,15 @@ public void testHttpProxyFlow() throws Exception {
124
125
this .serviceInternalGatewayHandler .setRequestFactory (mockRequestFactory );
125
126
126
127
this .mockMvc .perform (
127
- get ("/service" )
128
- .with (httpBasic ("admin" , "admin" ))
129
- .param ("name" , "foo" ))
128
+ get ("/service" )
129
+ .with (httpBasic ("admin" , "admin" ))
130
+ .param ("name" , "foo" ))
130
131
.andExpect (content ().string ("FOO" ));
131
132
132
133
this .mockMvc .perform (
133
- get ("/service" )
134
- .with (httpBasic ("user" , "user" ))
135
- .param ("name" , "name" ))
134
+ get ("/service" )
135
+ .with (httpBasic ("user" , "user" ))
136
+ .param ("name" , "name" ))
136
137
.andExpect (status ().isForbidden ())
137
138
.andExpect (content ().string ("Error" ));
138
139
}
@@ -141,26 +142,26 @@ public void testHttpProxyFlow() throws Exception {
141
142
public void testDynamicHttpEndpoint () throws Exception {
142
143
IntegrationFlow flow =
143
144
IntegrationFlows .from (Http .inboundGateway ("/dynamic" )
144
- .requestMapping (r -> r .params ("name" ))
145
- .payloadExpression ("#requestParams.name[0]" ))
145
+ .requestMapping (r -> r .params ("name" ))
146
+ .payloadExpression ("#requestParams.name[0]" ))
146
147
.<String , String >transform (String ::toLowerCase )
147
148
.get ();
148
149
149
150
IntegrationFlowContext .IntegrationFlowRegistration flowRegistration =
150
151
this .integrationFlowContext .registration (flow ).register ();
151
152
152
153
this .mockMvc .perform (
153
- get ("/dynamic" )
154
- .with (httpBasic ("user" , "user" ))
155
- .param ("name" , "BAR" ))
154
+ get ("/dynamic" )
155
+ .with (httpBasic ("user" , "user" ))
156
+ .param ("name" , "BAR" ))
156
157
.andExpect (content ().string ("bar" ));
157
158
158
159
flowRegistration .destroy ();
159
160
160
161
this .mockMvc .perform (
161
- get ("/dynamic" )
162
- .with (httpBasic ("user" , "user" ))
163
- .param ("name" , "BAZ" ))
162
+ get ("/dynamic" )
163
+ .with (httpBasic ("user" , "user" ))
164
+ .param ("name" , "BAZ" ))
164
165
.andExpect (status ().isNotFound ());
165
166
}
166
167
@@ -176,9 +177,9 @@ public void testMultiPartFiles() throws Exception {
176
177
MockPart mockPart2 = new MockPart ("a1" , "file2" , "DEF" .getBytes (StandardCharsets .UTF_8 ));
177
178
mockPart2 .getHeaders ().setContentType (MediaType .TEXT_PLAIN );
178
179
this .mockMvc .perform (
179
- multipart ("/multiPartFiles" )
180
- .part (mockPart1 , mockPart2 )
181
- .with (httpBasic ("user" , "user" )))
180
+ multipart ("/multiPartFiles" )
181
+ .part (mockPart1 , mockPart2 )
182
+ .with (httpBasic ("user" , "user" )))
182
183
.andExpect (status ().isOk ());
183
184
184
185
Message <?> result = this .multiPartFilesChannel .receive (10_000 );
@@ -214,23 +215,23 @@ public void testMultiPartFiles() throws Exception {
214
215
public void testValidation () throws Exception {
215
216
IntegrationFlow flow =
216
217
IntegrationFlows .from (
217
- Http .inboundChannelAdapter ("/validation" )
218
- .requestMapping ((mapping ) -> mapping
219
- .methods (HttpMethod .POST )
220
- .consumes (MediaType .APPLICATION_JSON_VALUE ))
221
- .requestPayloadType (TestModel .class )
222
- .validator (this .validator ))
218
+ Http .inboundChannelAdapter ("/validation" )
219
+ .requestMapping ((mapping ) -> mapping
220
+ .methods (HttpMethod .POST )
221
+ .consumes (MediaType .APPLICATION_JSON_VALUE ))
222
+ .requestPayloadType (TestModel .class )
223
+ .validator (this .validator ))
223
224
.bridge ()
224
225
.get ();
225
226
226
227
IntegrationFlowContext .IntegrationFlowRegistration flowRegistration =
227
228
this .integrationFlowContext .registration (flow ).register ();
228
229
229
230
this .mockMvc .perform (
230
- post ("/validation" )
231
- .with (httpBasic ("user" , "user" ))
232
- .contentType (MediaType .APPLICATION_JSON )
233
- .content ("{\" name\" : \" \" }" ))
231
+ post ("/validation" )
232
+ .with (httpBasic ("user" , "user" ))
233
+ .contentType (MediaType .APPLICATION_JSON )
234
+ .content ("{\" name\" : \" \" }" ))
234
235
.andExpect (status ().isBadRequest ())
235
236
.andExpect (status ().reason ("Validation failure" ));
236
237
@@ -241,21 +242,21 @@ public void testValidation() throws Exception {
241
242
public void testBadRequest () throws Exception {
242
243
IntegrationFlow flow =
243
244
IntegrationFlows .from (
244
- Http .inboundGateway ("/badRequest" )
245
- .errorChannel ((message , timeout ) -> {
246
- throw new ResponseStatusException (HttpStatus .BAD_REQUEST ,
247
- "Not valid request param" , ((ErrorMessage ) message ).getPayload ());
248
- })
249
- .payloadExpression ("#requestParams.p1" ))
245
+ Http .inboundGateway ("/badRequest" )
246
+ .errorChannel ((message , timeout ) -> {
247
+ throw new ResponseStatusException (HttpStatus .BAD_REQUEST ,
248
+ "Not valid request param" , ((ErrorMessage ) message ).getPayload ());
249
+ })
250
+ .payloadExpression ("#requestParams.p1" ))
250
251
.get ();
251
252
252
253
IntegrationFlowContext .IntegrationFlowRegistration flowRegistration =
253
254
this .integrationFlowContext .registration (flow ).register ();
254
255
255
256
this .mockMvc .perform (
256
- get ("/badRequest" )
257
- .with (httpBasic ("user" , "user" ))
258
- .param ("p2" , "P2" ))
257
+ get ("/badRequest" )
258
+ .with (httpBasic ("user" , "user" ))
259
+ .param ("p2" , "P2" ))
259
260
.andExpect (status ().isBadRequest ())
260
261
.andExpect (status ().reason ("Not valid request param" ));
261
262
@@ -266,16 +267,16 @@ public void testBadRequest() throws Exception {
266
267
public void testErrorChannelFlow () throws Exception {
267
268
IntegrationFlow flow =
268
269
IntegrationFlows .from (
269
- Http .inboundGateway ("/errorFlow" )
270
- .errorChannel (new FixedSubscriberChannel (
271
- new AbstractReplyProducingMessageHandler () {
270
+ Http .inboundGateway ("/errorFlow" )
271
+ .errorChannel (new FixedSubscriberChannel (
272
+ new AbstractReplyProducingMessageHandler () {
272
273
273
- @ Override
274
- protected Object handleRequestMessage (Message <?> requestMessage ) {
275
- return "Error Response" ;
276
- }
274
+ @ Override
275
+ protected Object handleRequestMessage (Message <?> requestMessage ) {
276
+ return "Error Response" ;
277
+ }
277
278
278
- })))
279
+ })))
279
280
.transform ((payload ) -> {
280
281
throw new RuntimeException ("Error!" );
281
282
})
@@ -285,8 +286,8 @@ protected Object handleRequestMessage(Message<?> requestMessage) {
285
286
this .integrationFlowContext .registration (flow ).register ();
286
287
287
288
this .mockMvc .perform (
288
- get ("/errorFlow" )
289
- .with (httpBasic ("user" , "user" )))
289
+ get ("/errorFlow" )
290
+ .with (httpBasic ("user" , "user" )))
290
291
.andExpect (status ().isOk ())
291
292
.andExpect (content ().string ("Error Response" ));
292
293
@@ -296,9 +297,8 @@ protected Object handleRequestMessage(Message<?> requestMessage) {
296
297
@ Configuration
297
298
@ EnableWebSecurity
298
299
@ EnableIntegration
299
- public static class ContextConfiguration extends WebSecurityConfigurerAdapter {
300
+ public static class ContextConfiguration {
300
301
301
- @ Override
302
302
@ Bean
303
303
public UserDetailsService userDetailsService () {
304
304
InMemoryUserDetailsManager manager = new InMemoryUserDetailsManager ();
@@ -320,16 +320,18 @@ public UserDetailsService userDetailsService() {
320
320
return manager ;
321
321
}
322
322
323
- @ Override
324
- protected void configure (HttpSecurity http ) throws Exception {
325
- http .authorizeRequests ()
323
+ @ Bean
324
+ public SecurityFilterChain filterChain (HttpSecurity http ) throws Exception {
325
+ return http .
326
+ authorizeRequests ()
326
327
.antMatchers ("/service/internal/**" ).hasRole ("ADMIN" )
327
328
.anyRequest ().permitAll ()
328
329
.and ()
329
330
.httpBasic ()
330
331
.and ()
331
332
.csrf ().disable ()
332
- .anonymous ().disable ();
333
+ .anonymous ().disable ()
334
+ .build ();
333
335
}
334
336
335
337
@ Bean
@@ -397,10 +399,11 @@ public AccessDecisionManager accessDecisionManager() {
397
399
}
398
400
399
401
@ Bean
400
- public ChannelSecurityInterceptor channelSecurityInterceptor (AccessDecisionManager accessDecisionManager )
401
- throws Exception {
402
+ public ChannelSecurityInterceptor channelSecurityInterceptor (AccessDecisionManager accessDecisionManager ,
403
+ AuthenticationManagerBuilder authenticationManagerBuilder ) {
404
+
402
405
ChannelSecurityInterceptor channelSecurityInterceptor = new ChannelSecurityInterceptor ();
403
- channelSecurityInterceptor .setAuthenticationManager (authenticationManager ());
406
+ channelSecurityInterceptor .setAuthenticationManager (authenticationManagerBuilder . getOrBuild ());
404
407
channelSecurityInterceptor .setAccessDecisionManager (accessDecisionManager );
405
408
return channelSecurityInterceptor ;
406
409
}
0 commit comments