欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

nginx多次转发后获取客户请求IP

程序员文章站 2022-03-15 13:57:37
...
/**
	 * 
	 * @Description:获取客户端请求IP地址,解决使用nginx请求转发后不能获取问题
	 * @param @param request
	 * @param @return
	 * @return String
	 * @throws
	 */
	public String getIpAddr(HttpServletRequest request){
		String ip=request.getHeader("X-Forwarded-For");
    	String localIP = "127.0.0.1";
	    if((ip == null)||(ip.length() == 0)||(ip.equalsIgnoreCase(localIP))||"unknown".equalsIgnoreCase(ip)){
	        ip = request.getHeader("Proxy-Client-IP");
		}
	    if((ip == null)||(ip.length() == 0)||(ip.equalsIgnoreCase(localIP))||"unknown".equalsIgnoreCase(ip)){
	        ip = request.getHeader("WL-Proxy-Client-IP");
		}
	    if((ip == null)||(ip.length() == 0)||(ip.equalsIgnoreCase(localIP))||"unknown".equalsIgnoreCase(ip)){
	        ip = request.getRemoteAddr();
		}
	    if(ip!=null && ip.length()>15){
	         if(ip.indexOf(",")>0){
	             ip = ip.substring(0,ip.indexOf(","));
	         }
	     }
	    return ip;
	}