创建会话时如何获取IP地址? [英] How to get the IP address when a session is created?

查看:20
本文介绍了创建会话时如何获取IP地址?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的 grails 应用程序中,我实现了接口 HttpSessionListener 来监听会话创建,如下所示:

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

class MyHttpSessionListener implements HttpSessionListener {

    public void sessionCreated(HttpSessionEvent event) {
        log.info "***************** Session created: id= ${event.getSession()?.id}"
    }
}

现在,我想记录负责创建会话的 IP 地址.

我该怎么做?

推荐答案

可以访问RequestContextHolder并获取值

you can access the RequestContextHolder and get the value

String ipAddr = ((ServletRequestAttributes)RequestContextHolder.currentRequestAttributes())
           .getRequest().getRemoteAddr();

这篇关于创建会话时如何获取IP地址?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆