A feature of recent project to obtain the user's ip address, add the user's system records.
I found that when I directly getRemoteAddr () when the user of the method to get the ip from HttpServletRequet, gets to the ip address of the server , Why is this so?
Internet to find answers :
" When we get the client IP by request, usually in order to protect its own server information or load balancing purposes, for their own proxy server at this time if we do the reverse by request.getRemoteAddr ();. May get to be their own agents IP server, but can not achieve the purpose of obtaining the user's request ip. "
The final solution :
The following finishing each developed their own proxy server forwards the service request header, request header these are not standard http request headers, not necessarily all of these agents will bring the request header, you can only get to the real ip through this way as much as possible but we can not guarantee that you can get to the real ip, ip header and the proxy request acquired may be falsified.
parameter:
nginx service agent | X-Real-IP |
Squid Proxy Service | X-Forwarded-For |
apache service agent | Proxy-Client-IP |
weblogic service agent | WL-Proxy-Client-IP |
Some proxy servers | HTTP_CLIENT_IP |
The following is to get the user's real ip tools:
Package com.pantech.boot.common.systemlog.util; Import org.springframework.beans.factory.annotation.Autowired; Import org.springframework.stereotype.Component; Import the javax.servlet.http.HttpServletRequest; / ** * @author Xiao Zheng Yu * @date 2019-10-29 13:41 * Note: client related operations ip address * / @Component public class IpAddress { @Autowired the HttpServletRequest request; / ** * acquisition client sends a request ip address * * @param request - the request header * @return - IP address * / public String getIpAddress(HttpServletRequest request) { String ip = null; //X-Forwarded-For:Squid 服务代理 String ipAddresses = request.getHeader("X-Forwarded-For"); //X-Real-IP:nginx服务代理 if (ipAddresses == null || ipAddresses.length() == 0 || "unknown".equalsIgnoreCase(ipAddresses)) { ipAddresses = request.getHeader("X-Real-IP"); } //Proxy-Client-IP:apache 服务代理 if (ipAddresses == null || ipAddresses.length() == 0 || "unknown".equalsIgnoreCase(ipAddresses)) { ipAddresses = request.getHeader("Proxy-Client-IP"); } //WL-Proxy-Client-IP:weblogic 服务代理 if (ipAddresses == null || ipAddresses.length() == 0 || "unknown".equalsIgnoreCase(ipAddresses)) { ipAddresses = request.getHeader("WL-Proxy-Client-IP"); } //HTTP_CLIENT_IP:有些代理服务器 if (ipAddresses == null || ipAddresses.length() == 0 || "unknown".equalsIgnoreCase(ipAddresses)) { ipAddresses = request.getHeader("HTTP_CLIENT_IP"); } //Some network through a multi-layer proxy, then get to the ip there will be more, are generally separated by a comma (,), and the first to be true ip IP client IF (IPAddresses! = Null && ipAddresses.length ! () = 0 ) { IP = ipAddresses.split ( ",") [0 ]; } // still not obtained, and finally by request.getRemoteAddr (); obtaining IF (IP == null || ip.length () == 0 || "Unknown" .equalsIgnoreCase (IPAddresses)) { IP = request.getRemoteAddr (); } return IP; } public String getIpAddress () { return getIpAddress (Request); } }
Solutions mentioned in the text of the original address: https://www.cnblogs.com/Mauno/p/Mauno.html