天天看點

java中兩種方法擷取真實IP

方法一:

//擷取真實	IP
	public final static String getIpAddress(HttpServletRequest request) throws IOException {  
        // 擷取請求主機IP位址,如果通過代理進來,則透過防火牆擷取真實IP位址  
  
        String ip = request.getHeader("X-Forwarded-For");  
  
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {  
            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();  
                if(ip.equals("127.0.0.1") || ip.equals("0:0:0:0:0:0:0:1")){  
                    //根據網卡取本機配置的IP  
                    InetAddress inet=null;  
                    try {  
                        inet = InetAddress.getLocalHost();  
                    } catch (UnknownHostException e) {  
                        e.printStackTrace();  
                    }  
                    ip= inet.getHostAddress();  
                }  
            }  
        } else if (ip.length() > 15) {  
            String[] ips = ip.split(",");  
            for (int index = 0; index < ips.length; index++) {  
                String strIp = (String) ips[index];  
                if (!("unknown".equalsIgnoreCase(strIp))) {  
                    ip = strIp;  
                    break;  
                }  
            }  
        }  
        return ip;  
    } 
           

方法2:

/**
     * <p>
     * Web 伺服器反向代理中用于存放用戶端原始 IP 位址的 Http header 名字,
     * 若新增其他的需要增加或者修改其中的值。
     * </p>
     */
    private static final String[] PROXY_REMOTE_IP_ADDRESS = { "X-Forwarded-For", "X-Real-IP" };       
 
       /**
	 * <p>
	 * 擷取請求的用戶端的 IP 位址。若應用伺服器前端配有反向代理的 Web 伺服器,
	 * 需要在 Web 伺服器中将用戶端原始請求的 IP 位址加入到 HTTP header 中。
	 * 詳見 {@link #PROXY_REMOTE_IP_ADDRESS}
	 * </p>
	 */
	public static String getRemoteIp( HttpServletRequest request ) {
	    for ( int i = 0 ; i < PROXY_REMOTE_IP_ADDRESS.length ; i++ ) {
	        String ip = request.getHeader( PROXY_REMOTE_IP_ADDRESS[i] );
	        if ( ip != null && ip.trim().length() > 0 ) {
	            return getRemoteIpFromForward( ip.trim() );
	        }
	    }
	    return request.getRemoteHost();
	}
	 
	/**
	 * <p>
	 * 從 HTTP Header 中截取用戶端連接配接 IP 位址。如果經過多次反向代理,
	 * 在請求頭中獲得的是以“,<SP>”分隔 IP 位址鍊,第一段為用戶端 IP 位址。
	 * </p>
	 *
	 * @param xforwardIp 從 HTTP 請求頭中擷取轉發過來的 IP 位址鍊
	 * @return 用戶端源 IP 位址
	 */
	private static String getRemoteIpFromForward( String xforwardIp ) {
	    int commaOffset = xforwardIp.indexOf( ',' );
	    if ( commaOffset < 0 ) {
	        return xforwardIp;
	    }
	    return xforwardIp.substring( 0 , commaOffset );
	}