gpt4 book ai didi

java - 创建 session 时如何获取IP地址?

转载 作者:搜寻专家 更新时间:2023-10-31 08:21:25 26 4
gpt4 key购买 nike

在我的 grails 应用程序中,我实现了接口(interface) HttpSessionListener 以监听 session 创建,如下所示:

class MyHttpSessionListener implements HttpSessionListener {

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

现在,我想记录负责创建 session 的 IP 地址

我该怎么做?

最佳答案

您可以访问 RequestContextHolder 并获取值

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

关于java - 创建 session 时如何获取IP地址?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3597083/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com