Nginx 防止IP伪造,绕过IP限制

背景介绍

在使用Nginx时,需要将IP地址转发到后置应用中,往往需要增加配置

proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;

在后端程序通过读取请求头里的X-Forwarded-For来获取用户客户端IP。

java 复制代码
public String getRemortIP(HttpServletRequest request) { 
  if (request.getHeader("x-forwarded-for") == null) { 
    return request.getRemoteAddr(); 
  } 
  return request.getHeader("x-forwarded-for"); 
}

但是,这个X-Forwarded-For可以通过修改报文来伪造IP。

解决办法

Nginx中增加配置

proxy_set_header X-Real-IP $remote_addr; #让WEB取得原始请求IP

代码中取客户端IP

java 复制代码
String clientIp = request.getHeader("X-Real-IP");
相关推荐
ST小智20 分钟前
2025年创作历程回顾与个人生活平衡
大数据·linux·人工智能
捧 花31 分钟前
前端如何调用后端接口(HTML + JS & Vue )
服务器·golang·vue·api·前后端交互
你好helloworld35 分钟前
ubuntu安装protobuf
linux·运维·ubuntu
chenyuhao20241 小时前
Linux网络编程:UDP和TCP套接字编程
linux·网络·udp·tcp
一只努力学习的Cat.1 小时前
Linux:五种IO模型
linux·服务器
jqrbcts1 小时前
关于发那科机器人如何时时把角度发给PLC
java·服务器·网络·人工智能
Atri厨1 小时前
awk入门练习题
linux·运维·服务器
胖好白1 小时前
【RK3588开发】RK3588的Debian系统环境搭建
linux·vscode·debian
Alex Cafu1 小时前
Linux网络编程1(OSI模型与TCP/IP协议栈)
linux·c语言·网络·tcp/ip
zhglhy1 小时前
ckman创建集群报错 数据校验失败: invaild ip range
服务器·网络·tcp/ip