从http协议中获得客户端ip

全文链接

import javax.servlet.http.HttpServletRequest;

import org.apache.log4j.Logger;

public final class InternetProtocol {
private static Logger log = Logger.getLogger(InternetProtocol.class);
/**
* 构造函数.
*/
private InternetProtocol() {
}

/**
 * 获取客户端IP地址.<br>
 * 支持多级反向代理
 * 
 * @param request
 *            HttpServletRequest
 * @return 客户端真实IP地址
 */
public static String getRemoteAddr(final HttpServletRequest request) {
    try{
        String remoteAddr = 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 (String element : array) {
                if (isEffective(element)) {
                    remoteAddr = element;
                    break;
                }
            }
        }
        if (!isEffective(remoteAddr)) {
            remoteAddr = request.getHeader("X-Real-IP");
        }
        if (!isEffective(remoteAddr)) {
            remoteAddr = request.getRemoteAddr();
        }
        return remoteAddr;
    }catch(Exception e){
        log.error("get romote ip error,error message:"+e.getMessage());
        return "";
    }
}

没有更多推荐了,返回首页