【问题标题】:Retrieving IP Address from DAO layer从 DAO 层检索 IP 地址
【发布时间】: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


【解决方案1】:

移动你的

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

编码到clientshared包的outside类中。查看您的应用程序 *.gwt.xml 描述符。例如,如果您将 com/domain/App.gwt.xml 作为 GWT 描述符,并且您的源包被声明为

<source path="client" />
<source path="shared" />

那么您必须在 com.domain.clientcom.domain.shared 包之外编写代码。

考虑对您的代码进行一些重构。如果您的 DAO 层绝对需要了解 IP,那么最好将其作为参数发送给某个方法调用。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-09-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-03-04
    • 2010-12-05
    相关资源
    最近更新 更多