获取用户真实ip


ip


/**
	 * 获取真实ip
	 * @author chenp
	 * @param request
	 * @return
	 */
	public  String getReallyIp(HttpServletRequest request) {
           String ip = request.getHeader("X-Forwarded-For");
           if(!StringUtils.isEmpty(ip) && !"unKnown".equalsIgnoreCase(ip)){
               //多次反向代理后会有多个ip值,第一个ip才是真实ip
               int index = ip.indexOf(",");
               if(index != -1){
                   return ip.substring(0,index);
               }else{
                   return ip;
               }
           }
           ip = request.getHeader("X-Real-IP");
           if(!StringUtils.isEmpty(ip) && !"unKnown".equalsIgnoreCase(ip)){
               return ip;
           }
           return request.getRemoteAddr();
       }

posted @ 2019-01-04 20:47  大天狗子  阅读(143)  评论(0编辑  收藏  举报