Geoserver跨域設(shè)置
服務(wù)端發(fā)布的服務(wù),在未設(shè)置允許跨域時(shí),從其他域前端訪問(wèn)時(shí),會(huì)顯示跨域錯(cuò)誤,同時(shí)也不能訪問(wèn)到數(shù)據(jù)。
Geoserver安裝完成時(shí),默認(rèn)未開(kāi)啟允許跨域,直接從其他域用前端的地圖類庫(kù)訪問(wèn)數(shù)據(jù)時(shí),會(huì)訪問(wèn)不到數(shù)據(jù),需要手動(dòng)進(jìn)行開(kāi)啟。
開(kāi)啟的方法:在Geoserver安裝完成的目錄中,打開(kāi)配置文件,路徑是:webapps/geoserver/WEB-INF/web.xml
使用jetty作為Server時(shí),將以下內(nèi)容取消注釋:
<filter>
?<filter-name>cross-origin</filter-name>
?<filter-class>org.eclipse.jetty.servlets.CrossOriginFilter</filter-class>
?<init-param>
? ?<param-name>chainPreflight</param-name>
? ?<param-value>false</param-value>
?</init-param>
?<init-param>
? ?<param-name>allowedOrigins</param-name>
? ?<param-value>*</param-value>
?</init-param>
?<init-param>
? ?<param-name>allowedMethods</param-name>
? ?<param-value>GET,POST,PUT,DELETE,HEAD,OPTIONS</param-value>
?</init-param>
?<init-param>
? ?<param-name>allowedHeaders</param-name>
? ?<param-value>*</param-value>
?</init-param>
</filter>
使用Tomcat作為Server時(shí),將以下內(nèi)容取消注釋:
<filter>
?<filter-name>cross-origin</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,PUT,DELETE,HEAD,OPTIONS</param-value>
?</init-param>
?<init-param>
? ?<param-name>cors.allowed.headers</param-name>
? ?<param-value>*</param-value>
?</init-param>
</filter>
同時(shí),無(wú)論使用何種Server,都將以下代碼取消注釋:
<filter-mapping>
?<filter-name>cross-origin</filter-name>
?<url-pattern>/*</url-pattern>
</filter-mapping>
或者,使用前端的web server進(jìn)行代理,加上允許跨域的頭,也可行。
參考內(nèi)容:
https://docs.geoserver.org/latest/en/user/production/container.html#production-container-enable-cors