Spring boot处理OPTIONS请求
一、现象
从fetch说起,用fetch构造一个POST请求。
fetch('http://127.0.0.1:8000/api/login', {
method: "POST",
headers: ({
'Content-Type': 'application/x-www-form-urlencoded'
}),
body: "name=" + name + "&password=" + pwd
}).then((res) = >{
console.log(res.status);
return res.json()
}).then((data) = >{
// console.log(data.result)
let loginResult = data.result
if (loginResult == 'ok') {
dispatch(getSuccess(data.list)) browserHistory.push('/index')
} else {
console.log("illegal login in !")
}
}).
catch((e) = >{
console.log(e.message)
})
调用的API服务是Spring boot开发的。
这个POST发出去,一切正常。
由于业务需要,我增加一个头字段:Authorization。
fetch请求的代码修改如下:
... headers: ({
'Content-Type': 'application/x-www-form-urlencoded',
'Authorization': ''
}),
body: "name=" + name + "&password=" + pwd
}).then((res) = >{ ...
问题出现了,服务器收到一个OPTIONS请求?!
二、原因
这是fetch出于安全性考虑做的一次服务器预查询,而我的服务没有做相应的处理,所以业务处理失败了。
三、解决
方法一:
网上查到的最多的说法就是:
在application.properties文件中增加这一行配置:
spring.mvc.dispatch-options-request=true
遗憾的是,我的环境配置没有生效。
方法二:
手动写一个Filter:
0 @Component
1 public class CorsFilter implements Filter {
@Override
public void init(FilterConfig filterConfig) throws ServletException {
// TODO Auto-generated method stub
} @Override
public void doFilter(ServletRequest req, ServletResponse res,
FilterChain chain) throws IOException, ServletException {
HttpServletResponse response = (HttpServletResponse) res;
response.setHeader("Access-Control-Allow-Origin", "*");
response.setHeader("Access-Control-Allow-Methods",
"POST, GET, OPTIONS, DELETE");
response.setHeader("Access-Control-Max-Age", "");
response.setHeader("Access-Control-Allow-Headers",
"Content-Type, x-requested-with, X-Custom-Header, Authorization");
chain.doFilter(req, res);
} @Override
public void destroy() {
// TODO Auto-generated method stub
}
}
一点说明:
response.setHeader("Access-Control-Allow-Headers", "Content-Type, x-requested-with, X-Custom-Header, Authorization");
配置中的Authorization是和请求中自定义的头部字段是一样的。
通过这个过滤器,fetch后续的POST请求就可以顺利的发出了。
Spring boot处理OPTIONS请求的更多相关文章
- 【spring boot】spring boot 前台GET请求,传递时间类型的字符串,后台无法解析,报错:Failed to convert from type [java.lang.String] to type [java.util.Date]
spring boot 前台GET请求,传递时间类型的字符串,后台无法解析,报错:Failed to convert from type [java.lang.String] to type [jav ...
- spring boot 跨域请求
场景 网站localhost:56338要访问网站localhost:3001的服务 在网站localhost:3001中增加CORS相关Java Config @Configuration @Ord ...
- 踩坑记录:spring boot的POST请求数据注入不了的问题
概述: 今天在使用spring boot框架的时候,踩了一个坑,是关于control层request body依赖注入的问题的,内容如下: 进过: 由于目前公司采用的系统架构,要求把springboo ...
- spring boot如何处理异步请求异常
springboot自定义错误页面 原创 2017年05月19日 13:26:46 标签: spring-boot 方法一:Spring Boot 将所有的错误默认映射到/error, 实现Err ...
- spring boot跨域请求访问配置以及spring security中配置失效的原理解析
一.同源策略 同源策略[same origin policy]是浏览器的一个安全功能,不同源的客户端脚本在没有明确授权的情况下,不能读写对方资源. 同源策略是浏览器安全的基石. 什么是源 源[orig ...
- Spring boot AOP 记录请求日志
如何将所有的通过url的请求参数以及返回结果都输出到日志中? 如果在controller的类中每个方法名都写一个log输出肯定是不明智的选择. 使用spring的AOP功能即可完成. 1. 在pom. ...
- Spring Boot 简单的请求示例(包括请求体验证)
1.先做个最简单的Get请求 新建一个Controller , 并给他添加注解@RestController 它是@Controller和@ResponseBody的组合注解,告诉Spring我是一个 ...
- spring boot 请求地址带有.json 兼容处理
项目以前时spring mvc的,现在升级为spring boot ,有些请求地址带有.json后缀,在请求spring boot项目时,无法匹配控制器,spring boot默认选择禁用后缀模式匹配 ...
- Spring Boot 异步请求和异步调用,一文搞定
一.Spring Boot中异步请求的使用 1.异步请求与同步请求 特点: 可以先释放容器分配给请求的线程与相关资源,减轻系统负担,释放了容器所分配线程的请求,其响应将被延后,可以在耗时处理完成(例如 ...
随机推荐
- Visual Tree
1.GetVisualParent public static T GetVisualParent<T>(DependencyObject obj) where T : Dependenc ...
- [转]How to query posts filtered by custom field values
Description It is often necessary to query the database for a list of posts based on a custom field ...
- jquery制作一个简单的轮播
效果图: 演示地址: http://ae6623.cn/demo/slider/index.html 思路: 利用css的定位属性 left 进行调整图片的显示,每次点击上一页下一页按钮的时候,-图片 ...
- SharpDevelop 版本信息
mscorlib,4.0.0.0,C:\Windows\Microsoft.NET\Framework\v4.0.30319\mscorlib.dll SharpDevelop,5.1.0.4936, ...
- [jQuery] $.map, $.each, detach() , $.getJSOIN()
$.map function will return the modifies array. $.each function will not new a new array, the old val ...
- 如何使用Total Recorder录制网上的音乐,如何下载只能试听的歌曲
1 在网上找到了对应的网站.其中正在播放的歌曲正是我们想要的 2 在地址栏输入上面音乐网站的网址,并点击捕获广播.(URL直接给出了音乐的完整地址,比如http://www.someserver.co ...
- Android动态加入控件约束位置
用LayoutParams: RelativeLayout insertLayout = (RelativeLayout)view1.findViewById(R.id.screen);//scree ...
- Excel只能输入不许修改
昨天出差,用户有个需求:需要他人录入数据,为了防止已经录入的数据被修改(录入数据由多方完成),必须限定只能输入,不能修改(修改时需要密码),最开始的方案是各设置一个密码,但如果他们相互之间联合 ...
- 嵌入式Linux的web视频服务器的构建
http://blog.sina.com.cn/s/blog_53d02d550102v8bu.html随着嵌入式处理器和开源Linux 的广泛应用,各种视频服务在嵌入式系统中逐渐发展起来. 1.引言 ...
- OpenERP 的XML-RPC的轻度体验+many2many,one2many,many2one创建方式
来自:http://cn.openerp.cn/openerp_import_image_by_xmlrpc/ 每当夏秋之交,我们都有展会,展会完后,都有很多的新的潜在客户要添加,我们收了一大堆名片, ...