Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

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

Posted on 2011-10-03
16
Medium Priority
?
266 Views
Last Modified: 2012-05-12
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.
0
Comment
Question by:Axter
  • 7
  • 6
  • 2
  • +1
16 Comments
 
LVL 13

Expert Comment

by:Hugh McCurdy
ID: 36905275
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
 
LVL 47

Expert Comment

by:for_yan
ID: 36905300
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
 
LVL 30

Author Comment

by:Axter
ID: 36905396
>>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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 47

Expert Comment

by:for_yan
ID: 36905425
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
 
LVL 47

Expert Comment

by:for_yan
ID: 36905436
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
 
LVL 30

Author Comment

by:Axter
ID: 36905485
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
 
LVL 47

Expert Comment

by:for_yan
ID: 36905522
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
 
LVL 47

Expert Comment

by:for_yan
ID: 36905567
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
 
LVL 30

Author Comment

by:Axter
ID: 36905622
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
 
LVL 47

Accepted Solution

by:
for_yan earned 2000 total points
ID: 36905689
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
 
LVL 9

Expert Comment

by:crazedsanity
ID: 36912060
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
 
LVL 30

Author Comment

by:Axter
ID: 36912091
>>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
 
LVL 30

Author Comment

by:Axter
ID: 36912099
Sorry!
Please disregard above post.
I posted it in wrong link.
0
 
LVL 30

Author Comment

by:Axter
ID: 36912109
hmmm!
Disregard my disregard..... :-)

I thought I posted it on wrong link.
0
 
LVL 9

Expert Comment

by:crazedsanity
ID: 36912171
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
 
LVL 30

Author Closing Comment

by:Axter
ID: 36924884
There doesn't seem to be a better alternative, so I'll accept this solution.

Thanks
0

Featured Post

Receive 1:1 tech help

Solve your biggest tech problems alongside global tech experts with 1:1 help.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Hi, in this article I'm going to teach you how to run your own site, and how to let people in (without IP). I'll talk about and explain each step... :) By the way, everything in this Tutorial is completely free and legal. This article is for …
In Solr 4.0 it is possible to atomically (or partially) update individual fields in a document. This article will show the operations possible for atomic updating as well as setting up your Solr instance to be able to perform the actions. One major …
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…
Suggested Courses
Course of the Month12 days, 2 hours left to enroll

564 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question