Search in sources :

Example 21 with CorsConfiguration

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

the class CrossOriginTests method customOriginDefinedViaPlaceholder.

@Test
public void customOriginDefinedViaPlaceholder() throws Exception {
    this.handlerMapping.registerHandler(new MethodLevelController());
    this.request.setRequestURI("/someOrigin");
    HandlerExecutionChain chain = this.handlerMapping.getHandler(request);
    CorsConfiguration config = getCorsConfiguration(chain, false);
    assertNotNull(config);
    assertEquals(Arrays.asList("http://example.com"), config.getAllowedOrigins());
    assertTrue(config.getAllowCredentials());
}
Also used : CorsConfiguration(org.springframework.web.cors.CorsConfiguration) HandlerExecutionChain(org.springframework.web.servlet.HandlerExecutionChain) Test(org.junit.Test)

Example 22 with CorsConfiguration

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

the class AbstractSockJsService method getCorsConfiguration.

@Override
public CorsConfiguration getCorsConfiguration(HttpServletRequest request) {
    if (!this.suppressCors && CorsUtils.isCorsRequest(request)) {
        CorsConfiguration config = new CorsConfiguration();
        config.addAllowedOrigin("*");
        config.addAllowedMethod("*");
        config.setAllowCredentials(true);
        config.setMaxAge(ONE_YEAR);
        config.addAllowedHeader("*");
        return config;
    }
    return null;
}
Also used : CorsConfiguration(org.springframework.web.cors.CorsConfiguration)

Example 23 with CorsConfiguration

use of org.springframework.web.cors.CorsConfiguration in project c4sg-services by Code4SocialGood.

the class RestConfig method corsFilter.

@Bean
public CorsFilter corsFilter() {
    UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
    CorsConfiguration config = new CorsConfiguration();
    config.setAllowCredentials(true);
    config.addAllowedOrigin("*");
    config.addAllowedHeader("*");
    config.addAllowedMethod("OPTIONS");
    config.addAllowedMethod("GET");
    config.addAllowedMethod("POST");
    config.addAllowedMethod("PUT");
    config.addAllowedMethod("DELETE");
    source.registerCorsConfiguration("/**", config);
    return new CorsFilter(source);
}
Also used : CorsFilter(org.springframework.web.filter.CorsFilter) UrlBasedCorsConfigurationSource(org.springframework.web.cors.UrlBasedCorsConfigurationSource) CorsConfiguration(org.springframework.web.cors.CorsConfiguration) Bean(org.springframework.context.annotation.Bean)

Example 24 with CorsConfiguration

use of org.springframework.web.cors.CorsConfiguration in project cas by apereo.

the class CasFiltersConfiguration method casCorsFilter.

@ConditionalOnProperty(prefix = "cas.httpWebRequest.cors", name = "enabled", havingValue = "true")
@Bean
@RefreshScope
public FilterRegistrationBean casCorsFilter() {
    final HttpWebRequestProperties.Cors cors = casProperties.getHttpWebRequest().getCors();
    final UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
    final CorsConfiguration config = new CorsConfiguration();
    config.setAllowCredentials(cors.isEnabled());
    config.setAllowedOrigins(cors.getAllowOrigins());
    config.setAllowedMethods(cors.getAllowMethods());
    config.setAllowedHeaders(cors.getAllowHeaders());
    config.setMaxAge(cors.getMaxAge());
    config.setExposedHeaders(cors.getExposedHeaders());
    source.registerCorsConfiguration("/**", config);
    final FilterRegistrationBean bean = new FilterRegistrationBean(new CorsFilter(source));
    bean.setName("casCorsFilter");
    bean.setAsyncSupported(true);
    bean.setOrder(0);
    return bean;
}
Also used : CorsFilter(org.springframework.web.filter.CorsFilter) UrlBasedCorsConfigurationSource(org.springframework.web.cors.UrlBasedCorsConfigurationSource) CorsConfiguration(org.springframework.web.cors.CorsConfiguration) HttpWebRequestProperties(org.apereo.cas.configuration.model.core.web.security.HttpWebRequestProperties) FilterRegistrationBean(org.springframework.boot.web.servlet.FilterRegistrationBean) RefreshScope(org.springframework.cloud.context.config.annotation.RefreshScope) ConditionalOnProperty(org.springframework.boot.autoconfigure.condition.ConditionalOnProperty) FilterRegistrationBean(org.springframework.boot.web.servlet.FilterRegistrationBean) Bean(org.springframework.context.annotation.Bean)

Example 25 with CorsConfiguration

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

the class AbstractHandlerMapping method getHandler.

@Override
public Mono<Object> getHandler(ServerWebExchange exchange) {
    return getHandlerInternal(exchange).map(handler -> {
        if (CorsUtils.isCorsRequest(exchange.getRequest())) {
            CorsConfiguration configA = this.globalCorsConfigSource.getCorsConfiguration(exchange);
            CorsConfiguration configB = getCorsConfiguration(handler, exchange);
            CorsConfiguration config = (configA != null ? configA.combine(configB) : configB);
            if (!getCorsProcessor().processRequest(config, exchange) || CorsUtils.isPreFlightRequest(exchange.getRequest())) {
                return REQUEST_HANDLED_HANDLER;
            }
        }
        return handler;
    });
}
Also used : CorsConfiguration(org.springframework.web.cors.CorsConfiguration)

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