How to get the IP of the client

Source: Internet
Author: User

In Java, the HttpServletRequest interface provides a getremoteaddr () method to obtain the client IP address (which is actually inherited from the ServletRequest interface) and is simple to use, as follows:

Suppose an existing HttpServletRequest object request (default in JSP)

String ipaddress = Request.getremoteaddr ();

But this method has a fatal flaw, is cannot penetrate the proxy server. When the proxy server is used in the system architecture, the above method captures the IP address of the proxy server. In the project development of large and medium-sized Java-ee architecture, it is not OK to use the cluster and proxy in the application server. I have a history of the technology related to the Java EE data, could not find an arrow through the proxy server directly available IP capture method. But the Java EE is powerful, the solution of course is not missing, here is a brief introduction to our solution--using applets with digital signatures.

We know that applets are downloaded to the client and run on the client's JVM, and using applets is a small case when the applet runs local resources that do not involve the client (this is the technique for dynamically displaying trading curves in many stock sites), However, when you want to obtain local resource information (even if you are just grabbing an IP address), be sure to digitally sign and authenticate the applet (see the Java Security mechanism for details).

The applet development process with digital signature and authentication is as follows:

(1) Develop and compile the Java source program for the applet.

The applet code that crawls the native (ie client) IP is as follows:
Import java.applet.*;
Import java.net.*;
Import java.awt.*;

public class Getipapp extends Applet
{
Public String Myip ()
{

String ipd= "";
try{
Using the method of fetching native IP getlocalhost (), returns a InetAddress object
InetAddress Addr=inetaddress.getlocalhost ();

Take the data out of the InetAddress object and put it in a byte-type array
Byte[] Ipaddr=addr.getaddress ();
int i=0;
int ipget=0;
Assembling string-Type IP addresses
while (I<ipaddr.length) {
Ipget=ipaddr[i];
Converts the returned value to a collation
if (ipget<0) {ipget=256+ipget;}
if (i==0) {
Ipd=ipd+ipget;
}else{
Ipd=ipd+ "." +ipget;
}
i++;
}
}catch (Unknownhostexception e) {
}
return IPD;
}

Public Getipapp ()
{
}

Initialize the Applet
public void Start ()
{
}
}

(2) Encapsulation of class files and resource files with a Jar tool.

Under current directory:
Jar CVF Myip.jar *.class

(3) Create the public key and the key with Keytool to generate X. 509V1 signed certificate, output certificate.

Under current directory:
Keytool-genkey-keystore Myip.keystore-alias Myip

Description: KeyStore will be used to store keys (private keys) and public key authentication, this command generated a keystore file named Myip.keystore, and then this command, the system will then be prompted to fill out a number of content, such as the creator, company name , address, set the password and so on, casually fill in and complete.

(4) Import the public key into the security certificate CER file.

Under current directory:
Keytool-export-keystore Myip.keystore-alias Myip-file Myip.cer

Note: This command generates a security certificate file named Myip.cer (this is also the file that the client needs to run the applet that accesses the local resource), and the command prompts you to enter the password and enter the previously set password.

At this point, a digitally signed and certified applet development is completed. Once this applet runs through the client's license validation (details below), it can crawl to the client's IP address and then deliver the address to the server. In practice, we use JavaScript scripts to communicate directly with applets, store crawled IP addresses into page elements, and then submit the IP addresses to the server through the form, as follows.

(5) The WEB (typically the login page) gets the Setup method for the applet parameters:

Add between <body> and </body>:
<applet code= "Getipapp.class" codebase = "" archive = "Myip.jar"
Width= "0" height= "0" name= "Getipapp" >
</applet>
and <input type= "hidden" name= "Iptxt" > (This is the page element in the Web page that holds the applet parameter)

is important:

Add between <script language=javascript>
function Getmyip ()
{
Suppose <input type= "hidden" name= "iptxt" > elements in the Form1 form
Window.document.form1.iptxt.value=window.document.getipapp.myip ();
}
</SCRIPT >

Note: Use JavaScript to treat the applet as a page element and provide simple communication to get the client IP address from the applet directly from the page and submit it to the server via the form (method slightly).

Now, what the client needs to do is install the JRE virtual machine (recommendation 1.4.1 above), and you will see a "Java Plug-in" icon in the Control Panel after a successful installation in the Windows system, and then open the program in the browser Confirm the browser (ie or Netscape) on which the system is used. Restart the browser, access the server with the applet on the Web page, the authentication information for the security certificate is automatically ejected, and when the certificate is validated, the applet with the certificate will be able to do whatever you like on your machine (hehe, you haven't done anything to inquire about the IP address on your machine) at the same time The security certificate you issued is automatically imported into the "certificate → signed applet" of the Java Plug-in.

The

said so much, in fact, mainly introduced through the proxy server to obtain client IP applet a complete solution. I believe there are many solutions under the Java EE Architecture, I hope we can exchange a lot. (in addition, this scheme only gets the primary IP of the machine, to take all the IP of this machine in the applet's source code to make some changes.) )

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.