问题1:浏览器页面跨域请求报错
相信很多程序员都遇到过跨域的问题,如何解决一直是一件很头疼的事情,我就描述下我遇到的问题,及解决办法。什么是跨域,请自行脑补。
需要添加maven依赖,在web.xml文件里配置过滤器:
com.thetransactioncompany
cors-filter
2.6
CORS
com.thetransactioncompany.cors.CORSFilter
cors.allowGenericHttpRequests
true
cors.allowOrigin
*
cors.allowSubdomains
false
cors.supportedMethods
GET,HEAD,POST,DELETE,OPTIONS,PUT
cors.supportedHeaders
content-type,token, authorization,Accept, Origin, X-Requested-With, Content-Type, Last-Modified,Access-Control-Allow-Headers
cors.supportsCredentials
true
cors.maxAge
3600
cors.tagRequests
true
cors.allowSubdomains
true
cors.exposedHeaders
Set-Cookie
CORS
*
有的同学可能配置了,依然存在跨域问题,让我们了解下跨域的options请求:
浏览器向目标服务器发送了OPTION请求并携带access-control-request-method和access-control-request-headers告诉目标服务器它的请求方式和所要发送的头信息,检测目标服务器是否准许,示例如下:
Request Method: OPTIONS
access-control-request-headers: content-type,cookies
access-control-request-method: POST
这两个头信息很重要,access-control-request-method这个不用说了,是请求的http方法,重点看access-control-request-headers,后面的值一定要在上面的过滤器里的cors.supportedHeaders的值里包含,否则就会存在跨域问题。
参考资料:http://www.04007.cn/article/752.html
问题2:浏览器跨域请求不报跨域错,但是取不到后台返回的Response里的header信息
原因是:后台返回的Response里没有添加这个头: Access-Control-Expose-Headers: Content-Disposition ,多个值逗号分隔