全局通过配置CorsFilter(跨域请求过滤器)实现支持跨域
@Configuration
public class GlobalCorsConfig {
@Bean
public CorsFilter corsFilter() {
final UrlBasedCorsConfigurationSource urlBasedCorsConfigurationSource = new UrlBasedCorsConfigurationSource();
final CorsConfiguration corsConfiguration = new CorsConfiguration();
corsConfiguration.setAllowCredentials(true);
corsConfiguration.addAllowedOrigin("*");
corsConfiguration.addAllowedHeader("*");
corsConfiguration.addAllowedMethod("*");
urlBasedCorsConfigurationSource.registerCorsConfiguration("/**", corsConfiguration);
return new CorsFilter(urlBasedCorsConfigurationSource);
}
}
单个请求的跨域支持通过 @CrossOrigin 注解来实现
@RequestMapping("/hello")
@CrossOrigin("http://localhost:8080")
public String hello( ){
return "Hello World";
}