Java get client, native Ip__java

Source: Internet
Author: User
Tags get ip zookeeper client
Get client IPIn some cases, the need or access to the user's client's IP, used to statistics or interception, such as some sites need to be shielded from overseas IP access, which requires access to the client IP. The Getremoteaddr () method of the HttpServletRequest object is generally available. However, it is not correct to obtain IP after requesting an HTTP proxy or reverse proxy (such as Ngrok, Nginx, etc.), which is somewhat similar to a springboard. Services after the reverse proxy to get the IP is not "first layer" IP, but the proxy server this "intermediary" IP, and sometimes even a multi-level reverse proxy, that this time how to accurately obtain the client's IP. Originally in the presence of agents, the request header will add some specific information, such as: X-forwarded-for, Proxy-client-ip, Wl-proxy-client-ip and so on to point to IP address, so get IP becomes simple.
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;
}
When multilevel agents exist, the first non-unknown IP is taken
   public static String getipaddradvanced (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 ();

            } if (IP!= null && ip.indexof (",")!=-1) {string[] Ipwithmultiproxy = Ip.split (","); for (int i = 0; I &lT Ipwithmultiproxy.length;
                ++i) {String eachipsegement = ipwithmultiproxy[i]; if (!)
                    Unknown ". Equalsignorecase (eachipsegement)) {IP = eachipsegement;
                Break
    }} return IP; }
Get native IP

What's the application scenario? Take a chestnut, in a distributed application environment, in order to ensure that the timing task only run on a single server, you can in the logic of the timing task with IP judgment (server, such as the public network Linux IP is known), to determine which servers can run scheduled tasks (of course, This scenario can also be implemented using a zookeeper client curator implementation or a distributed lock implementation.

    Public String Getlocalip () throws Exception {try {inetaddress candidateaddress = null; For (Enumeration ifaces = Networkinterface.getnetworkinterfaces (); ifaces.hasmoreelements ();)
                {NetworkInterface iface = (networkinterface) ifaces.nextelement (); For (Enumeration Inetaddrs = Iface.getinetaddresses (); inetaddrs.hasmoreelements ();)
                    {inetaddress inetaddr = (inetaddress) inetaddrs.nextelement ();
                            if (!inetaddr.isloopbackaddress ()) {if (inetaddr.issitelocaladdress ()) {
                        return inetaddr.gethostaddress ();
                        else if (candidateaddress = null) {candidateaddress = InetAddr;  }} if (candidateaddress!= null) {return
            Candidateaddress.gethostaddress (); } inEtaddress jdksuppliedaddress = Inetaddress.getlocalhost ();
        return jdksuppliedaddress.gethostaddress ();
        catch (Exception e) {e.printstacktrace ();
    return null; }

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.