A question about java web apps and X-REAL-IP header

前端 未结 2 541
梦谈多话
梦谈多话 2021-01-25 14:35

I\'m setting up a demo of a project for a client. On my server I have a lot of sites built with different technologies that are running under different servers on different port

相关标签:
2条回答
  • 2021-01-25 15:12

    You can do this using Tomcat's RemoteIpValve

    <Valve className="org.apache.catalina.valves.RemoteIpValve"
               remoteIpHeader="X-REAL-IP"
               requestAttributesEnabled="true"
               internalProxies="127.0.0.1"  />
    

    That way, when you call request.getRemoteAddr(), it will be providing the right information. By the way, you probably want to use the more standard header, which is X-Forwarded-For.

    0 讨论(0)
  • 2021-01-25 15:37

    That depends on what you mean by "real request IP". If you're talking about the value returned from request.getRemoteAddr() then yes, it's possible.
    The way to do that would be to set up a servlet filter in your web application that would intercept all URLs (or only the ones for which you want X-REAL-IP returned) and have that filter wrap incoming request into a descendant of HttpServletRequestWrapper which will override getRemoteAddr() to return X-REAL-IP value.

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