http://docs.spring.io/spring/docs/current/spring-framework-reference/html/cors.html
文档看似很清晰的描述了如何在Spring 4.2之后启用cors跨域访问,网上搜索介绍这样的帖子也不少。也提到了说什么如果用了Spring Security的话要采用filter的方式来配置。下面这段话就是官方文档
In order to support CORS with filter-based security frameworks like Spring Security, or with other libraries that do not support natively CORS, Spring Framework also provides a CorsFilter. Instead of using @CrossOrigin or WebMvcConfigurer#addCorsMappings(CorsRegistry), you need to register a custom filter defined like bellow:
123456789101112131415161718192021 import org.springframework.web.cors.CorsConfiguration;import org.springframework.web.cors.UrlBasedCorsConfigurationSource;import org.springframework.web.filter.CorsFilter;public class MyCorsFilter extends CorsFilter {public MyCorsFilter() {super(configurationSource());}private static UrlBasedCorsConfigurationSource configurationSource() {CorsConfiguration config = new CorsConfiguration();config.setAllowCredentials(true);config.addAllowedOrigin("http://domain1.com");config.addAllowedHeader("*");config.addAllowedMethod("*");UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();source.registerCorsConfiguration("/**", config);return source;}}
在经过测试之后,实在是无论采用哪一种方式都不行,实在是太费解了,debug了半天跨域的时候GET方法根本连DispatchServlet都不进去,POST方法倒是可以跨域,发现POST请求是根据header的origin来判断是否跨域。
还是想着从Spring Security这边来入手,结果就发现HttpSecurity类提供了这么一个方法。
|
|
我抱着试一试的心态,加上了这句话代码。
|
|
就是.cors().and()
这句了,然后还是采用addCorsMappings
方法来配置。
|
|
结果当然是成功了,Spring的文档也老是跟不上节奏,还是需要自己多探索和思考。希望能帮到遇到这个问题的朋友们。