獲取客戶端使用的ip
阿新 • • 發佈:2017-09-14
blog pre 方法 class equal client pan lsi servlet
一般使用jsp的時候大多數時間都可以使用request.getRemoteAddr() 來獲取ip,但是這個前提是未經過反向代理等操作的原始地址,所以,需要在反向代理等操作之後還要獲取客戶端的ip變得困難重重,這裏給大家個方法,僅供參考
1 //獲取客戶端真實註冊地址 2 public String getRemoteHost(HttpServletRequest request){ 3 String ip = request.getHeader("x-forwarded-for"); 4 if(ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)){5 ip = request.getHeader("Proxy-Client-IP"); 6 } 7 if(ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)){ 8 ip = request.getHeader("WL-Proxy-Client-IP"); 9 } 10 if(ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)){11 ip = request.getRemoteAddr(); 12 } 13 return ip.equals("0:0:0:0:0:0:0:1")?"127.0.0.1":ip; 14 }
獲取客戶端使用的ip