跨域资源共享CORS(Cross-origin Resource Sharing),是W3C的一个标准,允许浏览器向跨源的服务器发起 HttpRequest请求,克服ajax请求只能同源使用的限制。关于CORS的详细解读,可参考阮一峰大神的博客:跨域资源共享CORS详解。
1. 遇到的问题:
我用spring-boot 做Rest服务,Vue做前端框架,用了element-admin-ui这个框架做后台管理。在调试的过程中遇到了如下错误:
Preflight response is not successful
2. 分析问题
这个问题是典型的CORS跨域问题。
所谓跨域:
跨域,指的是浏览器不能执行其他网站的脚本。它是由浏览器的同源策略造成的,是浏览器对 施加的安全限制。
3. 解决方法
在项目中添加类CustomCORSConfiguration 代码如下:
import org.spring work.context.annotation.Bean;
import org.spring work.context.annotation.Configuration;
import org.spring work.web.cors.CorsConfiguration;
import org.spring work.web.cors.Url dCorsConfigurationSource;
import org.spring work.web.filter.CorsFilter;
/**
* @author spartajet
* @de ion
* @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() {
Url dCorsConfigurationSource source = new Url dCorsConfigurationSource();
source.registerCorsConfiguration("/**", buildConfig());
return new CorsFilter(source);
}
}
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。
继续阅读与本文标签相同的文章
上一篇 :
欧盟本月或再次重罚谷歌,要求改变安卓运营方式
下一篇 :
jQuery实现基本隐藏与显示效果的方法详解
-
面向海量数据的极致成本优化-云HBase的一体化冷热分离
2026-05-18栏目: 教程
-
地球如果流浪,大数据究竟能做什么?
2026-05-18栏目: 教程
-
网站漏洞测试 sql注入攻击代码的审计与检测
2026-05-18栏目: 教程
-
【DockerCon2017技术解读】使用Moby工具和Linuxkit定制容器系统
2026-05-18栏目: 教程
-
如何设置开机启动holer
2026-05-18栏目: 教程
