java web项目获取nginx反向代理后的客户端的真实ip

weblog 2061 0 0
在ngxin代理的配置文件的location块中添加如下配置
            proxy_set_header Host $host;
	    proxy_set_header X-Real-IP $remote_addr;
	    proxy_set_header REMOTE-HOST $remote_addr;
	    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;

整体如下

 location / {
	    proxy_pass http://127.0.0.1:8080;
        proxy_set_header Host $host;
	    proxy_set_header X-Real-IP $remote_addr;
	    proxy_set_header REMOTE-HOST $remote_addr;
	    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        root   html;
        index  index.html index.htm;
}

java获取真实地址的方法如下

    private static final String UNKNOWN = "unknown";
    private static final String LOCALHOST = "127.0.0.1";
    private static final String SEPARATOR = ",";

    public String getIpAddr(HttpServletRequest request) {
        String ipAddress;
        try {
            ipAddress = request.getHeader("x-forwarded-for");
            if (ipAddress == null || ipAddress.length() == 0 || UNKNOWN.equalsIgnoreCase(ipAddress)) {
                ipAddress = request.getHeader("Proxy-Client-IP");
            }
            if (ipAddress == null || ipAddress.length() == 0 || UNKNOWN.equalsIgnoreCase(ipAddress)) {
                ipAddress = request.getHeader("WL-Proxy-Client-IP");
            }
            if (ipAddress == null || ipAddress.length() == 0 || UNKNOWN.equalsIgnoreCase(ipAddress)) {
                ipAddress = request.getRemoteAddr();
                if (LOCALHOST.equals(ipAddress)) {
                    InetAddress inet = null;
                    try {
                        inet = InetAddress.getLocalHost();
                    } catch (UnknownHostException e) {
                        e.printStackTrace();
                    }
                    ipAddress = inet.getHostAddress();
                }
            }
            // 对于通过多个代理的情况,第一个IP为客户端真实IP,多个IP按照','分割
            // "***.***.***.***".length()
            if (ipAddress != null && ipAddress.length() > 15) {
                if (ipAddress.indexOf(SEPARATOR) > 0) {
                    ipAddress = ipAddress.substring(0, ipAddress.indexOf(","));
                }
            }
        } catch (Exception e) {
            ipAddress = "";
        }
        return ipAddress;
    }

 


猜你喜欢
工具 3112 计算机ip地址方法1.过滤器packagecn.com.dzqc.interceptor;importjava.io.IOException
算法基础 1493 区别总体来说正区别在于对象不一样,正对象是对象是服务。正一服务一服务以租
工具 2685 1.修改接口配置信息url填外网可以访问接口(必须是80口),如果是本地测试话可以用外网穿透。例:http://aaaa.com/call/backToken可以任意填写,但要和接口中配置一致例
mqtt协议 972 /artifactIdversion1.2.0/version/dependency发布importorg.eclipse.paho.client.mqttv3.MqttClient;importorg.eclipse.pah
java基础 3107 java静态示例packageclub.jiajiajia.test.staticproxy;publicinterfaceBuyHouse{voidbuyHosue();//买房子
算法基础 2138 如果一个类现了一个接口,如何接口中泛型际类型importjava.lang.reflect.ParameterizedType; importjava.lang.reflect.Type
前端,java基础 1881 一、pom依赖二、接口示例三、前页面  有些场景需要生成带参数小程序二维码,比如商城为每个商品生成小程序二维码,通过微信扫码直接跳转到该商品所在展示页面。微信为开发者们提供了相应接口
weblog 2235 java服务现sha256加密importjava.io.UnsupportedEncodingException;importjava.security.MessageDigest
目录
没有一个冬天不可逾越,没有一个春天不会来临。最慢的步伐不是跬步,而是徘徊,最快的脚步不是冲刺,而是坚持。