Java常用工具类之自定义访问对象

package com.wazn.learn.util;

import javax.servlet.http.HttpServletRequest;  

/** 
 * 自定义访问对象工具类 
 *  
 * 获取对象的IP地址等信息 
 * @author yangzhenyu
 * 
 */  
public class CusAccessObjectUtil {  
  
    /** 
     * 如:X-Forwarded-For:192.168.1.110, 192.168.1.120, 192.168.1.130, 
     * 192.168.1.100 
     *  
     * 用户真实IP为: 192.168.1.110 
     *  
     * @param request 
     * @return 
     */  
    public static String getIpAddress(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.getHeader("HTTP_CLIENT_IP");  
        }  
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {  
            ip = request.getHeader("HTTP_X_FORWARDED_FOR");  
        }  
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {  
            ip = request.getRemoteAddr();  
        }  
        return ip;  
    }  
      
}  

 

posted @ 2018-01-18 16:43  谁将新樽辞旧月,今月曾经照古人  阅读(242)  评论(0编辑  收藏  举报