Search in sources :

Example 1 with CorsConfiguration

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

the class EndpointWebMvcManagementContextConfiguration method getCorsConfiguration.

private CorsConfiguration getCorsConfiguration(EndpointCorsProperties properties) {
    if (CollectionUtils.isEmpty(properties.getAllowedOrigins())) {
        return null;
    }
    CorsConfiguration configuration = new CorsConfiguration();
    configuration.setAllowedOrigins(properties.getAllowedOrigins());
    if (!CollectionUtils.isEmpty(properties.getAllowedHeaders())) {
        configuration.setAllowedHeaders(properties.getAllowedHeaders());
    }
    if (!CollectionUtils.isEmpty(properties.getAllowedMethods())) {
        configuration.setAllowedMethods(properties.getAllowedMethods());
    }
    if (!CollectionUtils.isEmpty(properties.getExposedHeaders())) {
        configuration.setExposedHeaders(properties.getExposedHeaders());
    }
    if (properties.getMaxAge() != null) {
        configuration.setMaxAge(properties.getMaxAge());
    }
    if (properties.getAllowCredentials() != null) {
        configuration.setAllowCredentials(properties.getAllowCredentials());
    }
    return configuration;
}
Also used : CorsConfiguration(org.springframework.web.cors.CorsConfiguration)

Example 2 with CorsConfiguration

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

the class CloudFoundryActuatorAutoConfiguration method getCorsConfiguration.

private CorsConfiguration getCorsConfiguration() {
    CorsConfiguration corsConfiguration = new CorsConfiguration();
    corsConfiguration.addAllowedOrigin(CorsConfiguration.ALL);
    corsConfiguration.setAllowedMethods(Arrays.asList(HttpMethod.GET.name(), HttpMethod.POST.name()));
    corsConfiguration.setAllowedHeaders(Arrays.asList("Authorization", "X-Cf-App-Instance", "Content-Type"));
    return corsConfiguration;
}
Also used : CorsConfiguration(org.springframework.web.cors.CorsConfiguration)

Example 3 with CorsConfiguration

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

the class DefaultCorsProcessorTests method setup.

@Before
public void setup() {
    this.conf = new CorsConfiguration();
    this.processor = new DefaultCorsProcessor();
}
Also used : CorsConfiguration(org.springframework.web.cors.CorsConfiguration) Before(org.junit.Before)

Example 4 with CorsConfiguration

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

the class UrlBasedCorsConfigurationSourceTests method registerAndMatch.

@Test
public void registerAndMatch() {
    CorsConfiguration config = new CorsConfiguration();
    this.configSource.registerCorsConfiguration("/bar/**", config);
    ServerWebExchange exchange = MockServerHttpRequest.get("/foo/test.html").toExchange();
    assertNull(this.configSource.getCorsConfiguration(exchange));
    exchange = MockServerHttpRequest.get("/bar/test.html").toExchange();
    assertEquals(config, this.configSource.getCorsConfiguration(exchange));
}
Also used : ServerWebExchange(org.springframework.web.server.ServerWebExchange) CorsConfiguration(org.springframework.web.cors.CorsConfiguration) Test(org.junit.Test)

Example 5 with CorsConfiguration

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

the class AbstractHandlerMethodMapping method getCorsConfiguration.

@Override
protected CorsConfiguration getCorsConfiguration(Object handler, ServerWebExchange exchange) {
    CorsConfiguration corsConfig = super.getCorsConfiguration(handler, exchange);
    if (handler instanceof HandlerMethod) {
        HandlerMethod handlerMethod = (HandlerMethod) handler;
        if (handlerMethod.equals(PREFLIGHT_AMBIGUOUS_MATCH)) {
            return ALLOW_CORS_CONFIG;
        }
        CorsConfiguration methodConfig = this.mappingRegistry.getCorsConfiguration(handlerMethod);
        corsConfig = (corsConfig != null ? corsConfig.combine(methodConfig) : methodConfig);
    }
    return corsConfig;
}
Also used : CorsConfiguration(org.springframework.web.cors.CorsConfiguration) HandlerMethod(org.springframework.web.method.HandlerMethod)

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