Angularjs 弹簧靴-cors不工作
我的服务器运行在localhost:8080(spring boot app)上,前端运行在localhost:3000(angular app)上。问题是我想从前端向后端服务器发出请求。我熟悉cors,但对我来说不起作用。我已将此添加到我的服务器:Angularjs 弹簧靴-cors不工作,angularjs,spring,cors,Angularjs,Spring,Cors,我的服务器运行在localhost:8080(spring boot app)上,前端运行在localhost:3000(angular app)上。问题是我想从前端向后端服务器发出请求。我熟悉cors,但对我来说不起作用。我已将此添加到我的服务器: @Bean WebMvcConfigurer corsConfigurer() { return new WebMvcConfigurerAdapter() { @Override public vo
@Bean
WebMvcConfigurer corsConfigurer() {
return new WebMvcConfigurerAdapter() {
@Override
public void addCorsMappings(CorsRegistry registry) {
registry.addMapping("/**").allowedOrigins("http://localhost:3000");
}
};
}
,但我还是得到了
无法加载XMLHttpRequest。请求已重定向到“”,这对于需要飞行前处理的跨源请求是不允许的
怎么了?根据官方博客文章,如果你已经启用,你必须使用
根据官方博客文章,如果你已经启用,你必须使用
@Configuration
public class MyConfiguration {
@Bean
public FilterRegistrationBean corsFilter() {
UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
CorsConfiguration config = new CorsConfiguration();
config.setAllowCredentials(true);
config.addAllowedOrigin("http://domain1.com");
config.addAllowedHeader("*");
config.addAllowedMethod("*");
source.registerCorsConfiguration("/**", config);
FilterRegistrationBean bean = new FilterRegistrationBean(new CorsFilter(source));
bean.setOrder(0);
return bean;
}
}