1,采用内置默认跨域过滤器的方式指定默认的跨域配置。
编写配置Bean
MagicalCorsProperties
importcom.google.common.base.MoreObjects; importjava.io.Serializable; importjava.util.ArrayList; importjava.util.Collections; importjava.util.List; importorg.springframework.boot.context.properties.ConfigurationProperties; importorg.springframework.stereotype.Component; /*** 跨域自动配置* @author Jeckxu*/prefix=MagicalCorsProperties.PRE_FIX) (publicclassMagicalCorsPropertiesimplementsSerializable { publicstaticfinalStringPRE_FIX="magical.cors.default"; privateBooleanenabled=true; privateStringpathPattern="/cors/**"; privateList<String>allowedOrigins=Collections.singletonList("*"); privateList<String>allowedHeaders=Collections.singletonList("*"); privateList<String>allowedMethods=Collections.singletonList("*"); privateList<String>exposedHeaders=newArrayList<>(); privateLongmaxAge=3600L; privateBooleanallowCredentials=Boolean.TRUE; publicBooleangetEnabled() { returnenabled; } publicvoidsetEnabled(Booleanenabled) { this.enabled=enabled; } publicStringgetPathPattern() { returnpathPattern; } publicvoidsetPathPattern(StringpathPattern) { this.pathPattern=pathPattern; } publicList<String>getAllowedOrigins() { returnallowedOrigins; } publicvoidsetAllowedOrigins(List<String>allowedOrigins) { this.allowedOrigins=allowedOrigins; } publicList<String>getAllowedHeaders() { returnallowedHeaders; } publicvoidsetAllowedHeaders(List<String>allowedHeaders) { this.allowedHeaders=allowedHeaders; } publicList<String>getAllowedMethods() { returnallowedMethods; } publicvoidsetAllowedMethods(List<String>allowedMethods) { this.allowedMethods=allowedMethods; } publicList<String>getExposedHeaders() { returnexposedHeaders; } publicvoidsetExposedHeaders(List<String>exposedHeaders) { this.exposedHeaders=exposedHeaders; } publicLonggetMaxAge() { returnmaxAge; } publicvoidsetMaxAge(LongmaxAge) { this.maxAge=maxAge; } publicBooleangetAllowCredentials() { returnallowCredentials; } publicvoidsetAllowCredentials(BooleanallowCredentials) { this.allowCredentials=allowCredentials; } publicStringtoString() { returnMoreObjects.toStringHelper(this) .add("enabled", enabled) .add("pathPattern", pathPattern) .add("allowedOrigins", allowedOrigins) .add("allowedHeaders", allowedHeaders) .add("allowedMethods", allowedMethods) .add("exposedHeaders", exposedHeaders) .add("maxAge", maxAge) .add("allowCredentials", allowCredentials) .toString(); } }
编写自动配置类
MagicalCorsAutoConfiguration
importorg.jeckxu.magical.core.boot.props.MagicalCorsProperties; importorg.jeckxu.magical.core.launch.destroy.MagicalDestroying; importorg.springframework.boot.autoconfigure.condition.ConditionalOnProperty; importorg.springframework.context.annotation.Bean; importorg.springframework.context.annotation.Configuration; importorg.springframework.web.cors.CorsConfiguration; importorg.springframework.web.cors.UrlBasedCorsConfigurationSource; importorg.springframework.web.filter.CorsFilter; /*** @author xuchangjiang*/value=MagicalCorsProperties.PRE_FIX+".enabled", havingValue="true", matchIfMissing=true) (publicclassMagicalCorsAutoConfigurationimplementsMagicalDestroying { privatefinalMagicalCorsPropertiesmagicalCorsProperties; publicMagicalCorsAutoConfiguration(MagicalCorsPropertiesmagicalCorsProperties) { this.magicalCorsProperties=magicalCorsProperties; } /*** 默认跨域过滤器* @return*/publicCorsFiltercorsFilter() { CorsConfigurationcorsConfiguration=newCorsConfiguration(); magicalCorsProperties.getAllowedOrigins().forEach(corsConfiguration::addAllowedOrigin); magicalCorsProperties.getExposedHeaders().forEach(corsConfiguration::addExposedHeader); magicalCorsProperties.getAllowedHeaders().forEach(corsConfiguration::addAllowedHeader); magicalCorsProperties.getAllowedMethods().forEach(corsConfiguration::addAllowedMethod); corsConfiguration.setAllowCredentials(magicalCorsProperties.getAllowCredentials()); UrlBasedCorsConfigurationSourcecorsConfigurationSource=newUrlBasedCorsConfigurationSource(); corsConfigurationSource.registerCorsConfiguration(magicalCorsProperties.getPathPattern(), corsConfiguration); returnnewCorsFilter(corsConfigurationSource); } }
2,注意点:
不同Spring Boot版本的自动配置类MagicalCorsAutoConfiguration中的@Configuration注解参数不同。Spring Boot 2.1.x以及之前的版本为:
Spring Boot 2.2.x以及之后的版本为
proxyBeanMethods=false) (
以上配置项适用于Spring boot 2.0.x 到 2.3.x