6

I try to use a WFS service with OpenStreetMap to request GeoServer GeoJSON file. First I had a error message on the Firefox Cross-Origin Request Blocked: The Same Origin Policy disallows reading the remote resource at http://localhost:8080/geoserver/robot/ows?service=WFS&version=1.0.0&request=GetFeature&typeName=robot:geofield&outputFormat=aoolication%2Fjson. (Reason: CORS header 'Access-Control-Allow-Origin' missing).

Then I pasted the following lines in the web.xml from /usr/share/geoserver/webapps/geoserver/WEB-INF

<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>

After when I go on http://localhost:8080/geoserver/ I have a HTTP ERROR: 503 and still have the same CORS header problem

I don't know what is wrong, maybe this method doesn't work with latest GeoServer versions

If someone has a good and clear answer, it will be great because I think I'm not the only one who want to use a wfs with GeoServer and OpenLayers.

2 Answers 2

8

I think you are using jetty and not catalina, I got same problem in Windows :

http://www.eclipse.org/jetty/documentation/current/cross-origin-filter.html

Try putting instead in web.xml :

<filter>
    <filter-name>cross-origin</filter-name>
    <filter-class>org.eclipse.jetty.servlets.CrossOriginFilter</filter-class>
</filter>
<filter-mapping>
    <filter-name>cross-origin</filter-name>
    <url-pattern>/*</url-pattern>
</filter-mapping>

And copy into C:\Program Files (x86)\GeoServer 2.10.0\webapps\geoserver\WEB-INF\lib folder : jetty-servlets-9.2.13.v20150730.jar

Get jar needed from http://central.maven.org/maven2/org/eclipse/jetty/jetty-servlets/, and check needed version from your folder C:\Program Files (x86)\GeoServer 2.10.0\lib

3
  • 1
    in my package of Geoserver it was already there, so no needs to download from internetz. Worked for me, may work for others. $> cp $GEOSERVER_HOME\lib\jetty-servlets-9.2.13.v20150730.jar $GEOSERVER_HOME\webapps\geoserver\WEB-INF\lib
    – Sanctus
    Aug 14, 2018 at 12:32
  • I didn't need to do your 2nd step in order to fix this issue.
    – Theo F
    May 19, 2020 at 17:04
  • To add details: with recent versions of Geoserver, the JAR file is already there. Not only do you not need to add it to WEB-INF/lib, but rather DON'T do it! Creating a copy can (as it did to me) confuse the server and make it unable to start! Simply uncomment the 2 filter text blocks, and everything it good.
    – Horizen
    Mar 21, 2022 at 19:36
0

In my case I just put this in my C:\Program Files\GeoServer\webapps\geoserver\WEB-INF\web.xml and works for me

<filter>
    <filter-name>cross-origin</filter-name>
    <filter-class>org.eclipse.jetty.servlets.CrossOriginFilter</filter-class>
</filter>
<filter-mapping>
    <filter-name>cross-origin</filter-name>
    <url-pattern>/*</url-pattern>
</filter-mapping>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.