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.
LVL 30
AxterAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Hugh McCurdyCommented:
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.
0
for_yanCommented:
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
0
AxterAuthor Commented:
>>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?
0
Cloud Class® Course: CompTIA Cloud+

The CompTIA Cloud+ Basic training course will teach you about cloud concepts and models, data storage, networking, and network infrastructure.

for_yanCommented:
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

0
for_yanCommented:
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
0
AxterAuthor Commented:
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 %)?
0
for_yanCommented:
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






0
for_yanCommented:
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 "%"


0
AxterAuthor Commented:
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

0
for_yanCommented:
I'd just go therouyugh the array and check
if there is a string which begiins with getRemoteHost() - then I woud think it is local

Run the test with local client and with non-local client:

Something like that
out.println("getRemoteHost=" + request.getRemoteHost() + "<br/>\n");
String remoteHost =  request.getRemoteHost(); 
		InetAddress iaa = InetAddress.getLocalHost();
		String hostName = iaa.getHostName();
		InetAddress[] iad;
		iad = InetAddress.getAllByName(hostName);
		for (int jj = 0; jj < iad.length; jj++)
		{
    if(iad[jj].getHostAddress().startsWith(remoteHost)){//do something when it matches and break}
			
		}

// do something when it does not match

Open in new window



0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
crazedsanityCommented:
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.
0
AxterAuthor Commented:
>>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.
0
AxterAuthor Commented:
Sorry!
Please disregard above post.
I posted it in wrong link.
0
AxterAuthor Commented:
hmmm!
Disregard my disregard..... :-)

I thought I posted it on wrong link.
0
crazedsanityCommented:
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.
0
AxterAuthor Commented:
There doesn't seem to be a better alternative, so I'll accept this solution.

Thanks
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
JSP

From novice to tech pro — start learning today.