sentinel流控时,web的OPTIONS请求跨域问题
添加HandlerInterceptor、WebMvcConfigurer.addCorsMappings层级的跨域配置都不会生效
原因在于sentinel流控执行的声明周期高于以上层级,需要按如下配置CorsFilter层级的跨域设置
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;
@Configuration
public class CustomCORSConfiguration {
private CorsConfiguration buildConfig(){
CorsConfiguration corsConfiguration = new CorsConfiguration();
corsConfiguration.addAllowedHeader("*"); // 允许任何的head头部
corsConfiguration.addAllowedOrigin("*"); // 允许任何域名使用
corsConfiguration.addAllowedMethod("*"); // 允许任何的请求方法
corsConfiguration.setAllowCredentials(true);
return corsConfiguration;
}
// 添加CorsFilter拦截器,对任意的请求使用
@Bean
public CorsFilter corsFilter() {
UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
source.registerCorsConfiguration("/**", buildConfig());
return new CorsFilter(source);
}
}
网友评论