返回顶部

收藏

java获得真实IP代码

更多

在有代理服务器的情况下,需要通过X-FORWARDED-FOR http头来获得真实的用户ip:

public final class HTTPUtils {

    private static final String HEADER_X_FORWARDED_FOR =
        "X-FORWARDED-FOR";

    public static String remoteAddr(HttpServletRequest request) {
        String remoteAddr = request.getRemoteAddr();
        String x;
        if ((x = request.getHeader(HEADER_X_FORWARDED_FOR)) != null) {
            remoteAddr = x;
            int idx = remoteAddr.indexOf(',');
            if (idx > -1) {
                remoteAddr = remoteAddr.substring(0, idx);
            }
        }
        return remoteAddr;
    }

}

标签:java,ip

收藏

0人收藏

支持

1

反对

0

发表评论