當前臺頁面請求WMS可能會遇到瀏覽器如下提示(瀏覽器控制檯):html
已阻止跨源請求:同源策略禁止讀取位於 http://xxx.xxx.com 的遠程資源。(緣由:CORS 頭缺乏 'Access-Control-Allow-Origin')java
原文大概這樣web
Access to Image at 'http://192.168.0.131:8080/geoserver/CHINA/wms?SERVICE=WMS&VERSION=1.1.1&REQ…AT_OPTIONS=dpi%3A99&BBOX=-20037508.342789244%2C-20037508.342789244%2C0%2C0'
from origin 'http://localhost:59307' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource.
Origin 'http://localhost:59307' is therefore not allowed access. The response had HTTP status code 404.
貼個圖更形象一些瀏覽器
網上找到的大部分CORS配置都是針對GeoServer 安裝版的 像 基於CORS的geoserver同源訪問策略 這樣的app
由於我事先已經有Tomcat了,因此用的是解壓版本的GeoServer 。(貼上搭建環境的鏈接: 搭建簡易Web GIS網站:使用GeoServer+PostgreSQL+PostGIS+OpenLayers3)cors
找到的方法沒法實現,只能尋找其餘辦法,那就是針對Tomcat的 CORSwebapp
首先須要下載cors-filter-1.7.jar,java-property-utils-1.9.jar這兩個jar包,放到Tomcat 的 lib目錄下。maven
個人路徑是 D:\Program Files (x86)\ApacheTomcat\lib網站
(也可在http://search.maven.org上查詢並下載。)url
而後 找到你鬚鬚要配置CORS的應用的路徑(也就是個人 geoserver)
D:\Program Files (x86)\ApacheTomcat\webapps\geoserver
而後找到 WEB-INF 下面的 web.xml 在filter集合末尾額外添加以下配置
<filter> <filter-name>CORS</filter-name> <filter-class>com.thetransactioncompany.cors.CORSFilter</filter-class> <init-param> <param-name>cors.allowOrigin</param-name> <param-value>*</param-value> </init-param> <init-param> <param-name>cors.supportedMethods</param-name> <param-value>GET, POST, HEAD, PUT, DELETE</param-value> </init-param> <init-param> <param-name>cors.supportedHeaders</param-name> <param-value>Accept, Origin, X-Requested-With, Content-Type, Last-Modified</param-value> </init-param> <init-param> <param-name>cors.exposedHeaders</param-name> <param-value>Set-Cookie</param-value> </init-param> <init-param> <param-name>cors.supportsCredentials</param-name> <param-value>true</param-value> </init-param> </filter> <filter-mapping> <filter-name>CORS</filter-name> <url-pattern>/*</url-pattern> </filter-mapping>
而後重啓geoserver 就行了