Search in sources :

Example 31 with CorsConfiguration

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

the class RequestMappingHandlerMapping method initCorsConfiguration.

@Override
protected CorsConfiguration initCorsConfiguration(Object handler, Method method, RequestMappingInfo mappingInfo) {
    HandlerMethod handlerMethod = createHandlerMethod(handler, method);
    Class<?> beanType = handlerMethod.getBeanType();
    CrossOrigin typeAnnotation = AnnotatedElementUtils.findMergedAnnotation(beanType, CrossOrigin.class);
    CrossOrigin methodAnnotation = AnnotatedElementUtils.findMergedAnnotation(method, CrossOrigin.class);
    if (typeAnnotation == null && methodAnnotation == null) {
        return null;
    }
    CorsConfiguration config = new CorsConfiguration();
    updateCorsConfig(config, typeAnnotation);
    updateCorsConfig(config, methodAnnotation);
    if (CollectionUtils.isEmpty(config.getAllowedMethods())) {
        for (RequestMethod allowedMethod : mappingInfo.getMethodsCondition().getMethods()) {
            config.addAllowedMethod(allowedMethod.name());
        }
    }
    return config.applyPermitDefaultValues();
}
Also used : CrossOrigin(org.springframework.web.bind.annotation.CrossOrigin) CorsConfiguration(org.springframework.web.cors.CorsConfiguration) RequestMethod(org.springframework.web.bind.annotation.RequestMethod) HandlerMethod(org.springframework.web.method.HandlerMethod)

Example 32 with CorsConfiguration

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

the class RequestMappingHandlerMapping method initCorsConfiguration.

@Override
protected CorsConfiguration initCorsConfiguration(Object handler, Method method, RequestMappingInfo mappingInfo) {
    HandlerMethod handlerMethod = createHandlerMethod(handler, method);
    Class<?> beanType = handlerMethod.getBeanType();
    CrossOrigin typeAnnotation = AnnotatedElementUtils.findMergedAnnotation(beanType, CrossOrigin.class);
    CrossOrigin methodAnnotation = AnnotatedElementUtils.findMergedAnnotation(method, CrossOrigin.class);
    if (typeAnnotation == null && methodAnnotation == null) {
        return null;
    }
    CorsConfiguration config = new CorsConfiguration();
    updateCorsConfig(config, typeAnnotation);
    updateCorsConfig(config, methodAnnotation);
    if (CollectionUtils.isEmpty(config.getAllowedMethods())) {
        for (RequestMethod allowedMethod : mappingInfo.getMethodsCondition().getMethods()) {
            config.addAllowedMethod(allowedMethod.name());
        }
    }
    return config.applyPermitDefaultValues();
}
Also used : CrossOrigin(org.springframework.web.bind.annotation.CrossOrigin) CorsConfiguration(org.springframework.web.cors.CorsConfiguration) RequestMethod(org.springframework.web.bind.annotation.RequestMethod) HandlerMethod(org.springframework.web.method.HandlerMethod)

Example 33 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, HttpServletRequest request) {
    CorsConfiguration corsConfig = super.getCorsConfiguration(handler, request);
    if (handler instanceof HandlerMethod) {
        HandlerMethod handlerMethod = (HandlerMethod) handler;
        if (handlerMethod.equals(PREFLIGHT_AMBIGUOUS_MATCH)) {
            return AbstractHandlerMethodMapping.ALLOW_CORS_CONFIG;
        } else {
            CorsConfiguration corsConfigFromMethod = this.mappingRegistry.getCorsConfiguration(handlerMethod);
            corsConfig = (corsConfig != null ? corsConfig.combine(corsConfigFromMethod) : corsConfigFromMethod);
        }
    }
    return corsConfig;
}
Also used : CorsConfiguration(org.springframework.web.cors.CorsConfiguration) HandlerMethod(org.springframework.web.method.HandlerMethod)

Example 34 with CorsConfiguration

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

the class HandlerMappingIntrospectorTests method getCorsConfigurationPreFlight.

@Test
public void getCorsConfigurationPreFlight() throws Exception {
    AnnotationConfigWebApplicationContext cxt = new AnnotationConfigWebApplicationContext();
    cxt.register(TestConfig.class);
    cxt.refresh();
    // PRE-FLIGHT
    MockHttpServletRequest request = new MockHttpServletRequest("OPTIONS", "/path");
    request.addHeader("Origin", "http://localhost:9000");
    request.addHeader(HttpHeaders.ACCESS_CONTROL_REQUEST_METHOD, "POST");
    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 35 with CorsConfiguration

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

the class HandlerMethodMappingTests method registerMappingWithSameMethodAndTwoHandlerInstances.

@Test
public void registerMappingWithSameMethodAndTwoHandlerInstances() throws Exception {
    String key1 = "foo";
    String key2 = "bar";
    MyHandler handler1 = new MyHandler();
    MyHandler handler2 = new MyHandler();
    HandlerMethod handlerMethod1 = new HandlerMethod(handler1, this.method1);
    HandlerMethod handlerMethod2 = new HandlerMethod(handler2, this.method1);
    this.mapping.registerMapping(key1, handler1, this.method1);
    this.mapping.registerMapping(key2, handler2, this.method1);
    // Direct URL lookup
    List directUrlMatches = this.mapping.getMappingRegistry().getMappingsByUrl(key1);
    assertNotNull(directUrlMatches);
    assertEquals(1, directUrlMatches.size());
    assertEquals(key1, directUrlMatches.get(0));
    // Mapping name lookup
    String name = this.method1.getName();
    List<HandlerMethod> handlerMethods = this.mapping.getMappingRegistry().getHandlerMethodsByMappingName(name);
    assertNotNull(handlerMethods);
    assertEquals(2, handlerMethods.size());
    assertEquals(handlerMethod1, handlerMethods.get(0));
    assertEquals(handlerMethod2, handlerMethods.get(1));
    // CORS lookup
    CorsConfiguration config = this.mapping.getMappingRegistry().getCorsConfiguration(handlerMethod1);
    assertNotNull(config);
    assertEquals("http://" + handler1.hashCode() + name, config.getAllowedOrigins().get(0));
    config = this.mapping.getMappingRegistry().getCorsConfiguration(handlerMethod2);
    assertNotNull(config);
    assertEquals("http://" + handler2.hashCode() + name, 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)

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