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.
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/")
);
Testing results in:
> curl -I http://myhost.com:9580/my-app
curl: (7) couldn't connect to host
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
...
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With