解决Geoserver跨域问题

Resolve No ‘Access-Control-Allow-Origin’ issue by setting ‘Cross-Origin Resource Sharing’ (CORS) for cross origin AJAX calls between web applications & GeoServer running on different origion

Browsers blocks cross origin requests by default.

Web Application runs on 80 port under WAMP/ Apache/ IIS Server or 8080 port under Tomcat/ Any Servlet Container/ Engine/ Server/ Domain by default.

GeoServer runs on 8080 port under Jetty/ Tomcat/ Any Servlet Container/ Engine/ Server/ Domain by default.

GetFeatureInfo request from Web Application (http://localhost/myproject/index.htm or http://localhost:80/myproject/index.htm) to GeoServer (http://localhost:8080/geoserver/wms) will throw an ‘No Access-Control-Allow-Origin’ error, because both runs on different ports even though both are installed in single system. Response can be seen in browser’s developer tools console window by logging it using console.log(), but can’t be accessed by the web application.

To resolve this, either

  • Enable CORS (Recommended) for JSON (application/json) based request or
  • Enable JSONP for JSONP (text/javascript) based request or
  • Enable both CORS & JSONP for both JSON & JSONP based requests.

CORS is not required, when Web Application (http://localhost:8080/myproject/index.htm) & GeoServer (http://localhost:8080/geoserver/wms) runs on same port (E.g.: 8080) under Jetty/ Tomcat/ Any Servlet Container/ Engine/ Server/ Domain. Its possible to run Web Application & GeoServer on same port in many ways. In this case, JSONP may be enabled in GeoServer for JSONP based request.

1. Without enabling CORS: Run Web Application & GeoServer on same port

  1.  Copy Web Application(myproject)folder into below target folders where the geoserver application folder is also available.
    GeoServer Installer(2.16.0): GeoServer installed through GeoServer Installer, which includes GeoServer & Jetty.
    Windows: C:\Program Files (x86) or Program Files \GeoServer 2.16.0\webapps

    OpenGeo Suite Installer(4.8): GeoServer installed through OpenGeo Suite Installer, which includes GeoServer & Jetty etc.
    Windows: C:\Program Files (x86) or Program Files \Boundless\OpenGeo\jetty\webapps

    GeoServer Binaries: GeoServer installed through GeoServer Binaries, which includes GeoServer & Jetty.
     **Windows: ** C:\GeoServer 2.16.0\webapps

    GeoServer Web Archive (war): GeoServer installed through GeoServer WAR file.
    Windows (Jetty): C:\Jetty 9.2.13.v20150730\webapps
     **Windows (Tomcat): ** C:\Program Files (86) or Program Files \Apache Software Foundation\Tomcat 9.0\webapps

  2. Now Web Application (myproject) is available at http://localhost:8080/myproject/index.htm & GeoServer is available @ http://localhost:8080/geoserver/wms. So no need to enable CORS.
    2. Enable CORS for JSON based request (GeoServer & Jetty)

  3. Check the Jetty version (E.g.: GeoServer 2.11.1/ 2.16.0 uses Jetty 9.2.13.v20150730, OpenGeo Suite (4.8) uses Jetty 7.6.13.v20130916) from below target files or from any of the jetty jar files available in below target folders.
    GeoServer Installer
    Windows: C:\Program Files (x86) or Program Files \GeoServer 2.16.0\start.ini (OR) C:\Program Files (x86) or Program Files \GeoServer 2.16.0\lib

    OpenGeo Suite Installer
    Windows: C:\Program Files (x86) or Program Files \Boundless\OpenGeo\jetty\start.ini (OR) C:\Program Files (x86) or Program Files \Boundless\OpenGeo\jetty\lib

    GeoServer Binaries
    Windows: C:\GeoServer 2.16.0\start.ini (OR) C:\GeoServer 2.16.0\lib

    GeoServer Web Archive (war)
    The downloaded Jetty version is known.
    Download GeoServer Web Archive (war) file from http://geoserver.org.
    Paste the war file @ C:\Jetty 9.2.13.v20150730\webapps

  4. Download jetty-servlets-9.2.13.v20150730.jar file from http://central.maven.org/maven2/org/eclipse/jetty/jetty-servlets or http://repo1.maven.org/maven2/org/eclipse/jetty/jetty-servlets based on the Jetty version found in the above file.

  5. Stop GeoServer/ Jetty (If GeoServer installed through GeoServer WAR file under Jetty).

  6. Paste the jar file into below target folders, if not available.
    GeoServer Installer
    Windows: C:\Program Files (x86)\GeoServer 2.16.0\lib (Recommended) or C:\Program Files (x86)\GeoServer 2.16.0\webapps\geoserver\WEB-INF\lib

    OpenGeo Suite Installer
    Windows: C:\Program Files (x86)\Boundless\OpenGeo\jetty\lib (Recommended) or ?
    Mac OS X: /Users//Library/Application Support/GeoServer/jetty/lib

    GeoServer Binaries
    Windows: C:\GeoServer 2.16.0\lib (Recommended) or C:\GeoServer 2.16.0\webapps\geoserver\WEB-INF\lib

    GeoServer Web Archive (war)
    Windows: C:\Jetty 9.2.13.v20150730\lib (Recommended) or C:\Jetty 9.2.13.v20150730\webapps\geoserver\WEB-INF\lib

  7. Add the below eight lines into below target files.
    GeoServer Installer
    Windows: C:\Program Files (x86)\GeoServer 2.16.0\webapps\geoserver\WEB-INF\web.xml (Recommended) or C:\Program Files (x86)\GeoServer 2.16.0\etc\webdefault.xml

    OpenGeo Suite Installer
    Windows:

    GeoServer Binaries
    Windows:

    GeoServer Web Archive(war)
    Windows: C:\Jetty 9.2.13.v20150730\webapps\geoserver\WEB-INF\web.xml (Recommended) or C:\Jetty 9.2.13.v20150730\etc\webdefault.xml


    ...

    
        cross-origin
        org.eclipse.jetty.servlets.CrossOriginFilter
    
    ...
    
        cross-origin
        /*
    
    ...

  1. Save & close the file.
  2. Start/ Restart GeoServer/ Jetty.
  3. GeoServer/ Jetty takes time to start, So after sometime browse the url http://localhost:8080/geoserver to test whether it’s started/ not.
    **3. Enable CORS for JSON based request (GeoServer Web Archive (war) & Tomcat) **
  4. Download GeoServer Web Archive (war) file from http://geoserver.org..
  5. Stop Tomcat.
  6. Paste the war file into C:\Program Files (86)\Apache Software Foundation\Tomcat 9.0\webapps folder.
  7. Add the below eight lines into C:\Program Files (86)\Apache Software Foundation\Tomcat 9.0\conf\web.xml (Recommended) or C:\Program Files (86)\Apache Software Foundation\Tomcat 9.0\webapps\geoserver\WEB-INF\web.xml file.

    ...
    
        CorsFilter
        org.apache.catalina.filters.CorsFilter
    
    cors.allowed.origins
    *
  
    
    ...
    
        CorsFilter
        /*
    
    ...

  1. Save & close the file.
  2. Start/ Restart Tomcat.
  3. Tomcat takes time to start, So after sometime browse the url http://localhost:8080 to test whether it’s started/ not.

    4. Enable JSONP for JSONP based request (GeoServer & Jetty)
  4. Stop GeoServer/ Jetty (If GeoServer installed through GeoServer WAR file under Jetty).
  5. Uncomment/ Add (if not found) the below four lines into below target files. Ensure that ‘param-value’ is set to true.
    GeoServer Installer
    Windows: C:\Program Files (x86)\GeoServer 2.16.0\webapps\geoserver\WEB-INF\web.xml

    OpenGeo Suite Installer
    Windows: ?

    GeoServer Binaries
    Windows: C:\GeoServer 2.16.0\webapps\geoserver\WEB-INF\web.xml

    GeoServer Web Archive (war)
    Windows: C:\Jetty 9.2.13.v20150730\webapps\geoserver\WEB-INF\web.xml

    ...
    
        ENABLE_JSONP
        true
    
    ...

  1. Save & close the file.
  2. Start/ Restart GeoServer/ Jetty.
  3. GeoServer/ Jetty takes time to start, So after sometime browse the url http://localhost:8080/geoserver to test whether it’s started/ not.

    5. Enable JSONP for JSONP based request (GeoServer Web Archive (war) & Tomcat)
  4. Stop Tomcat.
  5. Uncomment/ Add (if not found) the below four lines into below target files. Ensure that ‘param-value’ is set to true.
    GeoServer Web Archive (war)
    Windows: C:\Program Files (86)\Apache Software Foundation\Tomcat 9.0\webapps\geoserver\WEB-INF\web.xml

    ...
    
        ENABLE_JSONP
        true
    
    ...

  1. Save & close the file.
  2. Start/ Restart Tomcat.
  3. Tomcat takes time to start, So after sometime browse the url http://localhost:8080/geoserver to test whether it’s started/ not.
    6. References
  4. Enable CORS in Jetty & GeoServer:http://suite.opengeo.org/docs/latest/sysadmin/cors/index.html
  5. Enable CORS in Tomcat & GeoServer:http://enable-cors.org/server_tomcat.html
  6. Enable JSONP in GeoServer:https://docs.geoserver.org/stable/en/user/services/wms/global.html#enable-jsonp
  7. Ways to set JSONP in GeoServer:http://docs.geoserver.org/stable/en/user/datadirectory/setting.html

你可能感兴趣的:(geoserver,tomcat)