Search code examples
javatomcatservletswebsocketatmosphere

HttpServletRequest.getRemoteAddr() empty for Tomcat 8 Java 8


If I run Tomcat 7 with Java 6, the HttpServletRequest.getRemoteAddr() method returns the IP address as I would expect.

If I run Tomcat 8 with Java 8, the same method returns an empty string.

The X-Forwarded-For header is also null in both scenarios.

The messages are sent via websockets with Atmosphere.

Also, remoteHost() and remoteUser() are both empty as well.


Solution

  • JSR-356 Websocket specification does not expose the client IP which is why HttpServletRequest.getRemoteAddr() returns null.

    This also explains why HttpServletRequest.getRemoteAddr() returns an empty string or 0.0.0.0 depending on the version of Atmosphere you are using because Atmosphere alters the HttpServletRequest.

    Although the Tomcat 7 documentation on Websockets states "Tomcat implements the Java WebSocket 1.1 API defined by JSR-356", when I run Tomcat version 7.0.68, which is the current Tomcat 7 version, HttpServletRequest.getRemoteAddr() still returns the client IP address via websocket. This is true when I run Tomcat with Java 6, 7, or 8.

    The Tomcat 8 documentation on Websockets also states "Tomcat implements the Java WebSocket 1.1 API defined by JSR-356" and when running the same application on Tomcat 8 that I ran on 7, HttpServletRequest.getRemoteAddr() returns an empty string.