请求Geoserver的WTMS服务返回200不返回图片问题-跨域导致

今天碰到个奇怪问题,改了个页面标题再打包布署GeoServer发现调用WTMS服务失败,请求返回状态码200,返回包大小0,使用postman模拟请求是可以正常返回图片的。

跟之前版本对比如下:

正常Response请求:

xml 复制代码
   HTTP/1.1 200
    X-Frame-Options: SAMEORIGIN
    X-Content-Type-Options: nosniff
    geowebcache-tile-index: [13591, 9726, 14]
    Cache-Control: no-cache, no-store, must-revalidate
    Pragma: no-cache
    Expires: 0
    geowebcache-cache-result: HIT
    geowebcache-tile-index: [13591, 9726, 14]
    geowebcache-tile-bounds: 13205872.500935059,3752140.8439404294,13208318.485839844,3754586.828845214
    geowebcache-gridset: EPSG:3857
    geowebcache-crs: EPSG:3857
    Last-Modified: Wed, 08 Jan 2025 03:40:58 GMT
    Content-Disposition: inline; filename=geoserver-dispatch.image
    Content-Type: image/png
    Content-Length: 188615
    Date: Thu, 27 Feb 2025 05:48:49 GMT
    Keep-Alive: timeout=20
    Connection: keep-alive

异常Response请求:

xml 复制代码
   HTTP/1.1 200
    X-Frame-Options: SAMEORIGIN
    X-Content-Type-Options: nosniff
    geowebcache-tile-index: [13591, 9726, 14]
    Cache-Control: no-cache, no-store, must-revalidate
    Pragma: no-cache
    Expires: 0
    geowebcache-cache-result: HIT
    geowebcache-tile-index: [13591, 9726, 14]
    geowebcache-tile-bounds: 13205872.500935059,3752140.8439404294,13208318.485839844,3754586.828845214
    geowebcache-gridset: EPSG:3857
    geowebcache-crs: EPSG:3857
    Last-Modified: Wed, 08 Jan 2025 03:40:58 GMT
    Content-Disposition: inline; filename=geoserver-dispatch.image
    Content-Type: image/png
    Content-Length: 188615
    Date: Thu, 27 Feb 2025 05:48:49 GMT
    Keep-Alive: timeout=20
    Connection: keep-alive

发现异常中没有:Access-Control-Allow-Origin

才想起来之前好像配置过跨域:

修改跨域:web.xml

注意如果按deepseek推荐使用org.eclipse.jetty.servlets.CrossOriginFilter这个类需要添加jar包(jetty-servlets-9.4.56.v20240826.jar, jetty-util-9.4.56.v20240826.jar)

下面我推荐tomcat的,不用加jar包

xml 复制代码
<!-- Uncomment following filter to enable CORS in Tomcat. Do not forget the second config block further down. -->
    <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>
    
    
    
    <!-- Uncomment following filter-mapping to enable CORS -->
    <filter-mapping>
        <filter-name>cross-origin</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>

完了记录清理Tomcat缓存:apache-tomcat-8.5.99\work\Catalina\localhost

重启Tomcat就可以正常使用。

如果需要修改缓存目录,修改geoserver中web.xml,增加

xml 复制代码
   <context-param>
      <param-name>GEOSERVER_DATA_DIR</param-name>
       <param-value>E:\map\geoserver-2.15.0\webapps\geoserver\data</param-value>
   </context-param>
   <context-param>
      <param-name>GEOWEBCACHE_CACHE_DIR</param-name>
      <param-value>E:\map\geoserver-2.15.0\cache</param-value>
    </context-param> 
相关推荐
这是个栗子5 小时前
【前端知识点总结】前端跨域问题
前端·跨域·cors
闲人编程1 天前
CORS跨域配置与安全策略
中间件·origin·跨域·cors·codecapsule·分离配置·最小权限
随风一样自由9 天前
React中实现iframe嵌套登录页面:跨域与状态同步解决方案详解
前端·react.js·前端框架·跨域
激动的兔子12 天前
Geoserver修行记-SLD样式内的中文字体乱码
运维·geoserver
激动的兔子1 个月前
Geoserver修行记-连接瀚高数据库显示java.sql.SQLException: org.postgresql.util.PSQLException
java·geoserver·瀚高数据库
漂流幻境1 个月前
Spring cloud gateway 跨域配置与碰到的问题
java·gateway·springcloud·跨域
linweidong1 个月前
VIVO前端面试题及参考答案
前端·跨域·localstorage·重绘·浏览器兼容·git管理·前端重构
尽兴-1 个月前
[特殊字符] 微前端部署实战:Nginx 配置 HTTPS 与 CORS 跨域解决方案(示例版)
前端·nginx·https·跨域·cors·chrom
激动的兔子2 个月前
Geoserver修行记-安装CSS插件避坑
运维·geoserver·插件安装
emanjusaka2 个月前
【开发问题】GeoServer 跨域问题解决方案
geoserver·cross-origin