Geoserver跨域解决方法

Geoserver跨域解决方法
参考来源:https://blog.csdn.net/GIS_Lyn/article/details/84744184

首先两种方式发布GeoServer,分别是Jetty与Tomcat
两种方式跨域 cross origin 解决方法不同,看完继续看第三种解决方法

方法1: Jetty发布的解决方法 自动安装的geoserver版本
https://www.cnblogs.com/arxive/p/8673399.html

https://blog.csdn.net/geol200709/article/details/80320824

方法2:Tomcat发布的Geoserver跨域解决方法
To add CORS headers, I simply added to $CATALINA_HOME/conf/web.xml the following lines:
/usr/tomcat/conf/web.xml设置添加参数如下:


  CorsFilter
  org.apache.catalina.filters.CorsFilter
  
        cors.allowed.origins
        http://localhost:8080,http://192.168.30.114:8888
    
    
        cors.allowed.methods
        GET,POST,HEAD,OPTIONS,PUT
    
    
        cors.allowed.headers
        Content-Type,X-Requested-With,accept,Origin,Access-Control-Request-Method,Access-Control-Request-Headers,Authorization
    

    
        cors.exposed.headers
        Access-Control-Allow-Origin,Access-Control-Allow-Credentials
    
     
	
        cors.support.credentials
        true
     
	
        cors.preflight.maxage
        10
      


  CorsFilter
  /*

/usr/tomcat/webapps/geoserver/WEB-INF/web.xml设置添加参数如下:


 
  CorsFilter
  org.apache.catalina.filters.CorsFilter
  
  
  
    cors.allowed.origins
    http://localhost:8080,http://192.168.30.114:8888
  

  
    cors.allowed.methods
    GET,POST,HEAD,OPTIONS,PUT
  
  
    cors.allowed.headers
    Content-Type,X-Requested-With,accept,Origin,Access-Control-Request-Method,Access-Control-Request-Headers,Authorization
  
  
    cors.exposed.headers
    Access-Control-Allow-Origin,Access-Control-Allow-Credentials
  



  
    cors.support.credentials
    true
  
  
    cors.preflight.maxage
    10
  


  CorsFilter
  /*



web端跨域访问方式:

function Base64_Encode(str) {
    var c1, c2, c3;
    var base64EncodeChars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
    var i = 0, len = str.length, string = '';

    while (i < len) {
        c1 = str.charCodeAt(i++) & 0xff;
        if (i === len) {
            string += base64EncodeChars.charAt(c1 >> 2);
            string += base64EncodeChars.charAt((c1 & 0x3) << 4);
            string += "==";
            break;
        }
        c2 = str.charCodeAt(i++);
        if (i === len) {
            string += base64EncodeChars.charAt(c1 >> 2);
            string += base64EncodeChars.charAt(((c1 & 0x3) << 4) | ((c2 & 0xF0) >> 4));
            string += base64EncodeChars.charAt((c2 & 0xF) << 2);
            string += "=";
            break;
        }
        c3 = str.charCodeAt(i++);
        string += base64EncodeChars.charAt(c1 >> 2);
        string += base64EncodeChars.charAt(((c1 & 0x3) << 4) | ((c2 & 0xF0) >> 4));
        string += base64EncodeChars.charAt(((c2 & 0xF) << 2) | ((c3 & 0xC0) >> 6));
        string += base64EncodeChars.charAt(c3 & 0x3F);
    }
    return string;
};
//geoserver服务rest api封装
function rest_gs_layers(gs_url,callback)
{   //http://192.168.30.114:8080/geoserver/rest/layers
    //var apiurl = webconfig.geoserver_url;
    //gs_url=http://192.168.30.114:8080/geoserver
    var auth=new Base64().encode('admin:geoserver');  //
    var auth2=Base64_Encode('admin:geoserver');
    //alert(auth+"_"+auth2);
    //YWRtaW46Z2Vvc2VydmVy   Base64().encode
    //YWRtaW46Z2Vvc2VydmVy   Base64_Encode
    //YWRtaW46Z2Vvc2VydmVy   Postman authorization
    $.ajax({
            type: "get",
            datatype: "json",
            url: gs_url + "/rest/layers",
            headers: {
                "Authorization":"Basic "+auth2
            },
            success: function (c) {
                if(callback) {
                    callback(c);
                }
            },
            error: function (ex) {
                alert(ex.responseText);
            }
        }
    );
};

方法3:使用代理
自己写一个proxy.php,proxy.ashx,proxy.xxx

proxy.config




  
    
    
    


proxy.ashx 代理arcgis rest的方法

<%@ WebHandler Language="C#" Class="proxy" %>
/*
  This proxy page does not have any security checks. It is highly recommended
  that a user deploying this proxy page on their web server, add appropriate
  security checks, for example checking request path, username/password, target
  url, etc.
*/
using System;
using System.Drawing;
using System.IO;
using System.Web;
using System.Collections.Generic;
using System.Text;
using System.Xml.Serialization;
using System.Web.Caching;

/// 
/// Forwards requests to an ArcGIS Server REST resource. Uses information in
/// the proxy.config file to determine properties of the server.
/// 
public class proxy : IHttpHandler {
  
    public void ProcessRequest (HttpContext context) {

        HttpResponse response = context.Response;

        // Get the URL requested by the client (take the entire querystring at once
        //  to handle the case of the URL itself containing querystring parameters)
        string uri = context.Request.Url.Query.Substring(1);

        // Get token, if applicable, and append to the request
        string token = getTokenFromConfigFile(uri);
        if (!String.IsNullOrEmpty(token))
        {
            if (uri.Contains("?"))
                uri += "&token=" + token;
            else
                uri += "?token=" + token;
        }
        		
        System.Net.HttpWebRequest req = (System.Net.HttpWebRequest)System.Net.HttpWebRequest.Create(uri);
        req.Method = context.Request.HttpMethod;
        req.ServicePoint.Expect100Continue = false;
                
        // Set body of request for POST requests
        if (context.Request.InputStream.Length > 0)
        {
            byte[] bytes = new byte[context.Request.InputStream.Length];
            context.Request.InputStream.Read(bytes, 0, (int)context.Request.InputStream.Length);
            req.ContentLength = bytes.Length;
            
            string ctype = context.Request.ContentType;
            if (String.IsNullOrEmpty(ctype)) {
              req.ContentType = "application/x-www-form-urlencoded";
            }
            else {
              req.ContentType = ctype;
            }
            
            using (Stream outputStream = req.GetRequestStream())
            {
                outputStream.Write(bytes, 0, bytes.Length);
            }
        }
    
        // Send the request to the server
        System.Net.WebResponse serverResponse = null;
        try
        {
            serverResponse = req.GetResponse();
        }
        catch (System.Net.WebException webExc)
        {
            response.StatusCode = 500;
            response.StatusDescription = webExc.Status.ToString();
            response.Write(webExc.Response);
            response.End();
            return;
        }
        
        // Set up the response to the client
        if (serverResponse != null) {
            response.ContentType = serverResponse.ContentType;
            using (Stream byteStream = serverResponse.GetResponseStream())
            {

                // Text response
                if (serverResponse.ContentType.Contains("text") || 
                    serverResponse.ContentType.Contains("json"))
                {
                    using (StreamReader sr = new StreamReader(byteStream))
                    {
                        string strResponse = sr.ReadToEnd();
                        response.Write(strResponse);
                    }
                }
                else
                {
                    // Binary response (image, lyr file, other binary file)
                    BinaryReader br = new BinaryReader(byteStream);
                    byte[] outb = br.ReadBytes((int)serverResponse.ContentLength);
                    br.Close();

                    // Tell client not to cache the image since it's dynamic
                    response.CacheControl = "no-cache";

                    // Send the image to the client
                    // (Note: if large images/files sent, could modify this to send in chunks)
                    response.OutputStream.Write(outb, 0, outb.Length);
                }

                serverResponse.Close();
            }
        }
        response.End();
    }
 
    public bool IsReusable {
        get {
            return false;
        }
    }

    // Gets the token for a server URL from a configuration file
    // TODO: ?modify so can generate a new short-lived token from username/password in the config file
    private string getTokenFromConfigFile(string uri)
    {
        try
        {
            ProxyConfig config = ProxyConfig.GetCurrentConfig();
            if (config != null)
                return config.GetToken(uri);
            else
                throw new ApplicationException(
                    "Proxy.config file does not exist at application root, or is not readable.");
        }
        catch (InvalidOperationException)
        {
            // Proxy is being used for an unsupported service (proxy.config has mustMatch="true")
            HttpResponse response = HttpContext.Current.Response;
            response.StatusCode = (int)System.Net.HttpStatusCode.Forbidden;
            response.End();
        }
        catch (Exception e)
        {
            if (e is ApplicationException)
                throw e;
            
            // just return an empty string at this point
            // -- may want to throw an exception, or add to a log file
        }
        
        return string.Empty;
    }
}

[XmlRoot("ProxyConfig")]
public class ProxyConfig
{
    #region Static Members

    private static object _lockobject = new object();

    public static ProxyConfig LoadProxyConfig(string fileName)
    {
        ProxyConfig config = null;

        lock (_lockobject)
        {
            if (System.IO.File.Exists(fileName))
            {
                XmlSerializer reader = new XmlSerializer(typeof(ProxyConfig));
                using (System.IO.StreamReader file = new System.IO.StreamReader(fileName))
                {
                    config = (ProxyConfig)reader.Deserialize(file);
                }
            }
        }

        return config;
    }

    public static ProxyConfig GetCurrentConfig()
    {
        ProxyConfig config = HttpRuntime.Cache["proxyConfig"] as ProxyConfig;
        if (config == null)
        {
            string fileName = GetFilename(HttpContext.Current);
            config = LoadProxyConfig(fileName);

            if (config != null)
            {
                CacheDependency dep = new CacheDependency(fileName);
                HttpRuntime.Cache.Insert("proxyConfig", config, dep);
            }
        }

        return config;
    }

    public static string GetFilename(HttpContext context)
    {
        return context.Server.MapPath("~/proxy.config");
    }
    #endregion

    ServerUrl[] serverUrls;
    bool mustMatch;

    [XmlArray("serverUrls")]
    [XmlArrayItem("serverUrl")]
    public ServerUrl[] ServerUrls
    {
        get { return this.serverUrls; }
        set { this.serverUrls = value; }
    }

    [XmlAttribute("mustMatch")]
    public bool MustMatch
    {
        get { return mustMatch; }
        set { mustMatch = value; }
    }

    public string GetToken(string uri)
    {
        foreach (ServerUrl su in serverUrls)
        {
            if (su.MatchAll && uri.StartsWith(su.Url, StringComparison.InvariantCultureIgnoreCase))
            {
                return su.Token;
            }
            else
            {
                if (String.Compare(uri, su.Url, StringComparison.InvariantCultureIgnoreCase) == 0)
                    return su.Token;
            }
        }

        if (mustMatch)
            throw new InvalidOperationException();

        return string.Empty;
    }
}

public class ServerUrl
{
    string url;
    bool matchAll;
    string token;

    [XmlAttribute("url")]
    public string Url
    {
        get { return url; }
        set { url = value; }
    }

    [XmlAttribute("matchAll")]
    public bool MatchAll
    {
        get { return matchAll; }
        set { matchAll = value; }
    }

    [XmlAttribute("token")]
    public string Token
    {
        get { return token; }
        set { token = value; }
    }
}

网上有许多代理的文章

你可能感兴趣的:(geoserver)