Spring CORS
简介
解决跨域可以引入如下代码
配置
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35
| import org.springframework.context.annotation.Configuration; import org.springframework.web.servlet.config.annotation.CorsRegistry; import org.springframework.web.servlet.config.annotation.WebMvcConfigurer; import org.springframework.boot.web.servlet.FilterRegistrationBean; import org.springframework.context.annotation.Bean; import org.springframework.core.Ordered; import org.springframework.web.cors.CorsConfiguration; import org.springframework.web.cors.UrlBasedCorsConfigurationSource; import org.springframework.web.filter.CorsFilter;
@Configuration public class WebConfig implements WebMvcConfigurer { @Override public void addCorsMappings(CorsRegistry registry) { registry.addMapping("/**") .allowedOrigins("*") .allowedMethods("*") .allowedHeaders("*") .exposedHeaders("Access-Control-Allow-Origin"); }
@Bean public FilterRegistrationBean<CorsFilter> corsFilterRegistrationBean() { UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource(); CorsConfiguration config = new CorsConfiguration(); config.addAllowedOrigin("*"); config.addAllowedMethod("*"); config.addAllowedHeader("*"); source.registerCorsConfiguration("/**", config); FilterRegistrationBean<CorsFilter> bean = new FilterRegistrationBean<>(new CorsFilter(source)); bean.setOrder(Ordered.HIGHEST_PRECEDENCE); return bean; } }
|
如果使用了 SpringSecurity 还需要进行额外的配置,样例如下:
1 2 3 4 5 6 7 8 9
| import org.springframework.security.config.annotation.web.configurers.AbstractHttpConfigurer;
@Bean SecurityWebFilterChain springSecurityFilterChain(ServerHttpSecurity http) { http .cors(AbstractHttpConfigurer::disable); return http.build(); }
|
测试
使用如下命令即可:
1
| curl -X 'GET' 'http://localhost:8080/test' -H 'accept: */*' -H 'origin:*' -v
|
若返回头中包含如下内容则证明配置成功:
1
| < Access-Control-Allow-Origin: *
|
注:在请求时必须加入 origin
头,否则不会返回 Access-Control-Allow-Origin