Rest - how get IP address of caller

前端 未结 5 1187
离开以前
离开以前 2021-02-05 04:11

I am writing a Java Rest Web Service and need the caller\'s IP Address. I thought I saw this in the cookie once but now I don\'t see it. Is there a consistent place to get this

5条回答
  •  野的像风
    2021-02-05 04:56

    You could do something like this:

    @WebService
    public class YourService {
    
       @Resource
       WebServiceContext webServiceContext; 
    
       @WebMethod 
       public String myMethod() { 
    
          MessageContext messageContext = webServiceContext.getMessageContext();
          HttpServletRequest request = (HttpServletRequest) messageContext.get(MessageContext.SERVLET_REQUEST); 
          String callerIpAddress = request.getRemoteAddr();
    
          System.out.println("Caller IP = " + callerIpAddress); 
    
       }
    }
    

提交回复
热议问题