JSP如何获取client真实IP
发布网友
发布时间:2022-10-07 16:48
我来回答
共4个回答
热心网友
时间:2023-10-28 07:35
如果使用代理的话,用户的浏览器并没有和你的服务器进行连接,所以你是无法得知用户的真正地址的。
热心网友
时间:2023-10-28 07:36
获得客户端真实IP地址的参考方法一:
public String getRemortIP(HttpServletRequest request) {
if (request.getHeader("x-forwarded-for") == null) {
return request.getRemoteAddr();
}
return request.getHeader("x-forwarded-for");
}
获得客户端真实IP地址的参考方法二:
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;
}
热心网友
时间:2023-10-28 07:36
String logIp = request.getHeader("X-Forwarded-For");
if(logIp == null || logIp.length() == 0) {
logIp=request.getHeader("WL-Proxy-Client-IP");
}
if(logIp == null || logIp.length() == 0) {
logIp=request.getRemoteAddr();
}
out.print(logIp);
热心网友
时间:2023-10-28 07:37
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;
}