如何获取到用户访问的真实ip地址

我们一般使用request.getRemoteAddr),获取访问用户的ip地址。但是当使用代理后,使用该方法就获取不到真实ip了,而是获取一个相同的ip。

      这时我们可以在http头中获取:

public static String getRemoteAddrHttpServletRequest request) {String ip = request.getHeader"X-Forwarded-For");if ip == null || ip.length) == 0 || "unknown".equalsIgnoreCaseip)) {ip = request.getHeader"Proxy-Client-IP");}if ip == null || ip.length) == 0 || "unknown".equalsIgnoreCaseip)) {ip = request.getHeader"WL-Proxy-Client-IP");}if ip == null || ip.length) == 0 || "unknown".equalsIgnoreCaseip)) {ip = request.getHeader"HTTP_CLIENT_IP");}if ip == null || ip.length) == 0 || "unknown".equalsIgnoreCaseip)) {ip = request.getHeader"HTTP_X_FORWARDED_FOR");}if ip == null || ip.length) == 0 || "unknown".equalsIgnoreCaseip)) {ip = request.getRemoteAddr);}return ip;}

通过上面这个方法,可以获取到用户真实的ip。亲测有效,请大家批评指正

Published by

风君子

独自遨游何稽首 揭天掀地慰生平

发表回复

您的电子邮箱地址不会被公开。 必填项已用 * 标注