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()
}
out.print(ip)
%>
/** *构造函数. */ privateInternetProtocol(){ } /** *获取客户端IP地址.<br> *支持多级反向代理 * *@paramrequest *HttpServletRequest *@return客户端真实IP地址 */ publicstaticStringgetRemoteAddr(finalHttpServletRequestrequest){ try{ StringremoteAddr=request.getHeader("X-Forwarded-For") //如果通过多级反向代理,X-Forwarded-For的值不止一个,而是一串用逗号分隔的IP值,此时取X-Forwarded-For中第一个非unknown的有效IP字符串 if(isEffective(remoteAddr)&&(remoteAddr.indexOf(",")>-1)){ String[]array=remoteAddr.split(",") for(Stringelement:array){ if(isEffective(element)){ remoteAddr=element break } } } if(!isEffective(remoteAddr)){ remoteAddr=request.getHeader("X-Real-IP") } if(!isEffective(remoteAddr)){ remoteAddr=request.getRemoteAddr() } returnremoteAddr }catch(Exceptione){ log.error("getromoteiperror,errormessage:"+e.getMessage()) return"" } } /** *获取客户端源端口 *@paramrequest *@return */ publicstaticLonggetRemotePort(finalHttpServletRequestrequest){ try{ Stringport=request.getHeader("remote-port") if(StringUtil.isNotEmptyString(port)){ try{ returnLong.parseLong(port) }catch(NumberFormatExceptionex){ log.error("convertporttolongerror,port: "+port) return0l } }else{ return0l } }catch(Exceptione){ log.error("getromoteporterror,errormessage:"+e.getMessage()) return0l } }欢迎分享,转载请注明来源:夏雨云
评论列表(0条)