Why request.getRemoteAddr() returns ipv4 or ipv6 depending on context (post query or ajax query)

Jerome Cance picture Jerome Cance · Jun 25, 2010 · Viewed 34.4k times · Source

I've donne a web app with Spring/GWT that uses Flash to upload files.

When I send an ajax request with GWT and try to get the user ip address, I get an ipv4 address like: 127.0.0.1

but when I upload my files with flash (and so a post request on the same webapp) I get an ipv6 address 0:0:0:0:0:0:0:1

I use the same code to get the user ip which is:

ServletRequestAttributes att = (ServletRequestAttributes) RequestContextHolder.currentRequestAttributes();
att.getRequest().getRemoteAddr();

The problem is that I use a signature which uses the ip address to be sure that user can upload files (due to bug with session lost when uploading with flash) and the ip address changes when I use ajax or Post via flash.

What's the problem and how can I get the same ip format ?

thanks

Answer

Ted Bigham picture Ted Bigham · Feb 21, 2014

You can fix it by starting your server with this flag

-Djava.net.preferIPv4Stack=true