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