灰熊+泽西岛仅在Localhost上收听 [英] Grizzly + Jersey Listening ONLY on Localhost
问题描述
我正在使用带有Grizzly嵌入式版本的Jersey,我只想在本地主机上绑定/监听.我正在使用带有create调用的GrizzlyWebContainerFactory创建ThreadSelector:
I'm using Jersey with an embedded version of Grizzly and I'd like to bind/listen on localhost ONLY. I'm creating the ThreadSelector using the GrizzlyWebContainerFactory with the create call:
threadSelector = GrizzlyWebContainerFactory.create("http://127.0.0.1:8080/", initParams);
这可行,但是我仍然可以从外部计算机上访问服务器.我如何才能将其绑定到/仅在本地主机上监听?
This works, but I'm still able to hit the server from an external machine. How can I get it to bind to/listen on ONLY localhost?
这是用于配置的东西,所以我不希望有任何其他东西能够连接到该服务器.
This is for configuration stuff, so I don't want anything off box to be able to connect to this server.
推荐答案
我能够使用泽西2.3.1中带有嵌入式版本Grizzly的主机名localhost
来做到这一点:
I was able to do this using the hostname localhost
in Jersey 2.3.1 with an embedded version of 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 "http://0.0.0.0:9580/my-app/"
或"http://myhost.com:9580/my-app/"
启动Grizzly服务器时,我可以使用
Whereas when starting the Grizzly server with the URIs "http://0.0.0.0:9580/my-app/"
, or "http://myhost.com:9580/my-app/"
I am be able to hit it with
> curl -I http://myhost.com:9580/my-app
HTTP/1.1 200 Not Found
...
这是一张表格,其中列出了使用GrizzlyHttpServerFactory
时哪些主机使用哪些URL.据我所知,这里没有什么惊奇之处:
Here's a table of which hosts work with which URLs when using GrizzlyHttpServerFactory
. No surprises here, as far as I understand:
# 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
这篇关于灰熊+泽西岛仅在Localhost上收听的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!