Stacked Q&A

Jersey remote ip address not working correctly
  • 1 voting
  • 2022-05-24 00:00

    I am trying to retrieve the IP address which makes the call to a REST call.

    The code I have is:

    import javax.servlet.http.HttpServletRequest;
    import javax.ws.rs.*;
    import javax.ws.rs.core.Context;
    import javax.ws.rs.core.MediaType;
    
    import org.json.JSONObject;
    
    @Path("/api")
    public class Endpoints {
    
        @GET
        @Path("/whoami")
        @Produces(MediaType.APPLICATION_JSON)
        public String testHeaders(@Context final HttpServletRequest request){
    
            final JSONObject json = new JSONObject();
            json.put("remoteaddr", request.getRemoteAddr());
    
            return json.toString(4);
        }
    
    }
    

    the application is deployed on heroku here. When you make the call to the api it returns the ip is: 10.155.0.152, how ever when I google it it says my IP address is something different. Is there any way to return the actual IP?

  • 1 242 0 0
첨부 파일
답변
총 1개의 답변이있습니다.
  • 답변일: 2022-05-24 00:00

    Heroku sets a number of request headers that contain this kind of information. In your case, you'll want:

    request.getHeader("X-Forwarded-For");
    

    This will return the originating IP address of the client connecting to the Heroku router.

    The IP address you are getting from request.getRemoteAddr() is probably the Heroku router.

  • 0 0 채택된 답변 퍼블 채택!
Tages
    활동순위
    유저
      파트너사