Search in sources :

Example 36 with CorsConfiguration

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

the class HandlerMethodMappingTests method registerMapping.

@Test
public void registerMapping() throws Exception {
    String key1 = "/foo";
    String key2 = "/foo*";
    this.mapping.registerMapping(key1, this.handler, this.method1);
    this.mapping.registerMapping(key2, this.handler, this.method2);
    // Direct URL lookup
    List directUrlMatches = this.mapping.getMappingRegistry().getMappingsByUrl(key1);
    assertNotNull(directUrlMatches);
    assertEquals(1, directUrlMatches.size());
    assertEquals(key1, directUrlMatches.get(0));
    // Mapping name lookup
    HandlerMethod handlerMethod1 = new HandlerMethod(this.handler, this.method1);
    HandlerMethod handlerMethod2 = new HandlerMethod(this.handler, this.method2);
    String name1 = this.method1.getName();
    List<HandlerMethod> handlerMethods = this.mapping.getMappingRegistry().getHandlerMethodsByMappingName(name1);
    assertNotNull(handlerMethods);
    assertEquals(1, handlerMethods.size());
    assertEquals(handlerMethod1, handlerMethods.get(0));
    String name2 = this.method2.getName();
    handlerMethods = this.mapping.getMappingRegistry().getHandlerMethodsByMappingName(name2);
    assertNotNull(handlerMethods);
    assertEquals(1, handlerMethods.size());
    assertEquals(handlerMethod2, handlerMethods.get(0));
    // CORS lookup
    CorsConfiguration config = this.mapping.getMappingRegistry().getCorsConfiguration(handlerMethod1);
    assertNotNull(config);
    assertEquals("http://" + handler.hashCode() + name1, config.getAllowedOrigins().get(0));
    config = this.mapping.getMappingRegistry().getCorsConfiguration(handlerMethod2);
    assertNotNull(config);
    assertEquals("http://" + handler.hashCode() + name2, config.getAllowedOrigins().get(0));
}
Also used : CorsConfiguration(org.springframework.web.cors.CorsConfiguration) List(java.util.List) HandlerMethod(org.springframework.web.method.HandlerMethod) Test(org.junit.Test)

Example 37 with CorsConfiguration

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

the class CorsAbstractHandlerMappingTests method preflightRequestWithMappedCorsConfiguration.

@Test
public void preflightRequestWithMappedCorsConfiguration() throws Exception {
    CorsConfiguration config = new CorsConfiguration();
    config.addAllowedOrigin("*");
    this.handlerMapping.setCorsConfigurations(Collections.singletonMap("/foo", config));
    this.request.setMethod(RequestMethod.OPTIONS.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);
    assertNotNull(chain.getHandler());
    assertTrue(chain.getHandler().getClass().getSimpleName().equals("PreFlightHandler"));
    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 38 with CorsConfiguration

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

the class CorsAbstractHandlerMappingTests method actualRequestWithCorsConfigurationProvider.

@Test
public void actualRequestWithCorsConfigurationProvider() throws Exception {
    this.request.setMethod(RequestMethod.GET.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);
    assertTrue(chain.getHandler() instanceof CorsAwareHandler);
    CorsConfiguration 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 39 with CorsConfiguration

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

the class CorsAbstractHandlerMappingTests method getCorsConfiguration.

private CorsConfiguration getCorsConfiguration(HandlerExecutionChain chain, boolean isPreFlightRequest) {
    if (isPreFlightRequest) {
        Object handler = chain.getHandler();
        assertTrue(handler.getClass().getSimpleName().equals("PreFlightHandler"));
        DirectFieldAccessor accessor = new DirectFieldAccessor(handler);
        return (CorsConfiguration) accessor.getPropertyValue("config");
    } else {
        HandlerInterceptor[] interceptors = chain.getInterceptors();
        if (interceptors != null) {
            for (HandlerInterceptor interceptor : interceptors) {
                if (interceptor.getClass().getSimpleName().equals("CorsInterceptor")) {
                    DirectFieldAccessor accessor = new DirectFieldAccessor(interceptor);
                    return (CorsConfiguration) accessor.getPropertyValue("config");
                }
            }
        }
    }
    return null;
}
Also used : CorsConfiguration(org.springframework.web.cors.CorsConfiguration) HandlerInterceptor(org.springframework.web.servlet.HandlerInterceptor) DirectFieldAccessor(org.springframework.beans.DirectFieldAccessor)

Example 40 with CorsConfiguration

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

the class CrossOriginTests method customOriginDefinedViaValueAttribute.

@Test
public void customOriginDefinedViaValueAttribute() throws Exception {
    this.handlerMapping.registerHandler(new MethodLevelController());
    this.request.setRequestURI("/customOrigin");
    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)

Aggregations

CorsConfiguration (org.springframework.web.cors.CorsConfiguration)49 Test (org.junit.Test)27 HandlerExecutionChain (org.springframework.web.servlet.HandlerExecutionChain)15 Bean (org.springframework.context.annotation.Bean)9 UrlBasedCorsConfigurationSource (org.springframework.web.cors.UrlBasedCorsConfigurationSource)7 HandlerMethod (org.springframework.web.method.HandlerMethod)7 CorsFilter (org.springframework.web.filter.CorsFilter)6 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 AbstractHandlerMapping (org.springframework.web.servlet.handler.AbstractHandlerMapping)2 DynamicCorsConfiguration (eu.bcvsolutions.idm.core.config.domain.DynamicCorsConfiguration)1 LinkedHashMap (java.util.LinkedHashMap)1 LinkedHashSet (java.util.LinkedHashSet)1