1 Importjavax.servlet.http.HttpServletRequest; 2 3 /** 4 * Custom Access Object tool class5 * 6 * Get information such as IP address of object7 * @authorX-rapido8 * 9 */ Ten Public classCusaccessobjectutil { One A /** - * Obtain the user real IP address, do not use REQUEST.GETREMOTEADDR (), the reason is that it is possible that the user uses the proxy software method to avoid the real IP address, - * - * 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 + * A * User Real IP: 192.168.1.110 at * - * @paramRequest - * @return - */ - Public StaticString getipaddress (httpservletrequest request) { -String IP = request.getheader ("X-forwarded-for"); in if(IP = =NULL|| Ip.length () = = 0 | | "Unknown". Equalsignorecase (IP)) { -ip = Request.getheader ("Proxy-client-ip"); to } + if(IP = =NULL|| Ip.length () = = 0 | | "Unknown". Equalsignorecase (IP)) { -ip = Request.getheader ("Wl-proxy-client-ip"); the } * if(IP = =NULL|| Ip.length () = = 0 | | "Unknown". Equalsignorecase (IP)) { $ip = Request.getheader ("Http_client_ip"); Panax Notoginseng } - if(IP = =NULL|| Ip.length () = = 0 | | "Unknown". Equalsignorecase (IP)) { theip = Request.getheader ("Http_x_forwarded_for"); + } A if(IP = =NULL|| Ip.length () = = 0 | | "Unknown". Equalsignorecase (IP)) { theIP =request.getremoteaddr (); + } - returnIP; $ } $ -}
Use HttpRequest in Java to get the real IP address of a user