15

I hope somebody has already figured this one out. I just installed Geoserver 2.9 on a vanilla Ubuntu 16.04 distro. The Geoserver 2.8 method of enabling CORS with the shanbe.hezoun class does no longer work with Jetty 9.2.13.

There are mentions that CORS support is already packaged with Jetty 9.2.13 in the jetty-servlets.jar.

The Jetty lib which is compiled with Geoserver contains a jetty-servlet-9.2.13.v20150730.jar in geoserver/lib but not jetty-servlets.9.2.13.v20150730.jar. Are these supposed to be the same jar with a different name?

It should be possible to enable CORS either in geoserver/etc/webdefault.xml or in geoserver/webapps/geoserver/WEB-INF/web.xml.

My understanding is that the webdefault.xml is applied first and the web.xml thereafter.

I have tried following filter in both xml. I haven't got as far as adding a filter mapping. Adding the filter alone will cause the Geoserver/Jetty service to not start proper.

<filter>
    <filter-name>cross-origin</filter-name>
    <filter-class>org.eclipse.jetty.servlets.CrossOriginFilter</filter-class>
</filter>
  • 1
    Servlet and servlets does not seem to be the same archive.eclipse.org/jetty/9.2.13.v20150730/apidocs/…. And some links to the documents you have used would help those who try to answer. – user30184 Sep 9 '16 at 12:36
  • why not use tomcat? – Ian Turton Sep 9 '16 at 12:42
  • 1
    Good question. I have Geoserver 2.9 running with Tomcat but wanted to test the binary setup just to see whether this would make my life easier. It didn't. – Dennis Bauszus Sep 9 '16 at 13:55
  • What was your solution? – Kieveli Sep 22 '16 at 15:01
  • 1
    Ok. I just already solved the problem for Geoserver 2.10. It's my fault to not installed the servlets jar correctly. I should download the correct servlets in here then copy into the directory "\WEB-INF\lib" and edit the "WEB-INF\web.xml" to be add the filter parameters as I followed the comment from zflaw in this thread. Jetty v9+ has already supported the CORS. – Rizky Firmansyah Feb 10 '17 at 2:53
19

Just to clarify there are 2 steps required to make CORS access work with GeoServer and Jetty:

  1. Add the Jetty-Utility Servlets Jar to match the version of Jetty - for current versions of GeoServer (2.15.x) it is 9.4.12.v20180830, copy this to webapps/geoserver/WEB-INF/lib inside the geoserver-2.15.0 directory (or wherever you unpacked the zip file).

  2. Edit the webapps/geoserver/WEB-INF/web.xml file. There are two references to CORS in this file:

<!-- Uncomment following filter to enable CORS -->
<filter>
  <filter-name>cross-origin</filter-name>
     <filter-class>org.eclipse.jetty.servlets.CrossOriginFilter</filter-class>
  </filter>

and

<!-- Uncomment following filter to enable CORS -->
<filter-mapping>
    <filter-name>cross-origin</filter-name>
    <url-pattern>/*</url-pattern>
</filter-mapping>

You must uncomment both blocks (that is remove <!-- and --> from the filter and filter-mapping blocks.

Then when you restart Jetty you can test that everything is working by using a command like:

curl -v -H "Origin: http://example.com" http://astun-desktop:9080/geoserver/wfs\?service\=WFS\&version\=2.0.0\&request\=GetFeature\&typenames\=sf:bugsites\&filter\=%3Cfes:Filter%20xmlns:fes\=%22http://www.opengis.net/fes/2.0%22%3E%3Cfes:ResourceId%20rid\=%22bugsites.3%22/%3E%3C/fes:Filter%3E

which if all is well will give a result like:

> User-Agent: curl/7.35.0
> Host: astun-desktop:9080
> Accept: */*
> Origin: http://example.com
>  
< HTTP/1.1 200 OK 
< Access-Control-Allow-Origin: http://example.com 
< Access-Control-Allow-Credentials: true 
< Access-Control-Expose-Headers:  
< Content-Type: text/xml; subtype=gml/3.2 
< Content-Disposition: inline; filename=geoserver-GetFeature.text 
< Transfer-Encoding: chunked
* Server Jetty(9.2.13.v20150730) is not blacklisted 
< Server: Jetty(9.2.13.v20150730) 
< 
* Connection #0 to host astun-desktop left intact 
<?xml version="1.0" encoding="UTF-8"?><wfs:FeatureCollection xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:sf="http://www.openplans.org/spearfish" xmlns:wfs="http://www.opengis.net/wfs/2.0" xmlns:gml="http://www.opengis.net/gml/3.2" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" numberMatched="1" numberReturned="1" timeStamp="2017-07-30T15:58:31.423Z" xsi:schemaLocation="http://www.opengis.net/wfs/2.0 http://astun-desktop:9080/geoserver/schemas/wfs/2.0/wfs.xsd http://www.openplans.org/spearfish http://astun-desktop:9080/geoserver/wfs?service=WFS&amp;version=2.0.0&amp;request=DescribeFeatureType&amp;typeName=sf%3Abugsites http://www.opengis.net/gml/3.2 http://astun-desktop:9080/geoserver/schemas/gml/3.2.1/gml.xsd"><wfs:member><sf:bugsites gml:id="bugsites.3"><sf:the_geom><gml:Point srsName="urn:ogc:def:crs:EPSG::26713" srsDimension="2"><gml:pos>590529 4914625</gml:pos></gml:Point></sf:the_geom><sf:cat>3</sf:cat><sf:str1>Beetle site</sf:str1></sf:bugsites></wfs:member></wfs:FeatureCollection>%
  • 6
    For different versions of geoserver, I've been guessing the compatible jetty verison using find $GEOSERVER_HOME -name "jetty*" | grep -E [[:digit:]] . – Steven Kalt Oct 12 '17 at 20:42
  • How do you restart jetty? – user210757 Jul 31 '18 at 17:41
6

It will work if you add the filter in "geoserver/webapp/geoserver/WEB-INF/web.xml" and if you add the jar "jetty-servlets.9.2.13.v20150730.jar" inside "geoserver/webapp/geoserver/WEB-INF/lib"

  • From where would I get the jetty-servlets.9.2.13.v20150730.jar? Is that different to the jetty-servlet-9.2.13.v20150730.jar which is packaged with Geoserver 2.9? – Dennis Bauszus Oct 4 '16 at 15:57
  • yes its different. Also note the destination folder is different – Calanus Jan 1 '17 at 20:42
  • I am using geoserver 2.8.2.Jetty version not getting.Can any tell me how to find jetty version.I am seeing only jetty-6.8.1 in C:/Program Files (x86)/GeoServer 2.8.2/lib – veena hosur Feb 20 '18 at 6:41
3

with Jetty9, UbuntuServer 16.04, i also had to modify /etc/jetty9/start.ini, so as not to get the following error :

2018-03-31 15:10:01.769:WARN:oejuc.AbstractLifeCycle:main: FAILED cross-origin: javax.servlet.UnavailableException: org.eclipse.jetty.servlets.CrossOriginFilter javax.servlet.UnavailableException: org.eclipse.jetty.servlets.CrossOriginFilter

the solution is here : you should enable the servlets module in your ${jetty.base}/start.ini

consequently, i replaced :

--module=deploy,http,jsp,jstl,websocket,ext,resources

by :

--module=deploy,http,jsp,jstl,websocket,ext,resources,servlets
0

Accepted answer by Ian Turton is absolutely best here. Since I'm using Docker manual editing is not the case. Also I'm not a SED guru, but thanks to structure of web.xml (target strings are unique in document scope), I come up with little snippet:

sed -i 's_<!-- <filter>_<filter>_' web.xml
sed -i 's_</filter> -->_</filter>_' web.xml
sed -i 's_<!-- <filter-mapping>_<filter-mapping>_' web.xml
sed -i 's_</filter-mapping> -->_</filter-mapping>_' web.xml

Or in Dockerfile:

# enable CORS
RUN wget -q http://central.maven.org/maven2/org/eclipse/jetty/jetty-servlets/9.2.13.v20150730/jetty-servlets-9.2.13.v20150730.jar -P ${GEOSERVER_INSTALL_DIR}/WEB-INF/lib \
 && sed -i 's_<!-- <filter>_<filter>_' ${GEOSERVER_INSTALL_DIR}/WEB-INF/web.xml \
 && sed -i 's_</filter> -->_</filter>_' ${GEOSERVER_INSTALL_DIR}/WEB-INF/web.xml \
 && sed -i 's_<!-- <filter-mapping>_<filter-mapping>_' ${GEOSERVER_INSTALL_DIR}/WEB-INF/web.xml \
 && sed -i 's_</filter-mapping> -->_</filter-mapping>_' ${GEOSERVER_INSTALL_DIR}/WEB-INF/web.xml
0

For anyone is wondering which version of jetty you have for your particular geoserver application.

For OSX I simply started geoserver and looked in the log it should show something like:

2019-05-10 07:25:13.444:INFO:oejs.Server:startup executor: jetty-9.2.13.v20150730

I'm sure it is similar in the tomcat logs when running from a linux server if needed.

Also, it should be visible in the response headers ie:

Connection: close
Server: Jetty(9.2.13.v20150730)
X-Frame-Options: SAMEORIGIN

Ie, as the accepted answer mentions try using curl command it will present the server version too:

curl -v -H "Origin: http://example.com" http://astun-desktop:9080/geoserver/wfs\?service\=WFS\&version\=2.0.0\&request\=GetFeature\&typenames\=sf:bugsites\&filter\=%3Cfes:Filter%20xmlns:fes\=%22http://www.opengis.net/fes/2.0%22%3E%3Cfes:ResourceId%20rid\=%22bugsites.3%22/%3E%3C/fes:Filter%3E

protected by Community Nov 23 '17 at 18:00

Thank you for your interest in this question. Because it has attracted low-quality or spam answers that had to be removed, posting an answer now requires 10 reputation on this site (the association bonus does not count).

Would you like to answer one of these unanswered questions instead?

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