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


    前言

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

    一、需求背景

         微服务架构使用了Nginx代理转发、并且使用了SpringCloud的Gateway统一控制所有请求,现在有个需求: 做一个日子记录切面,需要记录用户请求的ip地址
         在上述双重背景下,通过普通的方法获取用户ip地址是不可行的,只能获取到引用部署所在服务器的内网地址,必须要做一系列的设置以后才能正确获取到响应的地址。

    二、解决办法

    2.1 Nginx设置

      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;
      }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10

    2.2 gateway网关设置

    @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;
        }
    }
    
    
    • 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
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35

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

  • 相关阅读:
    【自然语言处理概述】百度百科数据爬取
    Elasticsearch 相关问题总结
    亚商投资顾问 早餐FM/1117我国5G应用开始规模复制
    c++ 模板 指针类型偏特化
    创建共享内存后,进程结束,共享内存是否会消失?
    Docker 安装 Oracle Database 23c
    【前端】JavaScript-PC端网页特效
    快速掌握 Base 64 | Java JS 密码系列
    玩转用户自定义函数UDF_大数据培训
    软考高级系统架构设计师系列案例考点专题六:面向服务架构设计
  • 原文地址:https://blog.csdn.net/qq_37844454/article/details/138182704