spring boot 跨域请求
场景
网站localhost:56338要访问网站localhost:3001的服务
在网站localhost:3001中增加CORS相关Java Config
@Configuration
@Order(Ordered.HIGHEST_PRECEDENCE)
public class CORSConfiguration {
@Bean
public WebMvcConfigurer corsConfigurer() {
return new WebMvcConfigurerAdapter() {
@Override
public void addCorsMappings(CorsRegistry registry) {
registry.addMapping("/**")
.allowedHeaders("*")
.allowedMethods("*")
.allowedOrigins("*")
.allowCredentials(true)
.maxAge(3600);
}
};
}
}
在网站localhost:56338中测试
@section Scripts{
<script>
$.ajax({
method: "GET",
url: "http://localhost:3001/api/project/getProjectInfo/50",
headers: {
'x-auth-token':'5136E8DD7D4AFCA77FDA3A4B4541A7FB88609FCFF1C0E9C2BC0060A3DBBB14E08F9B107336A218A62C247618BBF759312653BD70E4CEDAE86D285C8D459490728B008383929E8262CB40C9B0E8C841F3531E61F01FE71A937820176D45D348CE7C375D7020B8191A0190C46A318859C6'
},
xhrFields: {
withCredentials: true
}
}).done(function (resp) {
console.log(resp);
}).fail(function () {
}).always(function () {
});
</script>
}
备注
- 在使用过程中发现,有时可能不能正常实现跨域,原因是spring bean注册顺序的问题,所以一定要在配置上增加 @Order(Ordered.HIGHEST_PRECEDENCE), 保证它优先注册。
- 如果客户端需要带上cookie,需要增加withCredentials, vuejs, angularjs,ractjs上的设置方法要进一步研究一下。
参考资料
- 重拾后端之Spring Boot(五) -- 跨域、自定义查询及分页
- HTTP访问控制(CORS)
- 解决angular+spring boot的跨域问题
- Spring Boot设置跨域访问
- rest-service-cors
- CORS not working?
- localstorage的跨域存储方案
- Spring Boot HTTP over JSON 的错误码异常处理
完整的WebConfig代码
@Configuration
@Order(Ordered.HIGHEST_PRECEDENCE)
public class WebConfig extends WebMvcConfigurerAdapter {
@Autowired
private AuthorizationInterceptor authorizationInterceptor;
@Override
public void addInterceptors(InterceptorRegistry registry) {
registry.addInterceptor(authorizationInterceptor);
}
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler("/static/**")
.addResourceLocations("classpath:/static/");
}
// @Bean
// public FilterRegistrationBean corsFilter() {
// UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
// CorsConfiguration config = new CorsConfiguration();
// config.setAllowCredentials(true);
// config.addAllowedOrigin("*");
// config.addAllowedHeader("*");
// config.addAllowedMethod("*");
// config.setMaxAge(3600L);
// source.registerCorsConfiguration("/api/**", config);
// FilterRegistrationBean bean = new FilterRegistrationBean(new CorsFilter(source));
// // 这个顺序很重要哦,为避免麻烦请设置在最前
// bean.setOrder(0);
// return bean;
// }
@Override
public void addCorsMappings(CorsRegistry registry) {
registry.addMapping("/api/**")
.allowedOrigins("*")
.allowedMethods("GET", "POST", "OPTIONS")
.allowedHeaders("*")
.allowCredentials(true)
.maxAge(3600);
}
}
“年轻人不要怕表现,要敢于出来表现,但还是那句话,要有正确的度,你的表现是分析问题和解决问题的能力。”
– 《你凭什么做好互联网》
spring boot 跨域请求的更多相关文章
- spring boot跨域请求访问配置以及spring security中配置失效的原理解析
一.同源策略 同源策略[same origin policy]是浏览器的一个安全功能,不同源的客户端脚本在没有明确授权的情况下,不能读写对方资源. 同源策略是浏览器安全的基石. 什么是源 源[orig ...
- Spring处理跨域请求
[nio-8080-exec-8] o.s.web.cors.DefaultCorsProcessor : Skip CORS processing: request is from s ...
- spring boot跨域设置
定义 跨域是指从一个域名的网页去请求另一个域名的资源 跨域背景 限制原因 如果一个网页可以随意地访问另外一个网站的资源,那么就有可能在客户完全不知情的情况下出现安全问题 为什么要跨域 公司内部有多个不 ...
- spring boot 跨域访问处理
问题场景:由于项目中使用到跨域访问,今天也得到高人指点,所以写出来分享给大家.可能是考虑到前后端分离,前端后端服务器不在一台机器上,出现这种跨域访问的情况.正常情况下本地访问是没有问题,但是遇到这种非 ...
- SPRING BOOT跨域访问处理
尊重原创:http://blog.csdn.net/ruiguang21/article/details/77878933 问题场景:由于项目中使用到跨域访问,今天也得到高人指点,所以写出来分享给大家 ...
- spring boot跨域问题
跨域是指不同域名之间相互访问.跨域,指的是浏览器不能执行其他网站的脚本.它是浏览器的同源策略造成的,是浏览器对JavaScript施加的安全限制.也就是如果在A网站中,我们希望使用Ajax来获得B网站 ...
- spring boot跨域问题的简便解决方案
刚学spring boot的时候被跨域问题拦住好久,最终好不容易从网上抄了别人的极端代码才解决. 但是前些天看一同事的代码时,发现spring boot中用注解就可以解决. 在controller上添 ...
- Spring Boot 跨域访问
如何在 Spring Boot 中配置跨域访问呢? Spring Boot 提供了对 CORS 的支持,您可以实现WebMvcConfigurer 接口,重写addCorsMappings 方法来添加 ...
- spring boot 跨域问题
public class WebMvcConfig implements WebMvcConfigurer { @Override public void addInterceptors(Inter ...
随机推荐
- jquery制作移动端菜单栏左右滑动
//菜单栏滑动function move_scollX(){ var startPosition, endPosition, distanceX,distanceY; $(".left&qu ...
- JavaScript日期排序
//日期排序 function sortDownDate(a, b) { return Date.parse(a.received) - Date.parse(b.received); } funct ...
- Azure Ubuntu18.04安装lxde桌面记录,Windows远程连接Ubuntu18.04(Linux)
执行如下命令: 尽量按以下顺序执行,否则可能会发生意向不到的问题(坑) 1.更新数据源 sudo apt-get update 2.更新安装包 sudo apt-get upgrade 3.安装lxd ...
- 简述 Spring Cloud 是什么2
一.概念定义 Spring Cloud是一个微服务框架,相比Dubbo等RPC框架, Spring Cloud提供的全套的分布式系统解决方案. Spring Cloud对微服务基础框架Ne ...
- char/varchar/nvarchar的区别
原文:https://blog.csdn.net/w516162189/article/details/78914035 我们在设计数据库的时候,需要根据需求场景选择合适的字段类型,对数据的执行效率有 ...
- oracle FLASHBACK TABLE
闪回表 -- 开启行迁移 ALTER TABLE employees_test ENABLE ROW MOVEMENT; UPDATE employees_test SET salary = sala ...
- Head First Android --- Enable USB debugging on your device
1. Enable USB debugging on your device On your device, open “Developer options” (in Android 4.0 o ...
- orcale 使用创建日期排序然后分页每次取排序后的固定条数
需求: 一个使用mybatis分页插件的分页列表, 现在要求新增一条数据或者修改一条数据后,显示在最前端 思路: 使用sql , 先将查询出来的数据排序, 然后使用rownum > page*s ...
- VRS待解决的问题——原因及解决方案
1.持续滤波失败(查看文档) 通过查看文档及代码 2.GAL卫星数为0的网元及原因 3.判断发的是否是单个基站(网元未固定),多个用户进行测试 4.网元固定率(采用文件输出) 5.是否频繁重复初始化 ...
- Beta冲刺! Day2 - 砍柴
Beta冲刺! Day2 - 砍柴 今日已完成 晨瑶:大致确定了文章推荐的算法思路(Content-based recommender):理清了不少feature的事宜 昭锡:修复了日期选择越界时导致 ...