Search in sources :

Example 1 with CorsHandlerInterceptor

use of cn.taketoday.web.interceptor.CorsHandlerInterceptor in project today-framework by TAKETODAY.

the class HandlerCorsCustomizer method customize.

@Override
public Object customize(ActionMappingAnnotationHandler handler) {
    // 预防已经设置
    HandlerInterceptor[] interceptors = handler.getInterceptors();
    if (ObjectUtils.isNotEmpty(interceptors)) {
        for (HandlerInterceptor interceptor : interceptors) {
            if (interceptor instanceof CorsHandlerInterceptor) {
                return handler;
            }
        }
    }
    CrossOrigin methodCrossOrigin = handler.getMethod().getMethodAnnotation(CrossOrigin.class);
    CrossOrigin classCrossOrigin = handler.getMethod().getDeclaringClassAnnotation(CrossOrigin.class);
    if (classCrossOrigin == null && methodCrossOrigin == null) {
        // 没有 @CrossOrigin 配置
        return handler;
    }
    CorsConfiguration config = new CorsConfiguration();
    updateCorsConfig(config, classCrossOrigin);
    updateCorsConfig(config, methodCrossOrigin);
    // 覆盖默认
    config.applyPermitDefaultValues();
    CorsHandlerInterceptor interceptor = new CorsHandlerInterceptor(config);
    interceptor.setCorsProcessor(processor);
    interceptor.setOrder(Ordered.HIGHEST_PRECEDENCE);
    handler.addInterceptors(interceptor);
    return handler;
}
Also used : CrossOrigin(cn.taketoday.web.annotation.CrossOrigin) CorsHandlerInterceptor(cn.taketoday.web.interceptor.CorsHandlerInterceptor) CorsConfiguration(cn.taketoday.web.cors.CorsConfiguration) HandlerInterceptor(cn.taketoday.web.interceptor.HandlerInterceptor) CorsHandlerInterceptor(cn.taketoday.web.interceptor.CorsHandlerInterceptor)

Example 2 with CorsHandlerInterceptor

use of cn.taketoday.web.interceptor.CorsHandlerInterceptor in project today-infrastructure by TAKETODAY.

the class HandlerCorsCustomizer method customize.

@Override
public Object customize(ActionMappingAnnotationHandler handler) {
    // 预防已经设置
    HandlerInterceptor[] interceptors = handler.getInterceptors();
    if (ObjectUtils.isNotEmpty(interceptors)) {
        for (HandlerInterceptor interceptor : interceptors) {
            if (interceptor instanceof CorsHandlerInterceptor) {
                return handler;
            }
        }
    }
    CrossOrigin methodCrossOrigin = handler.getMethod().getMethodAnnotation(CrossOrigin.class);
    CrossOrigin classCrossOrigin = handler.getMethod().getDeclaringClassAnnotation(CrossOrigin.class);
    if (classCrossOrigin == null && methodCrossOrigin == null) {
        // 没有 @CrossOrigin 配置
        return handler;
    }
    CorsConfiguration config = new CorsConfiguration();
    updateCorsConfig(config, classCrossOrigin);
    updateCorsConfig(config, methodCrossOrigin);
    // 覆盖默认
    config.applyPermitDefaultValues();
    CorsHandlerInterceptor interceptor = new CorsHandlerInterceptor(config);
    interceptor.setCorsProcessor(processor);
    interceptor.setOrder(Ordered.HIGHEST_PRECEDENCE);
    handler.addInterceptors(interceptor);
    return handler;
}
Also used : CrossOrigin(cn.taketoday.web.annotation.CrossOrigin) CorsHandlerInterceptor(cn.taketoday.web.interceptor.CorsHandlerInterceptor) CorsConfiguration(cn.taketoday.web.cors.CorsConfiguration) HandlerInterceptor(cn.taketoday.web.interceptor.HandlerInterceptor) CorsHandlerInterceptor(cn.taketoday.web.interceptor.CorsHandlerInterceptor)

Aggregations

CrossOrigin (cn.taketoday.web.annotation.CrossOrigin)2 CorsConfiguration (cn.taketoday.web.cors.CorsConfiguration)2 CorsHandlerInterceptor (cn.taketoday.web.interceptor.CorsHandlerInterceptor)2 HandlerInterceptor (cn.taketoday.web.interceptor.HandlerInterceptor)2