SpringCloud使用Nginx代理、Gateway网关以后如何获取用户的真实ip

前言

本文转载自: www.microblog.store,且已获得授权.

一、需求背景

微服务架构使用了Nginx代理转发、并且使用了SpringCloud的Gateway统一控制所有请求,现在有个需求: 做一个日子记录切面,需要记录用户请求的ip地址

在上述双重背景下,通过普通的方法获取用户ip地址是不可行的,只能获取到引用部署所在服务器的内网地址,必须要做一系列的设置以后才能正确获取到响应的地址。

二、解决办法

2.1 Nginx设置

css 复制代码
  location / { 
      root /opt/xxx/xxx-auth; 
      index  index.html index.htm; 
      try_files $uri $uri/ /index.html; 
      
     # 获取用户请求ip
      proxy_set_header Host $host;
      proxy_set_header X-Real-IP $remote_addr;  #获取客户端真实IP
      proxy_set_header REMOTE-HOST $remote_addr;
  }

2.2 gateway网关设置

java 复制代码
@Component
@Log4j2
public class AuthenticationFilter implements GlobalFilter, Ordered {
    /**
     * 验证请求头是否带有Authentication
     */
    @Override
    public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {
        ServerHttpRequest request = exchange.getRequest();
        ServerHttpResponse response = exchange.getResponse();
        String path = request.getPath().pathWithinApplication().value();

        Optional.of(exchange.getRequest()).ifPresent(item -> {
            // 获取客户端IP地址
            List<String> xForwardedFor = item.getHeaders().get("x-forwarded-for");
            List<String> xRealIp = item.getHeaders().get("x-real-ip");
            List<String> remoteHost = item.getHeaders().get("remote-host");
            response.getHeaders().add("X-Forwarded-For", (xForwardedFor == null || xForwardedFor.isEmpty()) ? "" :
                    xForwardedFor.get(0));
            response.getHeaders().add("X-Real-IP", (xRealIp == null || xRealIp.isEmpty()) ? "" : xRealIp.get(0));
            response.getHeaders().add("Remote-Host", (remoteHost == null || remoteHost.isEmpty()) ? "" :
                    remoteHost.get(0));
        });

        //身份认证等等....
        
        return chain.filter(exchange);
    }

    @Override
    public int getOrder() {
        return 0;
    }
}

通过上述设置以后,我们就能正常在request请求头中获取相关信息了。

相关推荐
tzhou6445215 小时前
Docker 部署 Nginx HTTPS 服务(CentOS 7)
nginx·docker·https
等什么君!16 小时前
nginx启动失败 ,报404和 idea端口号被占用的解决办法
运维·nginx
全栈工程师修炼指南17 小时前
Nginx | HTTP 反向代理:当缓存失效时如何减轻后端(上游)服务压力?
运维·网络协议·nginx·http·缓存
骚戴18 小时前
AI架构指南:大型语言模型 (LLM) API 的通用集成与企业级配置(2025年)
人工智能·大模型·llm·gateway·api
yangmf204018 小时前
INFINI Gateway 助力联想集团 ES 迁移升级
大数据·数据库·elasticsearch·搜索引擎·gateway·全文检索
zhengxianyi5151 天前
vue 首屏加载优化
前端·javascript·vue.js·nginx·gzip·expires·静态文件缓存
全栈工程师修炼指南1 天前
Nginx | HTTP 反向代理:对上游服务端响应缓存流程浅析与配置实践
运维·网络协议·nginx·http·缓存
Justin_JGT2 天前
flask+uwsgi+Nginx
python·nginx·flask
淡笑沐白2 天前
Nginx 详细教程
运维·nginx
ts9772 天前
Nginx + Vue History 模式刷新 404 的完整排查与解决方案
运维·vue.js·nginx