2015-04-02 10 views
7

我有一個tomcat 8服務器,啓用了web.xml中的CORS。 的CORS插件工作在大多數情況下,但有時它混合了從本地主機和服務器主機頭請求CORS啓用但請求頭從不同的主機混合起來

XMLHttpRequest cannot load http://mipldevlinux7:6060/juneberry/data/blue-marbles/config.json. The 'Access-Control-Allow-Origin' header has a value 'http://localhost:3000' that is not equal to the supplied origin. Origin 'http://mipldevlinux7:7777' is therefore not allowed access 

我的Tomcat服務器是在端口6060稱爲mipldevlinux7服務器上,我有一個生產服務器在端口7777的同一臺主機上。

我在localhost:3000上做我的開發,我的同事在localhost:8080上運行他的開發服務器。

我們得到了CORS錯誤,並且錯誤會將我們本地主機之間的標頭混合到3000,有時還會混合8080。有時我們甚至會得到mipledevlinux7:7777的標題請求,爲什麼我們要從本地主機請求。

我使用的CORS是CORS tomcat的8構建提供:

<filter> 
    <filter-name>CorsFilter</filter-name> 
    <filter-class>org.apache.catalina.filters.CorsFilter</filter-class> 
    <init-param> 
     <param-name>cors.allowed.origins</param-name> 
     <param-value>*</param-value> 
    </init-param> 
    <init-param> 
     <param-name>cors.allowed.methods</param-name> 
     <param-value>GET,POST,HEAD,OPTIONS,PUT</param-value> 
    </init-param> 
    <init-param> 
     <param-name>cors.allowed.headers</param-name> 
     <param-value>Content-Type,X-Requested-With,accept,Origin,Access-Control-Request-Method,Access-Control-Request-Headers,Last-Modified</param-value> 
    </init-param> 
    <init-param> 
     <param-name>cors.exposed.headers</param-name> 
     <param-value>Access-Control-Allow-Origin,Access-Control-Allow-Credentials</param-value> 
    </init-param> 
    <init-param> 
     <param-name>cors.support.credentials</param-name> 
     <param-value>true</param-value> 
    </init-param> 
</filter> 
<filter-mapping> 
    <filter-name>CorsFilter</filter-name> 
    <url-pattern>/*</url-pattern> 
</filter-mapping> 

是由Tomcat緩存請求頭或使用它的最後一個請求頭不知何故這是造成混淆,並封鎖了所有請求?

+0

您是否嘗試將'cors.preflight.maxage'設置爲'-1'?從[documentation flowchat](https://tomcat.apache.org/tomcat-8.0-doc/images/cors-flowchart.png),如果'cors.support.credentials'爲true,那麼'Access-Control-Allow -Origin沒有設置爲「*」,而是設置爲原始標題。在你的情況下,它可能是一個瀏覽器緩存問題(tomcat告訴瀏覽器緩存pre-flight請求的結果1800秒)。 – 2015-04-19 07:41:15

回答

0

如果設置cors.support.credentials爲true,使用cors.preflight.maxage爲-1,以禁用瀏覽器的緩存:

<init-param> 
    <param-name>cors.preflight.maxage</param-name> 
    <param-value>-1</param-value> 
</init-param> 

您確定要設置cors.allowed.origins*

相關問題