Restlet 框架:如何只绑定到本地主机?
Restlet framework: how to bind to localhost only?
我需要构建一个(独立 Java)基于 restlet 的服务,该服务仅在本地主机上侦听,即不允许来自网络的请求。
我正在尝试做显而易见的事情:
Server srv = new Server(Protocol.HTTPS, "localhost", httpsPort);
component.getServers().add(srv);
但该服务仍在 0.0.0.0 上侦听。 :-(
我进入代码,发现 HttpsServerHelper 在创建服务时忽略了主机名:
this.server = HttpsServer.create(new InetSocketAddress(getHelped().getPort()), 0);
类似的代码存在于纯 HTTP 的 HttpServerHelper 中,其中更加清晰。
那么我的问题是:
如何配置 Restlet component/service 只监听本地主机?
我不知道您在独立的 Restlet 应用程序中使用的是哪个服务器。您应该使用默认连接器以外的服务器连接器,我建议您使用 Jetty 连接器。
为此,只需将扩展 org.restlet.ext.jetty
的 jar 放入您的类路径中。
在这种情况下,使用下面的代码应该符合您的需求:
component.getServers().add(Protocol.HTTP, "localhost", 8182);
这是应用程序启动时的相应跟踪:
2015-09-03 09:47:22.180:INFO::jetty-7.1.6.v20100715
2015-09-03 09:47:22.211:INFO::Started SelectChannelConnector@localhost:8182
此外,这里是 Restlet 文档中关于 Restlet 连接器的 link:http://restlet.com/technical-resources/restlet-framework/guide/2.3/core/base/connectors.
希望对你有帮助,
蒂埃里
更简单的方法是使用虚拟主机。
虚拟主机是处理请求时的第一个路由障碍,尤其是它有助于在域上进行路由。
下面是一个示例代码来说明这一点:
Component c = new Component();
c.getServers().add(Protocol.HTTP, 8182);
VirtualHost host = new VirtualHost();
host.setHostDomain("localhost");
c.getHosts().add(host);
host.attach(new Restlet() {
@Override
public void handle(Request request, Response response) {
response.setEntity("hello, world", MediaType.TEXT_PLAIN);
}
});
c.start();
通常,应用程序附加在组件的默认主机上。此默认主机除了根据附加应用程序的上下文路径路由请求外什么都不做:
c.getDefaultHost().attach("/contextPath1", new Test1Application());
c.getDefaultHost().attach("/contextPath2", new Test2Application());
如果您想根据请求路径以外的其他数据过滤调用,虚拟主机可能是解决方案。
这是一张可能对您有所帮助的图表:
http://restlet.com/technical-resources/restlet-framework/tutorials/2.3#part05
我需要构建一个(独立 Java)基于 restlet 的服务,该服务仅在本地主机上侦听,即不允许来自网络的请求。
我正在尝试做显而易见的事情:
Server srv = new Server(Protocol.HTTPS, "localhost", httpsPort);
component.getServers().add(srv);
但该服务仍在 0.0.0.0 上侦听。 :-(
我进入代码,发现 HttpsServerHelper 在创建服务时忽略了主机名:
this.server = HttpsServer.create(new InetSocketAddress(getHelped().getPort()), 0);
类似的代码存在于纯 HTTP 的 HttpServerHelper 中,其中更加清晰。
那么我的问题是:
如何配置 Restlet component/service 只监听本地主机?
我不知道您在独立的 Restlet 应用程序中使用的是哪个服务器。您应该使用默认连接器以外的服务器连接器,我建议您使用 Jetty 连接器。
为此,只需将扩展 org.restlet.ext.jetty
的 jar 放入您的类路径中。
在这种情况下,使用下面的代码应该符合您的需求:
component.getServers().add(Protocol.HTTP, "localhost", 8182);
这是应用程序启动时的相应跟踪:
2015-09-03 09:47:22.180:INFO::jetty-7.1.6.v20100715
2015-09-03 09:47:22.211:INFO::Started SelectChannelConnector@localhost:8182
此外,这里是 Restlet 文档中关于 Restlet 连接器的 link:http://restlet.com/technical-resources/restlet-framework/guide/2.3/core/base/connectors.
希望对你有帮助, 蒂埃里
更简单的方法是使用虚拟主机。 虚拟主机是处理请求时的第一个路由障碍,尤其是它有助于在域上进行路由。
下面是一个示例代码来说明这一点:
Component c = new Component();
c.getServers().add(Protocol.HTTP, 8182);
VirtualHost host = new VirtualHost();
host.setHostDomain("localhost");
c.getHosts().add(host);
host.attach(new Restlet() {
@Override
public void handle(Request request, Response response) {
response.setEntity("hello, world", MediaType.TEXT_PLAIN);
}
});
c.start();
通常,应用程序附加在组件的默认主机上。此默认主机除了根据附加应用程序的上下文路径路由请求外什么都不做:
c.getDefaultHost().attach("/contextPath1", new Test1Application());
c.getDefaultHost().attach("/contextPath2", new Test2Application());
如果您想根据请求路径以外的其他数据过滤调用,虚拟主机可能是解决方案。
这是一张可能对您有所帮助的图表:
http://restlet.com/technical-resources/restlet-framework/tutorials/2.3#part05