在Web请求中获取请求用户的真正IP:
public static String getUserRealIP(HttpServletRequest request) throws UnknownHostException {
String ip = "";
// 有的user可能使用代理,为处理用户使用代理的情况,使用x-forwarded-for
if (request.getHeader("x-forwarded-for") == null) {
ip = request.getRemoteAddr();
} else {
ip = request.getHeader("x-forwarded-for");
}
if ("127.0.0.1".equals(ip)) {
// 获取本机真正的ip地址
ip = InetAddress.getLocalHost().getHostAddress();
}
return ip;
}
|
|