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");
相关推荐
lifejump几秒前
NAT应用实验
运维·服务器
Hy行者勇哥6 分钟前
Linux 系统搭建桌面级云端办公 APP(从快捷方式到自定义应用)
linux·运维·服务器
python百炼成钢20 分钟前
52.Linux PWM子系统
linux·运维·服务器·驱动开发
CheungChunChiu22 分钟前
Linux 总线模型与 bind/unbind 完整解析
linux·ubuntu·sys·bind/unbind
可可苏饼干27 分钟前
ELK(Elastic Stack)日志采集与分析
linux·运维·笔记·elk
大柏怎么被偷了39 分钟前
【Git】基本操作
linux·运维·git
小女孩真可爱39 分钟前
大模型学习记录(八)---------RAG评估
linux·人工智能·python
我在人间贩卖青春1 小时前
查看文件相关命令
linux·查看文件
飞行增长手记1 小时前
IP协议从跨境到物联网的场景化应用
服务器·前端·网络·安全
GDAL1 小时前
客户端与服务器SSL协议/加密套件不兼容:完整解决方案
服务器·ssl