[OpenLayers-Users] Load GeoServer WMS secured layers with Openlayers and proxy.jsp

Tobias Kirschke tobias.kirschke.geoinformatik at gmail.com
Sun Apr 20 06:47:00 PDT 2014


Hello,

I'm trying to load a secured WMS layer from GeoServer in OpenLayers with 
"http://XXXX:XXXX@SERVER/geoserver/wms" but it doesn't work. I think the 
problem is the proxy. I use the following proxy.jsp from 
https://github.com/terrestris/JSP-Whitelist-Proxy/blob/master/proxy.jsp

An entry like Username:Password at SERVER:8080/geoserver/wms as an allowed 
host does not work.

How can I fix the Problem?

Thank you.


<%@page session="false"%>
<%@page import="java.net.*,java.io.*" %>
<%@page trimDirectiveWhitespaces="true"%>
<%

/**
* ...
* @author terrestris GmbH & Co. KG
* @author Christian Mayer
* @author Marc Jansen
*
* @license BSD see license.txt
*
*/
String[] allowedHosts = {
"www.openlayers.org", "openlayers.org",
"labs.metacarta.com", "world.freemap.in",
"prototype.openmnnd.org", "geo.openplans.org",
"sigma.openplans.org", "demo.opengeo.org",
"www.openstreetmap.org", "sample.azavea.com",
"v-swe.uni-muenster.de:8080",
"vmap0.tiles.osgeo.org"
};
HttpURLConnection con = null;
try {
String reqUrl = request.getQueryString();
String decodedUrl = "";
if (reqUrl != null) {
reqUrl = URLDecoder.decode(reqUrl, "UTF-8");
}
else {
response.setStatus(400);
out.println("ERROR 400: No target specified for proxy.");
}

// extract the host
String host = "";
host = reqUrl.split("\\/")[2];
boolean allowed = false;

// check if host (with port) is in white list
for (String surl : allowedHosts) {
if (host.equalsIgnoreCase(surl)) {
allowed = true;
break;
}
}

// do the proxy action (load requested ressource and transport it to client)
// if host is in white list
if(allowed) {
// replace the white spaces with plus in URL
reqUrl = reqUrl.replaceAll(" ", "+");

// call the requested ressource
URL url = new URL(reqUrl);
con = (HttpURLConnection)url.openConnection();
con.setDoOutput(true);
con.setRequestMethod(request.getMethod());
String reqContenType = request.getContentType();
if(reqContenType != null) {
con.setRequestProperty("Content-Type", reqContenType);
}
else {
con.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
}

int clength = request.getContentLength();
if(clength > 0) {
con.setDoInput(true);
byte[] idata = new byte[clength];
request.getInputStream().read(idata, 0, clength);
con.getOutputStream().write(idata, 0, clength);
}

// respond to client
response.setContentType(con.getContentType());

BufferedReader rd = new BufferedReader(new 
InputStreamReader(con.getInputStream()));
String line;
int i = 0;
while ((line = rd.readLine()) != null) {
out.println(line);
}
rd.close();
}
else {
// deny access via HTTP status code 502
response.setStatus(502);
out.println("ERROR 502: This proxy does not allow you to access that 
location.");
}

} catch(Exception e) {

// resond an internal server error with the stacktrace
// on exception
response.setStatus(500);
byte[] idata = new byte[5000];

if(con.getErrorStream() != null) {
con.getErrorStream().read(idata, 0, 5000);
}

out.println("ERROR 500: An internal server error occured. " + 
e.getMessage() + " " + new String(idata));
}
%>



More information about the Users mailing list