大家好, 我正在尝试在Tomcat 7.0.52上为Geoserver启用CORS。
我按照http://tomcat.apache.org/tomcat-7.0-doc/config/filter.html#CORS_Filter中的说明,修改了tomcat中conf文件夹下的web.xml文件。
但是,这并没有帮助设置头部的跨源资源共享。
我甚至尝试了对geoserver web-inf/web.xml文件进行修改,但也没有效果。
欢迎提出任何建议。
谢谢!
大家好, 我正在尝试在Tomcat 7.0.52上为Geoserver启用CORS。
我按照http://tomcat.apache.org/tomcat-7.0-doc/config/filter.html#CORS_Filter中的说明,修改了tomcat中conf文件夹下的web.xml文件。
但是,这并没有帮助设置头部的跨源资源共享。
我甚至尝试了对geoserver web-inf/web.xml文件进行修改,但也没有效果。
欢迎提出任何建议。
谢谢!
为了避免在OpenLayers中使用代理,我需要做同样的事情。
由于我正在运行Ubuntu 12.04,所以我安装了 Tomcat 7.0.55,而不是默认的7.0.26(从软件包中安装)。
为了添加CORS标头,我只需将以下行添加到 $CATALINA_HOME/conf/web.xml
中:
<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>
</filter>
<filter-mapping>
<filter-name>CorsFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
然后重新启动Tomcat。
例如,当我尝试从我的应用程序(运行在http://localhost:3000
)获取Geoserver的URL http://development.localhost.lan/geoserver/wfs
时,我会收到以下标头信息:
请求的标头:
POST /geoserver/wfs HTTP/1.1
Host: development.localhost.lan
Origin: http://localhost:3000
X-Requested-With: XMLHttpRequest
(...)
响应头:
Access-Control-Allow-Credentials:true
Access-Control-Allow-Origin:http://localhost:3000
Connection:Keep-Alive
Content-Disposition:inline; filename=geoserver-GetFeature.text
Content-Encoding:gzip
Content-Length:469
Content-Type:text/xml; subtype=gml/3.1.1
Date:Tue, 29 Jul 2014 21:31:08 GMT
Keep-Alive:timeout=5, max=100
Server:Apache-Coyote/1.1
这适用于Chrome(Ver. 35.0.1916.153)和Firefox(Ver. 31.0)。
/var/lib/tomcat7/webapps/geoserver/WEB-INF/web.xml
。 - Terry Brown<init-param>
<param-name>cors.allowed.methods</param-name>
<param-value>GET,POST,HEAD,OPTIONS,PUT</param-value>
</init-param>
大家好,在web.xml中添加了这个脚本后
<filter>
<filter-name>cross-origin</filter-name>
<filter-class>org.eclipse.jetty.servlets.CrossOriginFilter</filter-class>
<init-param>
<param-name>allowedOrigins</param-name>
<param-value>*</param-value>
</init-param>
<init-param>
<param-name>allowedMethods</param-name>
<param-value>*</param-value>
</init-param>
<init-param>
<param-name>allowedHeaders</param-name>
<param-value>*</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>cross-origin</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
请将您的ajax中的URL更改为以下内容
var boxsource = new ol.source.Vector({
//url: '{{ resource.ows_url|safe }}',
url: 'http://localhost:8080/geoserver/wfs?srsName=EPSG%3A4326&typename=cite:box&outputFormat=json&version=1.1.0&service=WFS&request=GetFeature',
format: new ol.format.GeoJSON(),
params: {'LAYERS': '{{ resource.typename }}'},
//STYLES:{'LAYERS': '{{ resource.typename }}'}
});
那就好了,我保证。