Java Get client ID address

Source: Internet
Author: User
Tags get ip

Ext.: http://zhenchengchagangzi.iteye.com/blog/1199300#bc2372048

In the JSP, the method to obtain the IP address of the client is: Request.getremoteaddr (), which is valid in most cases. However, the real IP address of the client cannot be obtained through the reverse proxy software such as Apache,squid.

If the reverse proxy software is used, the URL of the http://192.168.1.110:2046/is reverse proxy to http://www.javapeixun.com.cn/URL, with REQUEST.GETREMOTEADDR () The IP address obtained by the method is: 127.0.0.1 or 192.168.1.110, not the real IP of the client.

After the agent, due to the addition of the middle tier between the client and the service, so the server can not directly get the client's IP, the server-side application can not directly forward the requested address to the client. However, the x-forwarded-for information is added to the HTTP header information of the forwarding request. Used to track the original client IP address and the server address of the original client request. When we visit http://www.javapeixun.com.cn/index.jsp/, it is not that our browser actually accesses the index.jsp file on the server, but first the proxy server accesses HTTP/ 192.168.1.110:2046/INDEX.JSP, the proxy server will return the results of the access to our browser, because it is the proxy server to access the index.jsp, so index.jsp through the request.getremoteaddr ( Method gets the IP address of the proxy server, not the IP address of the client.

So we can get the real IP address of the client method one:
Public String Getremortip (HttpServletRequest request) {
if (Request.getheader ("x-forwarded-for") = = null) {
return request.getremoteaddr ();
}
Return Request.getheader ("X-forwarded-for");
}
When I visit http://www.5a520.cn/index.jsp/, however, the IP address returned is always unknown, not the 127.0.0.1 or 192.168.1.110 as shown above, and I visit http:// 192.168.1.110:2046/index.jsp, you can return the real IP address of the client and write a method to verify it. The reason was on the squid. The squid.conf configuration file Forwarded_for entry is on by default, if Forwarded_for is set to OFF: X-forwarded-for:unknown

Then we can obtain the client real IP address method two:
Public String getipaddr (HttpServletRequest request) {
String IP = request.getheader ("X-forwarded-for");
if (IP = = NULL | | ip.length () = = 0 | | "Unknown". Equalsignorecase (IP)) {
ip = Request.getheader ("Proxy-client-ip");
}
if (IP = = NULL | | ip.length () = = 0 | | "Unknown". Equalsignorecase (IP)) {
ip = Request.getheader ("Wl-proxy-client-ip");
}
if (IP = = NULL | | ip.length () = = 0 | | "Unknown". Equalsignorecase (IP)) {
ip = request.getremoteaddr ();
}
return IP;
}
However, if through the multi-level reverse proxy, x-forwarded-for value and more than one, but a string of IP values, exactly which is the real client IP?

The answer is to take the first non-unknown valid IP string in x-forwarded-for.

such as: x-forwarded-for:192.168.1.110, 192.168.1.120, 192.168.1.130, 192.168.1.100 user real IP: 192.168.1.110

2008-04-02 16:59 recently made a security system, the user's IP and MAC address needs to be verified, here is used to obtain the client IP and MAC address of two methods, retained.

1. Obtain the client IP address (this must be uploaded from the client to the backend):
JSP page, very simple, request.getremoteaddr ();
Because the view layer of the system is implemented in JSF, there is no direct access to the request on the page, and a cast is made in the bean.
Public String Getmyip () {
try {
Facescontext FC = Facescontext.getcurrentinstance ();
HttpServletRequest request = (HttpServletRequest) fc.getexternalcontext (). Getrequest ();
return request.getremoteaddr ();
}
catch (Exception e) {
E.printstacktrace ();
}
Return "";
}

2. Get the client MAC address
Call window's command, in the background bean implementation through the IP to obtain the MAC address. Here's how:

public string getmacaddress (string IP) {
String str = "";
String macAddress = "";
try {
Process p = runtime.getruntime (). EXEC ("nbtstat-a" + IP);
InputStreamReader ir = new InputStreamReader (P.getinputstream ());
LineNumberReader input = new LineNumberReader (IR);
for (int i = 1; i <; i++) {
str = Input.readline ();
if (str! = null) {
if (Str.indexof ("MAC Address") > 1) {
macAddress = str.substring (Str.indexof ("MAC Address") + +, str.length ());
Break
}
}
}
} catch (IOException e) {
E.printstacktrace (System.out);
}
return macAddress;
}

Add:
On the way to get IP address, recently there is a lesson under Linux, if you simply get the IP address through inetaddress, it will appear on different machines IP address different problems.
Inetaddress.getlocalhost (). GetAddress () is actually getting the IP address according to hostname. Linux system has just loaded the default hostname is localhost, so the above code to obtain the native IP is 127.0.0.1, corresponding, such as my hostname is rjlin.atsig.com The IP address returned is indeed the atsig.com address. The following code is used for the time being, and of course not flexible enough:
public static byte[] GetIP () throws Unknownhostexception {
Byte[] B = inetaddress.getlocalhost (). getaddress ();
Enumeration allnetinterfaces = null;
try {
Allnetinterfaces = Networkinterface.getnetworkinterfaces ();
} catch (SocketException e) {
E.printstacktrace ();
}
inetaddress IP = null;
NetworkInterface netinterface = null;
while (Allnetinterfaces.hasmoreelements ()) {
NetInterface = (networkinterface) allnetinterfaces.nextelement ();
if (Netinterface.getname (). Trim (). Equals ("eth0")) {
Enumeration addresses = Netinterface.getinetaddresses ();
while (Addresses.hasmoreelements ()) {
ip = (inetaddress) addresses.nextelement ();
}
Break
}
}
if (IP! = null && IP instanceof inet4address) {
return B = ip.getaddress ();
}
return b;
}

Java Get client ID address

Related Article

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.