Java Get IP Address

Source: Internet
Author: User
Tags get ip

There are many applications that require access to the IP address of the visitor, such as preventing unauthorized users or malicious users from accessing them. Java EE's servlet standard interface Javax.servlet.http.HttpServletRequest provides the GETREMOTEADDR () method for obtaining the visitor's IP address.

REQUEST.GETREMOTEADDR () This approach is valid for most cases of IP access. However, the client can not get the real IP address of the clients through to the agent software. 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, in the forwarding request HTTP header information, the X-forwarded-for information is added to track the original client IP address and the server address of the original client request. So want to get the real IP of the client must first Judge Request.getheader ();


Java gets the IP address code:

PUBLIC STATIC 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;    }


Reference:

http://xiaoboss.iteye.com/blog/1181488

Http://www.cnblogs.com/xuekyo/archive/2012/05/17/2505772.html



Java Get IP 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.