x-forwarded-for header 未通过 AWS Application Load Balancer

x-forwarded-for header not passed by AWS Application Load Balancer

我的 tomcat 应用程序未从 AWS 应用程序负载均衡器接收 X-Forwarded-For header。负载平衡器配置为卸载 SSL 并通过 HTTP 与 tomcat 应用程序连接。我正在接收其他 header,例如 x-forwarded-proto、x-forwarded-port、x-amzn-trace-id。我正在尝试查找客户端 IP 地址,但现在坚持使用它。我还检查了我收到的所有 headers 以找出客户端 ip 地址,但它不在那里。谁能帮帮我?

这可能与 tomcat 配置有关。检查您是否在 tomcat 中获得了以下配置来处理 X-Forwarded-ForX-Forwarded-proto headers

<filter>
    <filter-name>RemoteIpFilter</filter-name>
    <filter-class>org.apache.catalina.filters.RemoteIpFilter</filter-class>
    <init-param>
      <param-name>protocolHeader</param-name>
      <param-value>x-forwarded-proto</param-value>
    </init-param>
  </filter>

  <filter-mapping>
    <filter-name>RemoteIpFilter</filter-name>
    <url-pattern>/*</url-pattern>
    <dispatcher>REQUEST</dispatcher>
  </filter-mapping>

配置参考如下:https://tomcat.apache.org/tomcat-7.0-doc/config/filter.html#Basic_configuration_to_handle_'x-forwarded-for''x-forwarded-proto'

您可以在 $CATALINA_BASE/conf/web.xml 或应用程序的 WEB-INF/web 中找到配置文件。xml

Tomcat provides a number of Filters which may be configured for use with all web applications using $CATALINA_BASE/conf/web.xml or may be configured for individual web applications by configuring them in the application's WEB-INF/web.xml.