【发布时间】:2013-01-31 11:34:21
【问题描述】:
我使用 spring-acegi,我想从 DAO 层检索 IP 地址。当我使用类似的东西时:
Authentication auth = SecurityContextHolder.getContext().getAuthentication();
if (auth != null) {
WebAuthenticationDetails details1 = ((WebAuthenticationDetails)auth.getDetails());
if (details1 != null) {
userIp = details1.getRemoteAddress();
}
}
在身份验证的那一刻,auth 为空,我无法检索 IP 地址。
我也尝试通过
获取IP地址String remoteAddress = ((ServletRequestAttributes)RequestContextHolder.currentRequestAttributes())
.getRequest().getRemoteAddr();
但出现编译错误(我们使用 GWT)。
谁能帮帮我?谢谢。
【问题讨论】:
-
你想做什么?你说的 DAO 层是什么意思?这是在身份验证期间吗?
-
为什么不再创建一个过滤器(或使用现有的),并从 HttpRequest 获取 ip 并将其存储在静态 ThreadLocal 变量中以在您的 DAO 中使用?
-
抛出了什么异常?
标签: java spring spring-security dao