Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Grizzly + Jersey Listening ONLY on Localhost

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.

like image 733
Bill Avatar asked May 18 '11 20:05

Bill


1 Answers

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
like image 81
yegeniy Avatar answered Nov 18 '22 01:11

yegeniy