public String getIpAddr(HttpServletRequest request) {
String ip = request.getHeader("x-forwarded-for");
if(ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
ip = request.getHeader("Proxy-Client-IP");
}
if(ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
ip = request.getHeader("WL-Proxy-Client-IP");
}
if(ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
ip = request.getRemoteAddr();
}
return ip;
}
java获取客户端ip
最新推荐文章于 2023-08-17 10:52:03 发布
本文介绍了一种通过Java代码从HttpServletRequest对象中获取客户端真实IP地址的方法。该方法首先尝试从请求头中读取x-forwarded-for字段,如果未找到则继续检查Proxy-Client-IP和WL-Proxy-Client-IP等字段,最后若所有尝试均失败,则使用request.getRemoteAddr()来获取IP地址。
713

被折叠的 条评论
为什么被折叠?



