Java Get User IP

Source: Internet
Author: User

How does Java get the client IP?
Here are a few of the methods I've summed up:

/** * Get client IP address (can penetrate proxy) * * @param request * @return  * *     Public StaticStringgetremoteaddr(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.getheader ("Http_client_ip"); }if(IP = =NULL|| Ip.length () = =0||"Unknown". Equalsignorecase (IP)) {IP = Request.getheader ("Http_x_forwarded_for"); }if(IP = =NULL|| Ip.length () = =0||"Unknown". Equalsignorecase (IP)) {IP = request.getremoteaddr (); }returnip }Private Static FinalString[] Headers_to_try = {"X-forwarded-for","Proxy-client-ip","Wl-proxy-client-ip","Http_x_forwarded_for","http_x_forwarded","Http_x_cluster_client_ip","Http_client_ip","Http_forwarded_for","http_forwarded","Http_via","REMOTE_ADDR","X-real-ip"};/*** * Get client IP address (can penetrate proxy) * @param request * @return  * *     Public StaticStringgetclientipaddress(HttpServletRequest request) { for(String header:headers_to_try) {String IP = request.getheader (header);if(IP! =NULL&& ip.length ()! =0&&!"Unknown". Equalsignorecase (IP)) {returnip }        }returnRequest.getremoteaddr (); }/*** * Get client IP address (can penetrate proxy) * @param request * @return  * *     Public StaticStringgetclientipaddr(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.getheader ("Http_x_forwarded_for"); }if(IP = =NULL|| Ip.length () = =0||"Unknown". Equalsignorecase (IP)) {IP = Request.getheader ("http_x_forwarded"); }if(IP = =NULL|| Ip.length () = =0||"Unknown". Equalsignorecase (IP)) {IP = Request.getheader ("Http_x_cluster_client_ip"); }if(IP = =NULL|| Ip.length () = =0||"Unknown". Equalsignorecase (IP)) {IP = Request.getheader ("Http_client_ip"); }if(IP = =NULL|| Ip.length () = =0||"Unknown". Equalsignorecase (IP)) {IP = Request.getheader ("Http_forwarded_for"); }if(IP = =NULL|| Ip.length () = =0||"Unknown". Equalsignorecase (IP)) {IP = Request.getheader ("http_forwarded"); }if(IP = =NULL|| Ip.length () = =0||"Unknown". Equalsignorecase (IP)) {IP = Request.getheader ("Http_via"); }if(IP = =NULL|| Ip.length () = =0||"Unknown". Equalsignorecase (IP)) {IP = Request.getheader ("REMOTE_ADDR"); }if(IP = =NULL|| Ip.length () = =0||"Unknown". Equalsignorecase (IP)) {IP = request.getremoteaddr (); }returnip } Public StaticStringgetipaddr(HttpServletRequest request) {String IP = request.getheader ("X-real-ip");if(NULL! = IP &&!"". Equals (Ip.trim ()) &&!"Unknown". Equalsignorecase (IP)) {returnip } IP = Request.getheader ("X-forwarded-for");if(NULL! = IP &&!"". Equals (Ip.trim ()) &&!"Unknown". Equalsignorecase (IP)) {//Get first IP from proxy IP            intindex = Ip.indexof (', ');if(Index! =-1) {returnIp.substring (0, index); }Else{returnip }        }returnRequest.getremoteaddr (); }

I am using the Spring MVC Framework, which tests the controller code as follows:

Packagecom. Web. Controller;Import Java. Util. HashMap;Import Java. Util. Map;Import Javax. servlet. HTTP. HttpServletRequest;import org. Springframework. Stereotype. Controller;import org. Springframework. Web. Bind. Annotation. Requestmapping;import org. Springframework. Web. Bind. Annotation. Responsebody;Importcom. Common. Util. Systemhwutil;Importcom. Common. Util. Webservletutil;Importcom. String. Widgets. Util. Valuewidget;Importcom. Util. Jsonputil;/*** * For testing cross-domain * @author huangweii * May 29, 2015 * *@Controller @requestmapping ("/cors") public class Corscontroller {@ResponseBody @RequestMapping (value ="/simple", Produces=systemhwutil. RESPONSE_CONTENTTYPE_JSON_UTF) public String corsjsonsimple (HttpServletRequest request,string callback) {string content;Map map=new HashMap ();Map. Put("username","Good");Map. Put("Age"," the");Map. Put("Address","Beijing");Content=jsonputil. Getjsonp(Map, callback);System. out. println("GETIPADDR:"+webservletutil. Getipaddr(request));System. out. println("GETREMOTEADDR:"+webservletutil. Getremoteaddr(request));System. out. println("GETCLIENTIPADDR:"+webservletutil. Getclientipaddr(request));System. out. println("getclientipaddress:"+webservletutil. Getclientipaddress(request));return content;}}

Test results:

Java Get User IP

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.