Get User IP

Hi all,

I tried to get the User IP from context or other but i don’t find any way to do that …

The goal is to make IP address filter at the user login …

Thank by advance.

I found two different solutions with a little search here on the forum. The first one was by getting implementing the TransactionListener in your application class and using HttpServletRequest Something like this:


public void transactionStart(Application application, Object transactionData) {
   if (transactionData instanceof HttpServletRequest) {
      HttpServletRequest httpServletRequest = (HttpServletRequest) transactionData;
      String ip = httpServletRequest.getRemoteAddr();
      String host = httpServletRequest.getRemoteHost();
   }
}

The other option I found was this:


WebBrowser b = (WebBrowser) getMainWindow().getTerminal();
String ip = b.getAddress();
System.out.println(ip);

That seemed to work as long as long as you didn’t do it in the init() function of your application.

No guarantees, as both of these gave me the result ‘0:0:0:0:0:0:0:1’ but that can be because I run the server locally.

You can find more by searching for Transaction, and ip on the forum and on the wiki.

Hi,

I use the second way above to catch the user IP address and it indeed returns
null
when inside whatever methods called by the application init(), not just inside this last one. I believe the Application does not now about this data until some other method is called to set this information.

What I am curious about is why some time ago, I think before
IT Mill Toolkit
5.3 or so, I could swear that the same code which returns
null
today was working those days, showing the user IP in the logs. But I can be actually wrong, off course.

The “0:0:0:0:0:0:0:1” is shown when I use for example “localhost:8080/app” to access the app with the web browser, but not with “127.0.0.1:8080/app”. It can be perhaps some IPv6 configuration in the operating system, who knows. Could somebody else confirm this?

Did some more testing, this time at work. I got different results, as the setup is a little different. both from localhost and 127.0.0.1 gave me the ip “127.0.0.1” as a return from WebBrowser.getAddress(), but if I put my network ip as url, I also get that ip as an answer.

You’re right Dalton, it seems that I got a IPv6 address at home. I got windows 7 at home and xp at work. W7 seems to have IPv6 support preinstalled. Nice catch!

Found this is a known issue of Vaadin (6.0+): http://dev.vaadin.com/ticket/3181
Hoping this is fixed soon. The ticket is part of 6.1 milestone.

Thanks.

For the record, I’ve discovered the default configuration of
/etc/hosts
here (Ubuntu 9.04/Linux 2.6.28) is to assign the
localhost
name to the
::1
IPv6 address. So when we access the application server with this name, it is the “new” IPv6 working and all those hexadecimal addresses.

If somebody desires to force the use of localhost always as the traditional IPv4 127.0.0.1, just remove the
localhost
from the line starting with
::1
. But please backup the file before changing and also make sure
localhost
is seen in the “127.0.0.1” line.

Thanks for the methods, but the first is never launched by my program and i don’t know why …

The second always return 127.0.0.1 …

Do you know why the first method doesn’t work ? I implements “TransactionListener” and add the correct method with a System.out.println(ip) … Never printed …

Thanks by advance

Hi all,

Sorry to update old message, but the problem is still here for me …

Can you halp me please ???

James,

did you register your class ( which implements TransactionListener) with your application instance via addTransactionListener(…) ? I do not see this in your example, so, I guess, this cold be a problem.

Thanks,

But i put tha followin in my application initialization :

WebApplicationContext webCtx = (WebApplicationContext) getContext();
webCtx.addTransactionListener(this);

It’s not that i need ?

Yes, that’s right. And your transaction listener is never called ?

Yes the Transaction Listener is called !

The code :

public void transactionStart(Application application, Object transactionData)
{
	if (transactionData instanceof HttpServletRequest)
	{
		HttpServletRequest httpServletRequest = (HttpServletRequest) transactionData;
		ip = httpServletRequest.getRemoteAddr();
		System.out.println("IP : " + ip);
	}
}

And the result :

It’s not really that i want …

Okay, this prints out a local address. Sounds obvious, but does your app server (and computer where app server is running) has a non loopback address, e.g. has an active network connection and IP address assigned and you’re calling your webapp from a browser via the IP address, e.g. http://192.168.10.4:8080/myapp and not as http://localhost:8080/myapp or http://127.0.0.1:8080/myapp ?

I just checked with my app and Vaadin 6.1.5

If I call my app via http://localhost:8080/app, Im getting: IP = 0:0:0:0:0:0:0:1%0

And when I type http://192.168.1.37:8080/app, the result is: IP = 192.168.1.37

If you’re calling your app using real IP address in your web browser and still seeing a loopback in getRemoteAddr method, so probably something wrong with your application server - HttpServletRequest does not have anything with Vaadin, rather wiith the servlet container.

Okay, i understand the problem … Thanks for the answer …

But i realize that i want to get CLIENT ip address and not SERVER ip address … lol

I can’t with this method, right ?

James, getRemoteAddr() returns the CLIENT address, just check the servlet API javadoc :slight_smile:

So this is the exact method you need. But because server and client are running opn the same machine now (I assume) - the IP address is the same. Try to open your webapp from another computer via network and check your debug output

Ok sorry i answer too quickly …

So i’ll try with another computer …

But in this case it’s really strange that the way to call the webapp ‘localhost’ or ‘IP’ change the result of the method ‘getRemoteAddr()’ …

But no problems, i trust you and then it will do that i need !

Thanks a lot !

Every computer can have multiple network interfaces. The “127.0.0.1” is just a virtual loopback interface that is always active and always present, even if you do not have a network card, allowing to run the TCP/IP applicaitons and communicate with each other on the same computer.

When you type-in the address in the web browser, it sends a request to the web server. From the address typed, the operating system selects which network interface (from the list of available ones) to use and finally sends a request using that interface.

When you type special workd “localhost” or “127.0.0.1” - operating system always selects the loopback virtual interface.

Web server, typically, if not configured specially, listens for incoming connections from all avaliable network interfaces on the server machine. Including localhost one.

That’s the reason, why you see the different client IP addresses depending on what you typed in the client’s browser - if a request is sent through 127.0.0.1 loopback interface - the web server will receive the request from the same interface and will see the correct client IP address : 127.0.0.1.

Ok,

Thanks for the explanation ! I understand now …

Have a nice day, thanks again for the help !