How to get the client's IP

xiaoxiao2021-03-06  91

The GetRemoteAddr () method provides a getRemoteAddr () method to get the client IP address (actually inheriting from the servletRequest interface), the method is as follows:

Assume that there is already an HTTPSERVLETREQUEST object Request (Dewered in JSP)

String ipaddress = Request.getRemoteAddr ();

But this method has fatal defects, which is not to penetrate the proxy server. When the proxy server is used in the system architecture, the above method is caught is the IP address of the proxy server. In project development of large and medium J2EE architectures, Java application servers often need to use clusters and agents, so the above methods are not feasible. The author entered the J2EE technical information and failed to find an IP crawler approach that can be used as a direct access to the proxy server. But J2EE is powerful, the way is of course not lacking, here to introduce our solution - use the applet with a digital signature.

We know that applet is downloaded to the client, running on the client's JVM. When the applet runs the local resource that does not involve the client, it is a small case (the technique of dynamically displaying the transaction curve in many stock websites is this trick), but When you want to get local resource information (even if you just catch an IP address), you must do digital signatures and certifications to the applet (see details about Java security mechanism).

The Applet development process with digital signatures and certifications is as follows:

(1) Develop Applet's Java source program and compile them.

Cut the applet code of this machine (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 {// uses the method of grabbing this machine IP getLocalhost (), returns an inetaddress object inetaddress addr = inetaddress.getlocalHost ();

// Remove the data from the INetAddress object to the array of Byte type byte [] ipaddr = addr.getaddress (); int i = 0; int ipget = 0; // assembling the IP address of the String type (iPaddr. Length) {ipget = ipaddr [i]; // Transforms the returned value to the 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) Package class files and resource files with JAR tools.

Current directory: jar cvf myip.jar * .class

(3) Create a public key and key with KeyTool, generate X. 509V1 signing certificate, output certificate. Current directory: keytool -genkey-readystore myip.keystore -alias myip

Description: KeyStore will be used to store the private keys and public keys. This command generates a keystore file called Myip.KeyStore, then this command, the system will then have a prompt request to fill in some content, such as Create a person, company name, address, set password, etc., then complete it.

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

Current directory: keytool -export-readystore myip.keystore -alias myip -file myip.cer

Description: This command generates a security certificate file called myip.cer (this is also a file required when the client runs access to the applet of the local resource). You will prompt you when you use the command, enter the previously set password.

At this point, a digital signature and authentication of Applet are completed. Once this applet runs, once the client's license verification (details below), you can grab the client's IP address, then pass this address to the server. We use the JavaScript script to communicate directly with Applet in practice, and put the captured IP address into the page element, then submit the IP address to the server via a form, the method is as follows.

(5) Web (generally login page) Get an installation method of applet parameters:

Add: < / applet> and (this is a page element used to store Applet parameters)

Very important:

Add: