java获取客户端ip地址
/**
* 获取客户端IP
*
* @param request 请求对象
* @return IP地址
*/
public static String getIpAddr(HttpServletRequest request) {
if (request == null) {
return "unknown";
}
String ip = request.getHeader("x-forwarded-for");
if (ipIsNull(ip)) {
ip = request.getHeader("Proxy-Client-IP");
}
if (ipIsNull(ip)) {
ip = request.getHeader("X-Forwarded-For");
}
if (ipIsNull(ip)) {
ip = request.getHeader("WL-Proxy-Client-IP");
}
if (ipIsNull(ip)) {
ip = request.getHeader("X-Real-IP");
}
if (ipIsNull(ip)) {
ip = request.getRemoteAddr();
}
return ip;
}
public static boolean ipIsNull(String ip) {
boolean flag = (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) ? true : false;
return flag;
}
赠人玫瑰,手有余香~
如果以上对您有帮助,请关注以下微信公众号和小程序,感谢支持!