Skip to content

Commit e7d11af

Browse files
committed
Merge branch '4.0.x'
Closes gh-50258
2 parents b744839 + a0a344f commit e7d11af

2 files changed

Lines changed: 22 additions & 1 deletion

File tree

module/spring-boot-cloudfoundry/src/main/java/org/springframework/boot/cloudfoundry/autoconfigure/actuate/endpoint/servlet/CloudFoundryActuatorAutoConfiguration.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,7 @@ private SecurityInterceptor getSecurityInterceptor(RestClient.Builder restClient
148148
? new SecurityService(restClientBuilder, cloudControllerUrl, skipSslValidation) : null;
149149
}
150150

151-
private CorsConfiguration getCorsConfiguration() {
151+
private static CorsConfiguration getCorsConfiguration() {
152152
CorsConfiguration corsConfiguration = new CorsConfiguration();
153153
corsConfiguration.addAllowedOrigin(CorsConfiguration.ALL);
154154
corsConfiguration.setAllowedMethods(Arrays.asList(HttpMethod.GET.name(), HttpMethod.POST.name()));
@@ -173,6 +173,8 @@ static class IgnoredCloudFoundryPathsWebSecurityConfiguration {
173173
SecurityFilterChain cloudFoundrySecurityFilterChain(HttpSecurity http) throws Exception {
174174
RequestMatcher cloudFoundryRequest = getRequestMatcher();
175175
http.csrf((csrf) -> csrf.ignoringRequestMatchers(cloudFoundryRequest));
176+
CorsConfiguration corsConfiguration = getCorsConfiguration();
177+
http.cors((cors) -> cors.configurationSource((request) -> corsConfiguration));
176178
http.securityMatchers((matches) -> matches.requestMatchers(cloudFoundryRequest))
177179
.authorizeHttpRequests((authorize) -> authorize.anyRequest().permitAll());
178180
return http.build();

module/spring-boot-cloudfoundry/src/test/java/org/springframework/boot/cloudfoundry/autoconfigure/actuate/endpoint/servlet/CloudFoundryActuatorAutoConfigurationTests.java

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@
5050
import org.springframework.boot.webmvc.autoconfigure.DispatcherServletAutoConfiguration;
5151
import org.springframework.boot.webmvc.autoconfigure.WebMvcAutoConfiguration;
5252
import org.springframework.context.ApplicationContext;
53+
import org.springframework.http.HttpHeaders;
5354
import org.springframework.http.HttpMethod;
5455
import org.springframework.http.MediaType;
5556
import org.springframework.mock.web.MockHttpServletRequest;
@@ -61,11 +62,13 @@
6162
import org.springframework.test.web.servlet.assertj.MockMvcTester;
6263
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
6364
import org.springframework.web.cors.CorsConfiguration;
65+
import org.springframework.web.cors.UrlBasedCorsConfigurationSource;
6466
import org.springframework.web.filter.CompositeFilter;
6567

6668
import static org.assertj.core.api.Assertions.assertThat;
6769
import static org.springframework.security.test.web.servlet.request.SecurityMockMvcRequestPostProcessors.csrf;
6870
import static org.springframework.security.test.web.servlet.setup.SecurityMockMvcConfigurers.springSecurity;
71+
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
6972
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;
7073
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
7174

@@ -212,6 +215,22 @@ void cloudFoundryPathsPermittedWithCsrfBySpringSecurity() {
212215
});
213216
}
214217

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+
215234
private SecurityFilterChain getSecurityFilterChain(AssertableWebApplicationContext context) {
216235
Filter springSecurityFilterChain = context.getBean(BeanIds.SPRING_SECURITY_FILTER_CHAIN, Filter.class);
217236
FilterChainProxy filterChainProxy = getFilterChainProxy(springSecurityFilterChain);

0 commit comments

Comments
 (0)