2011-05-18 23 views
6

Estoy utilizando Jersey con una versión incrustada de Grizzly y me gustaría enlazar/escuchar SOLO en localhost. Estoy creando el ThreadSelector utilizando el GrizzlyWebContainerFactory con la llamada crear:Grizzly + Jersey Escuchando ÚNICAMENTE en Localhost

threadSelector = GrizzlyWebContainerFactory.create("http://127.0.0.1:8080/", initParams); 

Esto funciona, pero sigo siendo capaz de golpear el servidor desde una máquina externa. ¿Cómo puedo vincularlo/escucharlo SOLAMENTE con localhost?

Esto es para cosas de configuración, así que no quiero que nada fuera de la caja pueda conectarse a este servidor.

Respuesta

-2

Puede ampliar fácilmente GrizzlyWebContainerFactory para cumplir este requisito (dado que la clase es definitiva, he creado una utilidad independiente que le permite vincular el socket de escucha al localhost). Puede usar esta utilidad como:

SelectorThread threadSelector = GrizzlyWebContainerFactoryUtil.create("http://127.0.0.1:8080/", initParams, true); 

Al establecer el último parámetro en verdadero lo obliga a vincularse a localhost. El único código añadí para apoyar este requisito es:

selectorThread.setAddress(InetAddress.getByName("localhost")); 

La clase de utilidad entera se muestra a continuación:

import com.sun.grizzly.http.SelectorThread; 
import com.sun.grizzly.http.servlet.ServletAdapter; 
import com.sun.grizzly.standalone.StaticStreamAlgorithm; 
import com.sun.grizzly.tcp.Adapter; 
import com.sun.grizzly.tcp.http11.GrizzlyAdapter; 
import com.sun.jersey.api.container.ContainerException; 
import com.sun.jersey.api.core.ClasspathResourceConfig; 
import com.sun.jersey.spi.container.servlet.ServletContainer; 

import javax.servlet.Servlet; 
import java.io.File; 
import java.io.IOException; 
import java.net.InetAddress; 
import java.net.URI; 
import java.util.Map; 

public class GrizzlyWebContainerFactoryUtil { 

    public static SelectorThread create(String u, Map<String, String> initParams, boolean localHostOnly) 
      throws IOException, IllegalArgumentException { 
     if (u == null) 
      throw new IllegalArgumentException("The URI must not be null"); 

     return create(URI.create(u), initParams, localHostOnly); 
    } 

    public static SelectorThread create(URI u, Map<String, String> initParams, boolean localHostOnly) throws IOException { 
    return create(u, ServletContainer.class, initParams, localHostOnly); 
    } 

    public static SelectorThread create(URI u, Class<? extends Servlet> c, 
             Map<String, String> initParams, boolean localHostOnly) throws IOException { 
    if (u == null) 
     throw new IllegalArgumentException("The URI must not be null"); 

    ServletAdapter adapter = new ServletAdapter(); 
    if (initParams == null) { 
     adapter.addInitParameter(ClasspathResourceConfig.PROPERTY_CLASSPATH, 
     System.getProperty("java.class.path").replace(File.pathSeparatorChar, ';')); 
    } else { 
     for (Map.Entry<String, String> e : initParams.entrySet()) { 
     adapter.addInitParameter(e.getKey(), e.getValue()); 
     } 
    } 

    adapter.setServletInstance(getInstance(c)); 

    String path = u.getPath(); 
    if (path == null) 
     throw new IllegalArgumentException("The URI path, of the URI " + u + 
     ", must be non-null"); 
    else if (path.length() == 0) 
     throw new IllegalArgumentException("The URI path, of the URI " + u + 
     ", must be present"); 
    else if (path.charAt(0) != '/') 
     throw new IllegalArgumentException("The URI path, of the URI " + u + 
     ". must start with a '/'"); 

    if (path.length() > 1) { 
     if (path.endsWith("/")) 
     path = path.substring(0, path.length() - 1); 
     adapter.setContextPath(path); 
    } 

    return create(u, adapter, localHostOnly); 
    } 

    private static Servlet getInstance(Class<? extends Servlet> c) { 
    try { 
     return c.newInstance(); 
    } catch (Exception e) { 
     throw new ContainerException(e); 
    } 
    } 


    public static SelectorThread create(URI u, Adapter adapter, boolean localHostOnly) 
    throws IOException, IllegalArgumentException { 
    if (u == null) 
     throw new IllegalArgumentException("The URI must not be null"); 

    // TODO support https 
    final String scheme = u.getScheme(); 
    if (!scheme.equalsIgnoreCase("http")) 
     throw new IllegalArgumentException("The URI scheme, of the URI " + u + 
     ", must be equal (ignoring case) to 'http'"); 

    if (adapter instanceof GrizzlyAdapter) { 
     GrizzlyAdapter ga = (GrizzlyAdapter) adapter; 
     ga.setResourcesContextPath(u.getRawPath()); 
    } 

    final SelectorThread selectorThread = new SelectorThread(); 

    selectorThread.setAlgorithmClassName(StaticStreamAlgorithm.class.getName()); 

    final int port = (u.getPort() == -1) ? 80 : u.getPort(); 
    selectorThread.setPort(port); 

    if (localHostOnly) { 
     selectorThread.setAddress(InetAddress.getByName("localhost")); 
    } 
    selectorThread.setAdapter(adapter); 

    try { 
     selectorThread.listen(); 
    } catch (InstantiationException e) { 
     IOException _e = new IOException(); 
     _e.initCause(e); 
     throw _e; 
    } 
    return selectorThread; 
    } 

} 
11

yo era capaz de hacer esto utilizando el nombre de host localhost en Jersey 2.3.1 con una versión incorporada de Grizzly:

import org.glassfish.jersey.grizzly2.httpserver.GrizzlyHttpServerFactory; 
// ... 
GrizzlyHttpServerFactory.createHttpServer(
    URI.create("http://localhost:9580/my-app/") 
); 

Prueba resulta en:

> curl -I http://myhost.com:9580/my-app 
curl: (7) couldn't connect to host 

Mientras que al iniciar el servidor del grisáceo con los URI "http://0.0.0.0:9580/my-app/", o "http://myhost.com:9580/my-app/" estoy ser capaz de golpear con

> curl -I http://myhost.com:9580/my-app 
HTTP/1.1 200 Not Found 
... 

Aquí hay una tabla de los cuales alberga el trabajo con el que las direcciones URL cuando se utiliza GrizzlyHttpServerFactory. No hay sorpresas aquí, por lo que entiendo:

# For http://0.0.0.0:9575/my-app  | Works? 
curl -I http://0.0.0.0:9575/my-app | Yes 
curl -I http://127.0.0.1:9575/my-app | Yes 
curl -I http://localhost:9575/my-app | Yes 
curl -I http://myhost.com:9575/my-app | Yes 
             | 
# For http://127.0.0.1:9575/my-app | 
# For http://localhost:9575/my-app | 
curl -I http://0.0.0.0:9575/my-app | Yes 
curl -I http://127.0.0.1:9575/my-app | Yes 
curl -I http://localhost:9575/my-app | Yes 
curl -I http://myhost.com:9575/my-app | No 
             | 
# For http://myhost.com:9585/my-app | 
curl -I http://0.0.0.0:9585/my-app | No 
curl -I http://127.0.0.1:9585/my-app | No 
curl -I http://localhost:9575/my-app | No 
curl -I http://myhost.com:9585/my-app | Yes 
Cuestiones relacionadas