问题概述
通过Nginx反向代理后,HttpServletRequest.getRemoteAddr()方法无法获取到用户的真实IP,只能获取到本机IP127.0.0.1
,这是因为传入Java后端的是Nginx本机的IP
解决办法
将用户的IP存储到Nginx的消息头,Java后端直接通过消息头获取用户真实的IP
解决过程
Nginx将真实IP存储到消息头
在Nginx配置文件的http{}中加入以下代码:
1 2 3 4 5 6 7 8 9
| set_real_ip_from 0.0.0.0/0; real_ip_header X-Forwarded-For; real_ip_recursive on;
proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
|
Java获取真实IP
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
| package com.ledao.util;
import javax.servlet.http.HttpServletRequest;
public class IpUtil {
public static String getIpAddr(HttpServletRequest request) { String fromSource = "X-Real-IP"; String ip = request.getHeader(fromSource); if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) { 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.getRemoteAddr(); } return ip; } }
|