Link to home
Start Free TrialLog in
Avatar of Axter
AxterFlag for United States of America

asked on

Find out if user is logged in locally (local connection)

How can I determine if the user is logged on using the same machine which the JSP server is running on?

I have Tomcat running on MachineA.
I want to be able to determine when user uses MachineA to access my WebApps application, so I can provide a link to local file path.

If user is logged on remotely, I don't want to display the local file path link.
Avatar of Hugh McCurdy
Hugh McCurdy
Flag of United States of America image

Do you have access to the CGI variable REMOTE_ADDR?  http://www.perlfect.com/articles/cgi_env.shtml

I'm assuming a local login might have an IP like 192.168.7.15 while a remote would be something that doesn't start with 192.168

I don't know if that solves your problem or not.  Depends on what you mean by local and remote.
You can deterime the IP address of the client from whom web request is coming and then detemrimne the
IP address of your local machine- and compare them
Avatar of Axter

ASKER

>>You can deterime the IP address of the client from whom web request is coming and then detemrimne
>>the IP address of your local machine- and compare them

But exactly how do I do that?

I looked at the following API, and that does give me the IP address of the client.
out.println("getRemoteHost=" + request.getRemoteHost() + "<br/>\n");

How do I determine the IP address of the local machine running Tomcat?
this is how you can detertmine IP addresses of any hjhost running java application

InetAddress iaa = InetAddress.getLocalHost();
String hostName = iaa.getHostName();
InetAddress [] iad;
  iad = InetAddress.getAllByName(hostName);
  for (int jj=0; jj<iad.length; jj++){
           Ssytem.out.println( iad[jj].getHostAddress());
                   }

Open in new window

As yout Tomcat is run on yiour server - you just run code above and you should find all addreses of your server - then compare them
with that from the client

I think there still may be cases of some masking ip address or sometihing but in normal
case it should work
Avatar of Axter

ASKER

out.println("getRemoteHost=" + request.getRemoteHost() + "<br/>\n");
		InetAddress iaa = InetAddress.getLocalHost();
		String hostName = iaa.getHostName();
		InetAddress[] iad;
		iad = InetAddress.getAllByName(hostName);
		for (int jj = 0; jj < iad.length; jj++)
		{
			out.println("InetAddress[" + jj + "]=" + iad[jj].getHostAddress() + "<br/>\n");
		}

Open in new window

The above code gives me the following out put:
getRemoteHost=fe80:0:0:0:2535:6f9d:9f4b:dad3
InetAddress[0]=172.19.52.36
InetAddress[1]=192.168.186.1
InetAddress[2]=192.168.133.1
InetAddress[3]=fe80:0:0:0:2535:6f9d:9f4b:dad3%13
InetAddress[4]=fe80:0:0:0:80ff:f97d:b79a:20b7%14
InetAddress[5]=fe80:0:0:0:fcd5:1cdf:3f65:e23e%16
InetAddress[6]=fd01:1111:1:52:2535:6f9d:9f4b:dad3

The 4th one is the one closes matching the IP address, but it has "%13" appended.
Isn't there a cleaner method which can give me the IP address, and why does this method extra characters appended to the IP address (starting with %)?
The 4th one is the one closes matching the IP address, but it has "%13" appended
You mean this one :

InetAddress[3]=fe80:0:0:0:2535:6f9d:9f4b:dad3%13

No, this is not IP address

these are IP addresses:
InetAddress[0]=172.19.52.36
InetAddress[1]=192.168.186.1
InetAddress[2]=192.168.133.1

You should match these ones with the client's IP
I don't think client will provide you with it MAC addres






Oh I see remote host gives you aklso mac addres - iit is interesting I didn't know

check here:
http://download.oracle.com/javase/1.4.2/docs/api/java/net/InetAddress.html#getAllByName%28java.lang.String%29

I allawys use dthe above check of the array,  maybe getHostAddress() will give you lceaner, buyt that may be a real IP, just check

In the worst case just make substring up to "%"


Avatar of Axter

ASKER

getHostAddress gives me the IPv4 version of the IP address, and getRemoteHost is giving me the IPv6 version of the IP address, so they don't compare.
Here's new output.
 
getRemoteHost=fe80:0:0:0:2535:6f9d:9f4b:dad3
getHostAddress=172.19.52.36
InetAddress[0]=172.19.52.36
InetAddress[1]=192.168.186.1
InetAddress[2]=192.168.133.1
InetAddress[3]=fe80:0:0:0:2535:6f9d:9f4b:dad3%13
InetAddress[4]=fe80:0:0:0:80ff:f97d:b79a:20b7%14
InetAddress[5]=fe80:0:0:0:fcd5:1cdf:3f65:e23e%16
InetAddress[6]=fd01:1111:1:52:2535:6f9d:9f4b:dad3

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of for_yan
for_yan
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Why do all the work of checking the server's IP address against the machine connecting?  The server should have a private network address (http://en.wikipedia.org/wiki/Private_network); so if the server is using a private network, check to see if the one connecting has it.

For instance: my public web server has a private network address 192.168.0.76.  Since 192.168.x.x is a private network, any machine that connects with an IP of 192.168.x.x (such as 192.168.0.101) would be a machine on the local network.
Avatar of Axter

ASKER

>>For instance: my public web server has a private network address 192.168.0.76.  Since 192.168.x.x
>>is a private network, any machine that connects with an IP of 192.168.x.x (such as 192.168.0.101)
>>would be a machine on the local network.

That's not what I mean by local.  You're referring to local network access, and I'm referring to local computer access VS network access.

I just want to be able to determine if the user accessing the page is on the same computer which is running tomcat service for my application.
Avatar of Axter

ASKER

Sorry!
Please disregard above post.
I posted it in wrong link.
Avatar of Axter

ASKER

hmmm!
Disregard my disregard..... :-)

I thought I posted it on wrong link.
I understand what's happening; disregard my post.

In PHP, requests coming from the same machine as the server is running on show as 127.0.0.1 (the loopback address).  I would assume the same would be true for Tomcat, but I'd suggest looking at the logs (i.e. tail -f /path/to/file.log in Linux) and generate a request from the server to see for yourself.
Avatar of Axter

ASKER

There doesn't seem to be a better alternative, so I'll accept this solution.

Thanks