Tony
Tony

Reputation: 3805

Trouble getting IP of client in ServletRequest

I'm trying to get remote IP using this:

String remoteAddress = ((ServletRequestAttributes) RequestContextHolder
                .currentRequestAttributes()).getRequest().getRemoteAddr();

I call it in controller, but it returns 0:0:0:0:0:0:0:1. What's wrong?

Upvotes: 2

Views: 2766

Answers (1)

nha
nha

Reputation: 18005

0:0:0:0:0:0:0:1 is the loopback adress in IPv6. It's the equivalent of 127.0.0.1 in IPv4.

If you are using only one machine, this seems pretty normal to me. Otherwise, could you please give more details ?

EDIT

If you are trying to get your external (public) IP, then you could have a look at these SO posts :

Get real client IP in a Servlet

Getting the 'external' IP address in Java

How to get external IP successfully

Getting the IP address of the current machine using Java

In short, you can use request.getRemoteAddr(). But it won't return your public IP adress if you are on the same network.

Upvotes: 6

Related Questions