Handling X-FORWARDED-PROTO header in Java web application

前端 未结 2 392
感情败类
感情败类 2021-02-01 23:51

Can any one guide me in working with X-FORWARDED-PROTO header in Java web application deployed to Apache Tomcat.

The application setup is in such a way that tomcat talks

相关标签:
2条回答
  • 2021-02-02 00:38

    I am pretty sure you have it all figured out by now but I will add the answer nonetheless.

    You can use the class org.apache.catalina.valves.RemoteIpValve in the engine tag in conf/server.xml of tomcat.

        <Valve className="org.apache.catalina.valves.RemoteIpValve"
               internalProxies="192.168.1.XXX"
               remoteIpHeader="x-forwarded-for"
               remoteIpProxiesHeader="x-forwarded-by"
               protocolHeader="x-forwarded-proto"
        />
    

    Something to note that is very important is to set the internalProxies value. If this is not set and you are you using a non-standard network setup it could cause some issues where tomcat will not check for x-forwarded headers and it will default to "http". For security reasons I'd recommend to set it even if it works with the defaults.

    Look here for more information.

    0 讨论(0)
  • 2021-02-02 00:40

    Add this to your apache vhost managing connections

    <VirtualHost *:80>
      ...
      RewriteEngine On
      RewriteCond %{HTTP:X-Forwarded-Proto} !https
      RewriteRule !/status https://%{SERVER_NAME}%{REQUEST_URI} [L,R]
    </VirtualHost>
    

    this assumes your health check is /status, which doesn’t require https

    0 讨论(0)
提交回复
热议问题