Java – grizzly Jersey listens only on localhost

I'm using jersey with an embedded grizzly version. I just want to bind / listen on localhost I am creating a threadselector using grizzlywebcontainerfactory with a create call:

threadSelector = GrizzlyWebContainerFactory.create("http://127.0.0.1:8080/",initParams);

This works, but I can still call the server from an external machine How can I make it bind to / listen only to the local host?

This is a configuration thing, so I don't want any close boxes to connect to this server

Solution

I can use Jersey 2.3 1 and the embedded version of grizzly to do this:

import org.glassfish.jersey.grizzly2.httpserver.GrizzlyHttpServerFactory;
// ...
GrizzlyHttpServerFactory.createHttpServer(
    URI.create("http://localhost:9580/my-app/")
);

The test results are as follows:

> curl -I http://myhost.com:9580/my-app
curl: (7) Couldn't connect to host

When using URIs“ http://0.0.0.0:9580/my -App / "or“ http://myhost.com:9580/my -When "app /" starts the grizzly server, I can click it

> curl -I http://myhost.com:9580/my-app
HTTP/1.1 200 Not Found
...

The following is a table of which hosts use which URLs when using grizzlyhttpserverfactory As far as I know, there are no surprises here:

# 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
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>