The cross-domain configuration that I configured in other modules took effect, but it did not take effect here. I fiddled around and it turned out to be a problem with the file name. It is estimated that the corresponding bean is configured at the bottom of springboot and has the name CORSConfig.
1. My cross-domain configuration
@Configuration
public class CORSConfig {
private CorsConfiguration buildConfig() {
CorsConfiguration corsConfiguration = new CorsConfiguration();
corsConfiguration.addAllowedHeader("*"); // 设置访问源请求头
corsConfiguration.addAllowedMethod("GET"); // 允许GET方法访问
corsConfiguration.addAllowedMethod("POST"); // 允许POST方法访问
corsConfiguration.addAllowedMethod("OPTIONS"); // 允许OPTIONS方法访问
corsConfiguration.setAllowCredentials(Boolean.TRUE);
// 2.4以后这样设置 替换 addAllowedOrigin("*)
corsConfiguration.setAllowedOriginPatterns(Arrays.asList(CorsConfiguration.ALL));
corsConfiguration.setMaxAge(3600L);
return corsConfiguration;
}
@Bean
public CorsFilter corsFilter() {
UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
source.registerCorsConfiguration("/**", buildConfig()); // 4 对接口配置跨域设置
return new CorsFilter(source);
}
}
2. Abnormal causes
Just change the file name CORSConfig to another name, such as PASSCORSConfig,
ACORSConfig,
3. Summary
The name is CORSConfig / GlobalCORSConfig. Cross-domain files sometimes cause problems.
4.Normal config
package cn.wisegraph.purchasems.config;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.cors.CorsConfiguration;
import org.springframework.web.cors.UrlBasedCorsConfigurationSource;
import org.springframework.web.filter.CorsFilter;
import java.util.Arrays;
@Configuration
public class PassCorsConfig {
private CorsConfiguration buildConfig() {
CorsConfiguration corsConfiguration = new CorsConfiguration();
corsConfiguration.addAllowedHeader("*"); // 设置访问源请求头
corsConfiguration.addAllowedMethod("GET"); // 允许GET方法访问
corsConfiguration.addAllowedMethod("POST"); // 允许POST方法访问
corsConfiguration.addAllowedMethod("OPTIONS"); // 允许OPTIONS方法访问
corsConfiguration.setAllowCredentials(Boolean.TRUE);
// 2.4以后这样设置 替换 addAllowedOrigin("*)
corsConfiguration.setAllowedOriginPatterns(Arrays.asList(CorsConfiguration.ALL));
corsConfiguration.setMaxAge(3600L);
return corsConfiguration;
}
@Bean
public CorsFilter corsFilter() {
UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
source.registerCorsConfiguration("/**", buildConfig()); // 4 对接口配置跨域设置
return new CorsFilter(source);
}
}