|
50 | 50 | import org.springframework.boot.webmvc.autoconfigure.DispatcherServletAutoConfiguration; |
51 | 51 | import org.springframework.boot.webmvc.autoconfigure.WebMvcAutoConfiguration; |
52 | 52 | import org.springframework.context.ApplicationContext; |
| 53 | +import org.springframework.http.HttpHeaders; |
53 | 54 | import org.springframework.http.HttpMethod; |
54 | 55 | import org.springframework.http.MediaType; |
55 | 56 | import org.springframework.mock.web.MockHttpServletRequest; |
|
61 | 62 | import org.springframework.test.web.servlet.assertj.MockMvcTester; |
62 | 63 | import org.springframework.test.web.servlet.setup.MockMvcBuilders; |
63 | 64 | import org.springframework.web.cors.CorsConfiguration; |
| 65 | +import org.springframework.web.cors.UrlBasedCorsConfigurationSource; |
64 | 66 | import org.springframework.web.filter.CompositeFilter; |
65 | 67 |
|
66 | 68 | import static org.assertj.core.api.Assertions.assertThat; |
67 | 69 | import static org.springframework.security.test.web.servlet.request.SecurityMockMvcRequestPostProcessors.csrf; |
68 | 70 | import static org.springframework.security.test.web.servlet.setup.SecurityMockMvcConfigurers.springSecurity; |
| 71 | +import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get; |
69 | 72 | import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post; |
70 | 73 | import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; |
71 | 74 |
|
@@ -212,6 +215,22 @@ void cloudFoundryPathsPermittedWithCsrfBySpringSecurity() { |
212 | 215 | }); |
213 | 216 | } |
214 | 217 |
|
| 218 | + @Test |
| 219 | + void crossOriginRequestToCloudFoundryPathsPermittedBySpringSecurity() { |
| 220 | + UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource(); |
| 221 | + source.registerCorsConfiguration("/**", new CorsConfiguration()); |
| 222 | + this.contextRunner.withBean(TestEndpoint.class, TestEndpoint::new) |
| 223 | + .withBean("corsConfigurationSource", UrlBasedCorsConfigurationSource.class, () -> source) |
| 224 | + .withPropertyValues("VCAP_APPLICATION:---", "vcap.application.application_id:my-app-id") |
| 225 | + .run((context) -> { |
| 226 | + MockMvc mvc = MockMvcBuilders.webAppContextSetup(context).apply(springSecurity()).build(); |
| 227 | + mvc.perform(get(BASE_PATH + "/test").header(HttpHeaders.ORIGIN, "elsewhere.example.com") |
| 228 | + .contentType(MediaType.APPLICATION_JSON)).andExpect(status().isServiceUnavailable()); |
| 229 | + // If CORS fails we'll get a 403, if it works we get service unavailable |
| 230 | + // because of "Cloud controller URL is not available" |
| 231 | + }); |
| 232 | + } |
| 233 | + |
215 | 234 | private SecurityFilterChain getSecurityFilterChain(AssertableWebApplicationContext context) { |
216 | 235 | Filter springSecurityFilterChain = context.getBean(BeanIds.SPRING_SECURITY_FILTER_CHAIN, Filter.class); |
217 | 236 | FilterChainProxy filterChainProxy = getFilterChainProxy(springSecurityFilterChain); |
|
0 commit comments