脚本宝典收集整理的这篇文章主要介绍了Spring boot 和Vue开发中CORS跨域问题,脚本宝典觉得挺不错的,现在分享给大家,也给大家做个参考。
我用sPRing-boot
做Rest
服务,Vue
做前端框架,用了element-admin-ui
这个框架做后台管理。在调试的过程中遇到了如下错误:
Preflight response is not successful
这个问题是典型的CORS
跨域问题。
所谓跨域:
跨域,指的是浏览器不能执行其他网站的脚本。它是由浏览器的同源策略造成的,是浏览器对JavaScript施加的安全限制。
在项目中添加类CustomCORSconfiguration
代码如下:
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;
/**
* @author spartajet
* @description
* @create 2018-05-15 下午5:00
* @email spartajet.guo@gmail.COM
*/
@Configuration
public class CustomCORSConfiguration {
private CorsConfiguration buildConfig() {
CorsConfiguration corsConfiguration = new CorsConfiguration();
corsConfiguration.addAllowedOrigin("*");
corsConfiguration.addAllowedHeader("*");
corsConfiguration.addAllowedMethod("*");
corsConfiguration.setAllowCredentials(true);
return corsConfiguration;
}
@Bean
public CorsFilter corsFilter() {
UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
source.registerCorsConfiguration("/**", buildConfig());
return new CorsFilter(source);
}
}
以上是脚本宝典为你收集整理的Spring boot 和Vue开发中CORS跨域问题全部内容,希望文章能够帮你解决Spring boot 和Vue开发中CORS跨域问题所遇到的问题。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。