用springmvc 开发为 app 提供后台服务遇到跨域请求的问题
瀏覽器使用的chrome,安裝了cors插件(開啟允許跨域請求)
然后訪問遠程服務(wù)器提供的服務(wù)的時候,瀏覽器console 輸出如下形式的錯誤信息:
問題1.OPTION: xxxxxx url地址
問題2.XMLHttpRequest cannot load :xxxxx url 地址
Request header field xxxx is not allowed by Access-Control-Allow-Headers
----------
OPTIONS方法是用于請求獲得由Request-URI標(biāo)識的資源在請求/響應(yīng)的通信過程中可以使用的功能選項。
通過這個方法,客戶端可以在采取具體資源請求之前,決定對該資源采取何種必要措施,或者了解服務(wù)器的性能。
如果想詳細了解ajax跨域請求,可以參考這位作者的三篇文章,It's awesome!
詳解AJAX跨域請求:
http://blog.csdn.net/net_lover/article/details/5172509
http://blog.csdn.net/net_lover/article/details/5172522?
http://blog.csdn.net/net_lover/article/details/5172532
---------------
瀏覽器在發(fā)送真正的http請求之前會發(fā)送OPTION類型的請求,執(zhí)行預(yù)檢,檢查服務(wù)器支持的請求類型,
對請求頭允許的特殊說明,得到服務(wù)器的批準(zhǔn),允許才發(fā)送實際的請求,服務(wù)器也可以向瀏覽器發(fā)送通知,
知會瀏覽器是否需要在請求中向服務(wù)器傳遞某些認(rèn)證信息(cookie,或者Authentication認(rèn)證數(shù)據(jù)),
下面的圖形象描述了這個過程:
?
?來自維基百科:https://en.wikipedia.org/wiki/Cross-origin_resource_sharing
別的不多說,就是說我們需要在服務(wù)器端允許,OPTION 類型的請求,可以解決問題1;
在服務(wù)器端設(shè)置允許的 源、請求頭包含信息、允許的方法,deviceName,deviceID ?是我的項目需要的,根據(jù)您的需要去設(shè)置,
'Access-Control-Allow-Origin': '*',
'Access-Control-Allow-Headers': 'Origin, X-Requested-With, Content-Type, Accept,deviceName,deviceID',
'Access-Control-Allow-Methods': 'GET, POST, PUT',
---------------
spring中的設(shè)置方法:
web.xml中做如下配置:
<security-constraint> <web-resource-collection> <url-pattern>/*</url-pattern> <http-method>PUT</http-method> <http-method>DELETE</http-method> <http-method>HEAD</http-method> <!--<http-method>OPTIONS</http-method>--> <!-- 注意需要去掉的就是這行,解決問題1 --> <http-method>TRACE</http-method> </web-resource-collection> <auth-constraint> </auth-constraint> </security-constraint> <login-config> <auth-method>BASIC</auth-method> </login-config> <!-- rest cors request filter 解決問題2--><filter><display-name>RequestHeaderFilter</display-name><filter-name>RequestHeaderFilter</filter-name><filter-class>com.xxx.filter.RequestHeaderFilter</filter-class></filter><filter-mapping><filter-name>RequestHeaderFilter</filter-name><url-pattern>/*</url-pattern></filter-mapping>新增一個RequestHeaderFilter:
public final class RequestHeaderFilter implements Filter {/*** Default constructor.*/public RequestHeaderFilter() {}/*** @see Filter#destroy()*/public void destroy() {}/*** @see Filter#doFilter(ServletRequest, ServletResponse, FilterChain)*/public void doFilter(ServletRequest request, ServletResponse response,FilterChain chain) throws IOException, ServletException {// 設(shè)置允許跨域訪問HttpServletResponse httpServletResponse = (HttpServletResponse)response;httpServletResponse.setHeader("Access-Control-Allow-Origin", "*");// 允許 header中包括 deviceName,deviceIDhttpServletResponse.setHeader("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept,deviceName,deviceID");chain.doFilter(request, httpServletResponse);}/*** @see Filter#init(FilterConfig)*/public void init(FilterConfig fConfig) throws ServletException {}}?
至此,配置修改完畢,it works great.
不過,最后提醒一句:以上方式只是在開發(fā)測試的時候,為了方便才這么做的,安全原因,生產(chǎn)環(huán)境強烈不推薦。
謝謝大家~
轉(zhuǎn)載于:https://www.cnblogs.com/mengyehongmanshan/p/5670904.html
總結(jié)
以上是生活随笔為你收集整理的用springmvc 开发为 app 提供后台服务遇到跨域请求的问题的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: sql2005主从数据库同步配置
- 下一篇: 关于CAShapeLayer的一些实用案