GeoServer配置CORS(跨域資源共享)


當前台頁面請求WMS可能會遇到瀏覽器以下提示(瀏覽器控制台):

    已阻止跨源請求:同源策略禁止讀取位於 http://xxx.xxx.com 的遠程資源。(原因:CORS 頭缺少 'Access-Control-Allow-Origin')

原文大概這樣

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同源訪問策略 這樣的

因為我事先已經有Tomcat了,所以用的是解壓版本的GeoServer 。(貼上搭建環境的連接: 搭建簡易Web GIS網站:使用GeoServer+PostgreSQL+PostGIS+OpenLayers3

找到的方法無法實現,只能尋找其他辦法,那就是針對Tomcat的 CORS

 

首先需要下載cors-filter-1.7.jar,java-property-utils-1.9.jar這兩個jar包,放到Tomcat 的  lib目錄下。

我的路徑是 D:\Program Files (x86)\ApacheTomcat\lib

(也可在http://search.maven.org上查詢並下載。)

下載地址 

然后 找到你須需要配置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 就好了

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM