Wird die Google Maps API von einer verschlüsselten HTTPS Seite aus geladen, zeigen einige Browser (wie z. B. der Internet Explorer) über eine Sicherheitsmeldung an, dass unsichere Objekte geladen werden.

Um diese Meldung zu verhindern gibt es unterschiedliche Möglichkeiten.

Eine Lösung für dieses Problem ist es ein kleines Proxy-Servlet zu schreiben, dass die Google Maps API im Backend lädt.

Hier ein Code Beispiel in JAVA:

package com.esche.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.params.ClientPNames;
import org.apache.http.client.params.CookiePolicy;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;
import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class Proxy extends HttpServlet {

	private static final long serialVersionUID = 1L;
	
	private static final String HTTP = "http:";
	
	private static final String USER_AGENT = "User-Agent";
	
	private static final String TEXT = "text";
	
	private static final String QMARK = "?";
	
	
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doProcess(request, response);
	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doProcess(request, response);
	}

	protected void doProcess(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
	
	  String urlPath = request.getQueryString();
	
	  if(urlPath == null || urlPath.isEmpty()){
		 response.sendError(HttpStatus.SC_NOT_FOUND);
	  }

	  String proxyPath = request.getRequestURI();
	  String user_agent = request.getHeader(USER_AGENT);
	  
	  DefaultHttpClient client = new DefaultHttpClient();
	  HttpGet method = new HttpGet(urlPath);
	  
	  client.getParams().setBooleanParameter(ClientPNames.ALLOW_CIRCULAR_REDIRECTS, false);
	  client.getParams().setParameter(ClientPNames.COOKIE_POLICY, CookiePolicy.BROWSER_COMPATIBILITY);
	  
	  method.addHeader(USER_AGENT, user_agent);
	  
	  HttpEntity entity = null;

	  try {
		HttpResponse httpResponse = client.execute(method);
		
		 int statusCode = httpResponse.getStatusLine().getStatusCode();
		 entity = httpResponse.getEntity();
		 
		  if (statusCode == HttpStatus.SC_OK) {
				String contentType =  entity.getContentType().getValue();
	

		          if (isText(contentType)) { // content is text
		                String resultScript = EntityUtils.toString(entity);
		                		                
		                // replace http: with proxy
		                if (resultScript != null && resultScript.indexOf(proxyPath) == -1) {
		                    resultScript = resultScript.replaceAll(HTTP,proxyPath + QMARK + HTTP);
		                } 
		                
		              	PrintWriter pw = response.getWriter();
		              	pw.print(resultScript);
		              	pw.flush();
		              	pw.close();

		          }else{// content is binary
		                response.setContentType(contentType);
		                Long length = entity.getContentLength();
		                
		                response.setContentLength(length.intValue());

		                ServletOutputStream output = response.getOutputStream();
		                entity.writeTo(output);
		                output.flush();
		                output.close();
		          }
		  }  

	  } catch( Exception ex ) {
		  response.sendError(HttpStatus.SC_INTERNAL_SERVER_ERROR);
	  } finally {
		    if(entity != null)entity.consumeContent();
	      if(method != null)method.abort();// Release the connection.
	      if(client != null)client.getConnectionManager().shutdown(); // shut down the connection manager to ensure immediate deallocation of all system resources
	  } 
	}
	
	
	private static boolean isText(String type){
		if(type == null || type.isEmpty() || type.toLowerCase().indexOf(TEXT) == -1){return false;}
		return true;
	}
}

In diesem Beispiel wird zusätzlich die Apache HttpClient 4 Components Libraries verwendet.

Danach muss noch das „src“ Attribut im Script-Tag angepasst werden.

Ist das Servlet z.B. unter /sslproxy zu erreichen, sieht die Einbindung folgendermaßen aus:

<script src="/sslproxy?http://maps.google.com/maps?file=api&v=2&key={google_map_key}" type="text/javascript"></script> 
Google Maps unter SSL ohne Sicherheitsmeldung laden

Schreibe einen Kommentar

Deine E-Mail-Adresse wird nicht veröffentlicht. Erforderliche Felder sind mit * markiert