All Downloads are FREE. Search and download functionalities are using the official Maven repository.

matrix.boot.based.config.CorsAutoConfiguration Maven / Gradle / Ivy

There is a newer version: 2.1.10
Show newest version
package matrix.boot.based.config;

import lombok.NonNull;
import matrix.boot.based.filter.FluxAllowAllCorsFilter;
import matrix.boot.based.filter.MvcAllowAllCorsFilter;
import matrix.boot.based.properties.CorsProperties;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.util.CollectionUtils;
import org.springframework.web.reactive.config.CorsRegistry;
import org.springframework.web.reactive.config.WebFluxConfigurer;
import org.springframework.web.servlet.config.annotation.CorsRegistration;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import java.util.List;

/**
 * 跨域自动配置
 *
 * @author wangcheng
 * 2021/10/6
 **/
@EnableConfigurationProperties({CorsProperties.class, CorsProperties.Url.class})
public class CorsAutoConfiguration {

    /**
     * web Mvc跨域配置
     */
    @ConditionalOnProperty(value = {"matrix.cors.mvc-enabled"})
    public static class WebMvcCorsConfiguration implements WebMvcConfigurer {
        /**
         * 跨域配置
         */
        private final CorsProperties corsProperties;

        public WebMvcCorsConfiguration(CorsProperties corsProperties) {
            this.corsProperties = corsProperties;
        }

        @Override
        public void addCorsMappings(@NonNull org.springframework.web.servlet.config.annotation.CorsRegistry registry) {
            List urls = corsProperties.getUrls();
            if (!CollectionUtils.isEmpty(urls)) {
                for (CorsProperties.Url url : urls) {
                    CorsRegistration registration = registry.addMapping(url.getMapping()).exposedHeaders(url.getExposeHeaders()).allowCredentials(url.isAllowCredentials()).allowedHeaders(url.getHeaders()).allowedMethods(url.getMethods());
                    if (url.isAllowCredentials()) {
                        registration.allowedOriginPatterns(url.getAllowedOriginPatterns());
                    } else {
                        registration.allowedOrigins(url.getOrigins());
                    }
                }
            }
        }

        @Bean
        @ConditionalOnProperty(value = {"matrix.cors.allow-all"})
        public MvcAllowAllCorsFilter mvcAllowAllCorsFilter() {
            return new MvcAllowAllCorsFilter();
        }
    }

    /**
     * web Flux跨域配置
     */
    @ConditionalOnProperty(value = {"matrix.cors.flux-enabled"})
    public static class WebFluxCorsConfiguration implements WebFluxConfigurer {

        /**
         * 跨域配置
         */
        private final CorsProperties corsProperties;

        public WebFluxCorsConfiguration(CorsProperties corsProperties) {
            this.corsProperties = corsProperties;
        }

        @Override
        public void addCorsMappings(@NonNull CorsRegistry registry) {
            List urls = corsProperties.getUrls();
            if (!CollectionUtils.isEmpty(urls)) {
                for (CorsProperties.Url url : urls) {
                    org.springframework.web.reactive.config.CorsRegistration registration = registry.addMapping(url.getMapping()).exposedHeaders(url.getExposeHeaders()).allowCredentials(url.isAllowCredentials()).allowedHeaders(url.getHeaders()).allowedMethods(url.getMethods());
                    if (url.isAllowCredentials()) {
                        registration.allowedOriginPatterns(url.getAllowedOriginPatterns());
                    } else {
                        registration.allowedOrigins(url.getOrigins());
                    }
                }
            }
        }

        @Bean
        @ConditionalOnProperty(value = {"matrix.cors.allow-all"})
        public FluxAllowAllCorsFilter fluxAllowAllCorsFilter() {
            return new FluxAllowAllCorsFilter();
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy