我正在使用带有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("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("localhost:9580/my-app/") );测试结果为:
> curl -I myhost:9580/my-app curl: (7) couldn't connect to host当使用URI "0.0.0.0:9580/my-app/"或"myhost:9580/my-app/"启动Grizzly服务器时,我可以使用
Whereas when starting the Grizzly server with the URIs "0.0.0.0:9580/my-app/", or "myhost:9580/my-app/" I am be able to hit it with
> curl -I myhost: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 0.0.0.0:9575/my-app | Works? curl -I 0.0.0.0:9575/my-app | Yes curl -I 127.0.0.1:9575/my-app | Yes curl -I localhost:9575/my-app | Yes curl -I myhost:9575/my-app | Yes | # For 127.0.0.1:9575/my-app | # For localhost:9575/my-app | curl -I 0.0.0.0:9575/my-app | Yes curl -I 127.0.0.1:9575/my-app | Yes curl -I localhost:9575/my-app | Yes curl -I myhost:9575/my-app | No | # For myhost:9585/my-app | curl -I 0.0.0.0:9585/my-app | No curl -I 127.0.0.1:9585/my-app | No curl -I localhost:9575/my-app | No curl -I myhost:9585/my-app | Yes更多推荐
灰熊+泽西岛仅在Localhost上收听
发布评论