Search in sources :

Example 41 with CorsConfiguration

use of org.springframework.web.cors.CorsConfiguration in project spring-framework by spring-projects.

the class CorsUrlHandlerMappingTests method actualRequestWithGlobalCorsConfig.

@Test
public void actualRequestWithGlobalCorsConfig() throws Exception {
    CorsConfiguration mappedConfig = new CorsConfiguration();
    mappedConfig.addAllowedOrigin("*");
    this.handlerMapping.setCorsConfigurations(Collections.singletonMap("/welcome.html", mappedConfig));
    String origin = "http://domain2.com";
    ServerWebExchange exchange = createExchange(HttpMethod.GET, "/welcome.html", origin);
    Object actual = this.handlerMapping.getHandler(exchange).block();
    assertNotNull(actual);
    assertSame(this.welcomeController, actual);
    assertEquals("*", exchange.getResponse().getHeaders().getFirst(HttpHeaders.ACCESS_CONTROL_ALLOW_ORIGIN));
}
Also used : ServerWebExchange(org.springframework.web.server.ServerWebExchange) CorsConfiguration(org.springframework.web.cors.CorsConfiguration) Test(org.junit.Test)

Example 42 with CorsConfiguration

use of org.springframework.web.cors.CorsConfiguration in project spring-boot by spring-projects.

the class CloudFoundryActuatorAutoConfigurationTests method cloudFoundryPlatformActive.

@Test
public void cloudFoundryPlatformActive() throws Exception {
    CloudFoundryEndpointHandlerMapping handlerMapping = getHandlerMapping();
    assertThat(handlerMapping.getPrefix()).isEqualTo("/cloudfoundryapplication");
    CorsConfiguration corsConfiguration = (CorsConfiguration) ReflectionTestUtils.getField(handlerMapping, "corsConfiguration");
    assertThat(corsConfiguration.getAllowedOrigins()).contains("*");
    assertThat(corsConfiguration.getAllowedMethods()).containsAll(Arrays.asList(HttpMethod.GET.name(), HttpMethod.POST.name()));
    assertThat(corsConfiguration.getAllowedHeaders()).containsAll(Arrays.asList("Authorization", "X-Cf-App-Instance", "Content-Type"));
}
Also used : CorsConfiguration(org.springframework.web.cors.CorsConfiguration) Test(org.junit.Test)

Example 43 with CorsConfiguration

use of org.springframework.web.cors.CorsConfiguration in project spring-boot by spring-projects.

the class EndpointWebMvcManagementContextConfiguration method endpointHandlerMapping.

@Bean
@ConditionalOnMissingBean
public EndpointHandlerMapping endpointHandlerMapping() {
    Set<MvcEndpoint> endpoints = mvcEndpoints().getEndpoints();
    CorsConfiguration corsConfiguration = getCorsConfiguration(this.corsProperties);
    EndpointHandlerMapping mapping = new EndpointHandlerMapping(endpoints, corsConfiguration);
    mapping.setPrefix(this.managementServerProperties.getContextPath());
    MvcEndpointSecurityInterceptor securityInterceptor = new MvcEndpointSecurityInterceptor(this.managementServerProperties.getSecurity().isEnabled(), this.managementServerProperties.getSecurity().getRoles());
    mapping.setSecurityInterceptor(securityInterceptor);
    for (EndpointHandlerMappingCustomizer customizer : this.mappingCustomizers) {
        customizer.customize(mapping);
    }
    return mapping;
}
Also used : EnvironmentMvcEndpoint(org.springframework.boot.actuate.endpoint.mvc.EnvironmentMvcEndpoint) HeapdumpMvcEndpoint(org.springframework.boot.actuate.endpoint.mvc.HeapdumpMvcEndpoint) LogFileMvcEndpoint(org.springframework.boot.actuate.endpoint.mvc.LogFileMvcEndpoint) LoggersMvcEndpoint(org.springframework.boot.actuate.endpoint.mvc.LoggersMvcEndpoint) ShutdownMvcEndpoint(org.springframework.boot.actuate.endpoint.mvc.ShutdownMvcEndpoint) AuditEventsMvcEndpoint(org.springframework.boot.actuate.endpoint.mvc.AuditEventsMvcEndpoint) MetricsMvcEndpoint(org.springframework.boot.actuate.endpoint.mvc.MetricsMvcEndpoint) HealthMvcEndpoint(org.springframework.boot.actuate.endpoint.mvc.HealthMvcEndpoint) MvcEndpoint(org.springframework.boot.actuate.endpoint.mvc.MvcEndpoint) EndpointHandlerMappingCustomizer(org.springframework.boot.actuate.endpoint.mvc.EndpointHandlerMappingCustomizer) CorsConfiguration(org.springframework.web.cors.CorsConfiguration) MvcEndpointSecurityInterceptor(org.springframework.boot.actuate.endpoint.mvc.MvcEndpointSecurityInterceptor) EndpointHandlerMapping(org.springframework.boot.actuate.endpoint.mvc.EndpointHandlerMapping) ConditionalOnMissingBean(org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean) ConditionalOnBean(org.springframework.boot.autoconfigure.condition.ConditionalOnBean) ConditionalOnMissingBean(org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean) Bean(org.springframework.context.annotation.Bean)

Example 44 with CorsConfiguration

use of org.springframework.web.cors.CorsConfiguration in project spring-boot by spring-projects.

the class CloudFoundryActuatorAutoConfiguration method cloudFoundryEndpointHandlerMapping.

@Bean
public CloudFoundryEndpointHandlerMapping cloudFoundryEndpointHandlerMapping(MvcEndpoints mvcEndpoints, RestTemplateBuilder restTemplateBuilder, Environment environment) {
    Set<NamedMvcEndpoint> endpoints = new LinkedHashSet<>(mvcEndpoints.getEndpoints(NamedMvcEndpoint.class));
    HandlerInterceptor securityInterceptor = getSecurityInterceptor(restTemplateBuilder, environment);
    CorsConfiguration corsConfiguration = getCorsConfiguration();
    CloudFoundryEndpointHandlerMapping mapping = new CloudFoundryEndpointHandlerMapping(endpoints, corsConfiguration, securityInterceptor);
    mapping.setPrefix("/cloudfoundryapplication");
    return mapping;
}
Also used : LinkedHashSet(java.util.LinkedHashSet) NamedMvcEndpoint(org.springframework.boot.actuate.endpoint.mvc.NamedMvcEndpoint) CorsConfiguration(org.springframework.web.cors.CorsConfiguration) HandlerInterceptor(org.springframework.web.servlet.HandlerInterceptor) ConditionalOnBean(org.springframework.boot.autoconfigure.condition.ConditionalOnBean) Bean(org.springframework.context.annotation.Bean)

Aggregations

CorsConfiguration (org.springframework.web.cors.CorsConfiguration)44 Test (org.junit.Test)27 HandlerExecutionChain (org.springframework.web.servlet.HandlerExecutionChain)15 HandlerMethod (org.springframework.web.method.HandlerMethod)7 Bean (org.springframework.context.annotation.Bean)4 MockHttpServletRequest (org.springframework.mock.web.test.MockHttpServletRequest)3 ServerWebExchange (org.springframework.web.server.ServerWebExchange)3 HandlerInterceptor (org.springframework.web.servlet.HandlerInterceptor)3 List (java.util.List)2 DirectFieldAccessor (org.springframework.beans.DirectFieldAccessor)2 ConditionalOnBean (org.springframework.boot.autoconfigure.condition.ConditionalOnBean)2 CrossOrigin (org.springframework.web.bind.annotation.CrossOrigin)2 RequestMethod (org.springframework.web.bind.annotation.RequestMethod)2 AnnotationConfigWebApplicationContext (org.springframework.web.context.support.AnnotationConfigWebApplicationContext)2 UrlBasedCorsConfigurationSource (org.springframework.web.cors.UrlBasedCorsConfigurationSource)2 CorsFilter (org.springframework.web.filter.CorsFilter)2 AbstractHandlerMapping (org.springframework.web.servlet.handler.AbstractHandlerMapping)2 LinkedHashMap (java.util.LinkedHashMap)1 LinkedHashSet (java.util.LinkedHashSet)1 HttpWebRequestProperties (org.apereo.cas.configuration.model.core.web.security.HttpWebRequestProperties)1