Java 灰熊&x2B;仅在本地主机上侦听

Java 灰熊&x2B;仅在本地主机上侦听,java,jersey,grizzly,Java,Jersey,Grizzly,我将Jersey与Grizzly的嵌入式版本一起使用,我只想在localhost上绑定/侦听。我正在通过create调用使用GrizzlyWebContainerFactory创建线程选择器: threadSelector = GrizzlyWebContainerFactory.create("http://127.0.0.1:8080/", initParams); 这是可行的,但我仍然能够从外部机器访问服务器。如何使其仅绑定到/侦听本地主机 这是用于配置的东西,所以我不希望任何现成的东西

我将Jersey与Grizzly的嵌入式版本一起使用,我只想在localhost上绑定/侦听。我正在通过create调用使用GrizzlyWebContainerFactory创建线程选择器:

threadSelector = GrizzlyWebContainerFactory.create("http://127.0.0.1:8080/", initParams);
这是可行的,但我仍然能够从外部机器访问服务器。如何使其仅绑定到/侦听本地主机


这是用于配置的东西,所以我不希望任何现成的东西能够连接到此服务器。

您可以轻松地扩展GrizzlyWebContainerFactory以支持此要求(因为类是最终的,所以我创建了一个自包含的实用程序,允许您将侦听套接字绑定到本地主机)。您可以将此实用程序用作:

SelectorThread threadSelector = GrizzlyWebContainerFactoryUtil.create("http://127.0.0.1:8080/", initParams, true);
将最后一个参数设置为true将强制它绑定到localhost。我为支持此要求而添加的唯一代码是:

selectorThread.setAddress(InetAddress.getByName("localhost"));
整个实用程序类如下所示:

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;
  }

}
import com.sun.grizzly.http.selectorhread;
导入com.sun.grizzly.http.servlet.ServletAdapter;
导入com.sun.grizzly.standalone.StaticStreamAlgorithm;
导入com.sun.grizzly.tcp.Adapter;
导入com.sun.grizzly.tcp.http11.GrizzlyAdapter;
导入com.sun.jersey.api.container.ContainerException;
导入com.sun.jersey.api.core.ClasspathResourceConfig;
导入com.sun.jersey.spi.container.servlet.ServletContainer;
导入javax.servlet.servlet;
导入java.io.File;
导入java.io.IOException;
导入java.net.InetAddress;
导入java.net.URI;
导入java.util.Map;
公共类GrizzlyWebContainerFactoryUtil{
公共静态选择器读取创建(字符串u、映射initParams、布尔localHostOnly)
抛出IOException、IllegalArgumentException{
如果(u==null)
抛出新的IllegalArgumentException(“URI不能为null”);
返回create(URI.create(u)、initParams、localHostOnly);
}
公共静态选择器读取创建(URI u、Map initParams、boolean localHostOnly)引发IOException{
返回create(u,ServletContainer.class,initParams,localHostOnly);
}

publicstaticselectorreadcreate(uriu,ClassI能够通过Jersey 2.3.1中的主机名
localhost
和Grizzly的嵌入式版本来实现这一点:

import org.glassfish.jersey.grizzly2.httpserver.GrizzlyHttpServerFactory;
// ...
GrizzlyHttpServerFactory.createHttpServer(
    URI.create("http://localhost:9580/my-app/")
);
测试结果如下:

> curl -I http://myhost.com:9580/my-app
curl: (7) couldn't connect to host
而当用URI启动Grizzly服务器时http://0.0.0.0:9580/my-app/“
,或
”http://myhost.com:9580/my-app/“
我可以用

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

以下是使用
GrizzlyHttpServerFactory
时,哪些主机使用哪些URL的表格。据我所知,这并不奇怪:

# 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