Search in sources :

Example 11 with CorsConfiguration

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

the class CorsRegistryTests method customizedMapping.

@Test
public void customizedMapping() {
    this.registry.addMapping("/foo").allowedOrigins("http://domain2.com", "http://domain2.com").allowedMethods("DELETE").allowCredentials(false).allowedHeaders("header1", "header2").exposedHeaders("header3", "header4").maxAge(3600);
    Map<String, CorsConfiguration> configs = this.registry.getCorsConfigurations();
    assertEquals(1, configs.size());
    CorsConfiguration config = configs.get("/foo");
    assertEquals(Arrays.asList("http://domain2.com", "http://domain2.com"), config.getAllowedOrigins());
    assertEquals(Arrays.asList("DELETE"), config.getAllowedMethods());
    assertEquals(Arrays.asList("header1", "header2"), config.getAllowedHeaders());
    assertEquals(Arrays.asList("header3", "header4"), config.getExposedHeaders());
    assertEquals(false, config.getAllowCredentials());
    assertEquals(Long.valueOf(3600), config.getMaxAge());
}
Also used : CorsConfiguration(org.springframework.web.cors.CorsConfiguration) Test(org.junit.Test)

Example 12 with CorsConfiguration

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

the class CorsAbstractHandlerMappingTests method preflightRequestWithCorsConfigurationProvider.

@Test
public void preflightRequestWithCorsConfigurationProvider() throws Exception {
    this.request.setMethod(RequestMethod.OPTIONS.name());
    this.request.setRequestURI("/cors");
    this.request.addHeader(HttpHeaders.ORIGIN, "http://domain2.com");
    this.request.addHeader(HttpHeaders.ACCESS_CONTROL_REQUEST_METHOD, "GET");
    HandlerExecutionChain chain = handlerMapping.getHandler(this.request);
    assertNotNull(chain);
    assertNotNull(chain.getHandler());
    assertTrue(chain.getHandler().getClass().getSimpleName().equals("PreFlightHandler"));
    CorsConfiguration config = getCorsConfiguration(chain, true);
    assertNotNull(config);
    assertArrayEquals(config.getAllowedOrigins().toArray(), new String[] { "*" });
}
Also used : CorsConfiguration(org.springframework.web.cors.CorsConfiguration) HandlerExecutionChain(org.springframework.web.servlet.HandlerExecutionChain) Test(org.junit.Test)

Example 13 with CorsConfiguration

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

the class CorsAbstractHandlerMappingTests method actualRequestWithMappedCorsConfiguration.

@Test
public void actualRequestWithMappedCorsConfiguration() throws Exception {
    CorsConfiguration config = new CorsConfiguration();
    config.addAllowedOrigin("*");
    this.handlerMapping.setCorsConfigurations(Collections.singletonMap("/foo", config));
    this.request.setMethod(RequestMethod.GET.name());
    this.request.setRequestURI("/foo");
    this.request.addHeader(HttpHeaders.ORIGIN, "http://domain2.com");
    this.request.addHeader(HttpHeaders.ACCESS_CONTROL_REQUEST_METHOD, "GET");
    HandlerExecutionChain chain = handlerMapping.getHandler(this.request);
    assertNotNull(chain);
    assertTrue(chain.getHandler() instanceof SimpleHandler);
    config = getCorsConfiguration(chain, false);
    assertNotNull(config);
    assertArrayEquals(config.getAllowedOrigins().toArray(), new String[] { "*" });
}
Also used : CorsConfiguration(org.springframework.web.cors.CorsConfiguration) HandlerExecutionChain(org.springframework.web.servlet.HandlerExecutionChain) Test(org.junit.Test)

Example 14 with CorsConfiguration

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

the class HandlerMappingIntrospectorTests method getCorsConfigurationActual.

@Test
public void getCorsConfigurationActual() throws Exception {
    AnnotationConfigWebApplicationContext cxt = new AnnotationConfigWebApplicationContext();
    cxt.register(TestConfig.class);
    cxt.refresh();
    MockHttpServletRequest request = new MockHttpServletRequest("POST", "/path");
    request.addHeader("Origin", "http://localhost:9000");
    CorsConfiguration corsConfig = new HandlerMappingIntrospector(cxt).getCorsConfiguration(request);
    assertNotNull(corsConfig);
    assertEquals(Collections.singletonList("http://localhost:9000"), corsConfig.getAllowedOrigins());
    assertEquals(Collections.singletonList("POST"), corsConfig.getAllowedMethods());
}
Also used : CorsConfiguration(org.springframework.web.cors.CorsConfiguration) MockHttpServletRequest(org.springframework.mock.web.test.MockHttpServletRequest) AnnotationConfigWebApplicationContext(org.springframework.web.context.support.AnnotationConfigWebApplicationContext) Test(org.junit.Test)

Example 15 with CorsConfiguration

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

the class HandlerMethodMappingTests method getCorsConfigWithBeanNameHandler.

@Test
public void getCorsConfigWithBeanNameHandler() throws Exception {
    String key = "foo";
    String beanName = "handler1";
    StaticWebApplicationContext context = new StaticWebApplicationContext();
    context.registerSingleton(beanName, MyHandler.class);
    this.mapping.setApplicationContext(context);
    this.mapping.registerMapping(key, beanName, this.method1);
    HandlerMethod handlerMethod = this.mapping.getHandlerInternal(new MockHttpServletRequest("GET", key));
    CorsConfiguration config = this.mapping.getMappingRegistry().getCorsConfiguration(handlerMethod);
    assertNotNull(config);
    assertEquals("http://" + beanName.hashCode() + this.method1.getName(), config.getAllowedOrigins().get(0));
}
Also used : CorsConfiguration(org.springframework.web.cors.CorsConfiguration) MockHttpServletRequest(org.springframework.mock.web.test.MockHttpServletRequest) StaticWebApplicationContext(org.springframework.web.context.support.StaticWebApplicationContext) HandlerMethod(org.springframework.web.method.HandlerMethod) Test(org.junit.Test)

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