How to get the IP address when a session is created?

前端 未结 3 641
别跟我提以往
别跟我提以往 2021-01-13 08:05

In my grails application, I have implemented the interface HttpSessionListener to listen for session creation as given below:

c         


        
相关标签:
3条回答
  • 2021-01-13 08:37

    As far as I know you can't using the HttpSessionListener interface.

    You can get and log the IP Address from "ServletRequest.getRemoteAddr()" but you don't have access to the servlet request from HttpSessionListener or from HttpSessionEvent.

    Best idea would to have a javax.servlet.Filter which gets the IP address and sets it as a session attribute if not already present. (You could also do the logging if not already present).

    0 讨论(0)
  • 2021-01-13 08:42

    you can access the RequestContextHolder and get the value

    String ipAddr = ((ServletRequestAttributes)RequestContextHolder.currentRequestAttributes())
               .getRequest().getRemoteAddr();
    
    0 讨论(0)
  • 2021-01-13 08:52

    You can also use this interface in your HttpSessionListener : ServletRequestListener You can implement : requestInitialized() like this.

    @Override
    public void requestInitialized(ServletRequestEvent servletRequestEvent) {
        this.request = (HttpServletRequest) servletRequestEvent.getServletRequest();
    }
    

    it s working fine, the request object can bring you the remote adress, there is a méthod to do that

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