11/*
2- * Copyright 2016-2021 the original author or authors.
2+ * Copyright 2016-2022 the original author or authors.
33 *
44 * Licensed under the Apache License, Version 2.0 (the "License");
55 * you may not use this file except in compliance with the License.
6363import org .springframework .security .access .AccessDecisionManager ;
6464import org .springframework .security .access .vote .AffirmativeBased ;
6565import org .springframework .security .access .vote .RoleVoter ;
66+ import org .springframework .security .config .annotation .authentication .builders .AuthenticationManagerBuilder ;
6667import org .springframework .security .config .annotation .web .builders .HttpSecurity ;
6768import org .springframework .security .config .annotation .web .configuration .EnableWebSecurity ;
68- import org .springframework .security .config .annotation .web .configuration .WebSecurityConfigurerAdapter ;
6969import org .springframework .security .core .userdetails .User ;
7070import org .springframework .security .core .userdetails .UserDetailsService ;
7171import org .springframework .security .crypto .factory .PasswordEncoderFactories ;
7272import org .springframework .security .provisioning .InMemoryUserDetailsManager ;
73+ import org .springframework .security .web .SecurityFilterChain ;
7374import org .springframework .test .annotation .DirtiesContext ;
7475import org .springframework .test .context .junit .jupiter .web .SpringJUnitWebConfig ;
7576import org .springframework .test .web .client .MockMvcClientHttpRequestFactory ;
@@ -124,15 +125,15 @@ public void testHttpProxyFlow() throws Exception {
124125 this .serviceInternalGatewayHandler .setRequestFactory (mockRequestFactory );
125126
126127 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" ))
130131 .andExpect (content ().string ("FOO" ));
131132
132133 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" ))
136137 .andExpect (status ().isForbidden ())
137138 .andExpect (content ().string ("Error" ));
138139 }
@@ -141,26 +142,26 @@ public void testHttpProxyFlow() throws Exception {
141142 public void testDynamicHttpEndpoint () throws Exception {
142143 IntegrationFlow flow =
143144 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]" ))
146147 .<String , String >transform (String ::toLowerCase )
147148 .get ();
148149
149150 IntegrationFlowContext .IntegrationFlowRegistration flowRegistration =
150151 this .integrationFlowContext .registration (flow ).register ();
151152
152153 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" ))
156157 .andExpect (content ().string ("bar" ));
157158
158159 flowRegistration .destroy ();
159160
160161 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" ))
164165 .andExpect (status ().isNotFound ());
165166 }
166167
@@ -176,9 +177,9 @@ public void testMultiPartFiles() throws Exception {
176177 MockPart mockPart2 = new MockPart ("a1" , "file2" , "DEF" .getBytes (StandardCharsets .UTF_8 ));
177178 mockPart2 .getHeaders ().setContentType (MediaType .TEXT_PLAIN );
178179 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" )))
182183 .andExpect (status ().isOk ());
183184
184185 Message <?> result = this .multiPartFilesChannel .receive (10_000 );
@@ -214,23 +215,23 @@ public void testMultiPartFiles() throws Exception {
214215 public void testValidation () throws Exception {
215216 IntegrationFlow flow =
216217 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 ))
223224 .bridge ()
224225 .get ();
225226
226227 IntegrationFlowContext .IntegrationFlowRegistration flowRegistration =
227228 this .integrationFlowContext .registration (flow ).register ();
228229
229230 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\" : \" \" }" ))
234235 .andExpect (status ().isBadRequest ())
235236 .andExpect (status ().reason ("Validation failure" ));
236237
@@ -241,21 +242,21 @@ public void testValidation() throws Exception {
241242 public void testBadRequest () throws Exception {
242243 IntegrationFlow flow =
243244 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" ))
250251 .get ();
251252
252253 IntegrationFlowContext .IntegrationFlowRegistration flowRegistration =
253254 this .integrationFlowContext .registration (flow ).register ();
254255
255256 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" ))
259260 .andExpect (status ().isBadRequest ())
260261 .andExpect (status ().reason ("Not valid request param" ));
261262
@@ -266,16 +267,16 @@ public void testBadRequest() throws Exception {
266267 public void testErrorChannelFlow () throws Exception {
267268 IntegrationFlow flow =
268269 IntegrationFlows .from (
269- Http .inboundGateway ("/errorFlow" )
270- .errorChannel (new FixedSubscriberChannel (
271- new AbstractReplyProducingMessageHandler () {
270+ Http .inboundGateway ("/errorFlow" )
271+ .errorChannel (new FixedSubscriberChannel (
272+ new AbstractReplyProducingMessageHandler () {
272273
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+ }
277278
278- })))
279+ })))
279280 .transform ((payload ) -> {
280281 throw new RuntimeException ("Error!" );
281282 })
@@ -285,8 +286,8 @@ protected Object handleRequestMessage(Message<?> requestMessage) {
285286 this .integrationFlowContext .registration (flow ).register ();
286287
287288 this .mockMvc .perform (
288- get ("/errorFlow" )
289- .with (httpBasic ("user" , "user" )))
289+ get ("/errorFlow" )
290+ .with (httpBasic ("user" , "user" )))
290291 .andExpect (status ().isOk ())
291292 .andExpect (content ().string ("Error Response" ));
292293
@@ -296,9 +297,8 @@ protected Object handleRequestMessage(Message<?> requestMessage) {
296297 @ Configuration
297298 @ EnableWebSecurity
298299 @ EnableIntegration
299- public static class ContextConfiguration extends WebSecurityConfigurerAdapter {
300+ public static class ContextConfiguration {
300301
301- @ Override
302302 @ Bean
303303 public UserDetailsService userDetailsService () {
304304 InMemoryUserDetailsManager manager = new InMemoryUserDetailsManager ();
@@ -320,16 +320,18 @@ public UserDetailsService userDetailsService() {
320320 return manager ;
321321 }
322322
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 ()
326327 .antMatchers ("/service/internal/**" ).hasRole ("ADMIN" )
327328 .anyRequest ().permitAll ()
328329 .and ()
329330 .httpBasic ()
330331 .and ()
331332 .csrf ().disable ()
332- .anonymous ().disable ();
333+ .anonymous ().disable ()
334+ .build ();
333335 }
334336
335337 @ Bean
@@ -397,10 +399,11 @@ public AccessDecisionManager accessDecisionManager() {
397399 }
398400
399401 @ Bean
400- public ChannelSecurityInterceptor channelSecurityInterceptor (AccessDecisionManager accessDecisionManager )
401- throws Exception {
402+ public ChannelSecurityInterceptor channelSecurityInterceptor (AccessDecisionManager accessDecisionManager ,
403+ AuthenticationManagerBuilder authenticationManagerBuilder ) {
404+
402405 ChannelSecurityInterceptor channelSecurityInterceptor = new ChannelSecurityInterceptor ();
403- channelSecurityInterceptor .setAuthenticationManager (authenticationManager ());
406+ channelSecurityInterceptor .setAuthenticationManager (authenticationManagerBuilder . getOrBuild ());
404407 channelSecurityInterceptor .setAccessDecisionManager (accessDecisionManager );
405408 return channelSecurityInterceptor ;
406409 }
0 commit comments