【问题标题】:Active directory auth Tomcat - Read username in Action?Active Directory auth Tomcat - 在操作中读取用户名?
【发布时间】:2012-11-26 22:30:03
【问题描述】:

我已经在 Tomcat 中为我的 Struts 2 Web 应用程序实现了 Active Directory 身份验证。我希望能够在应用程序中使用登录用户的用户名。如何在动作中阅读它?

【问题讨论】:

  • 我得到它与 Servlet Context getRemoteUser() 一起使用...这和 getUserPrincipal 有什么区别?
  • 别担心我在这里发现了...stackoverflow.com/questions/8680843/…
  • 这样做会使测试变得更加困难。为什么不使用框架提供的抽象?

标签: java tomcat struts2 active-directory


【解决方案1】:

在 Struts2 中有 org.apache.struts2.interceptor.PrincipalAware 接口,它将org.apache.struts2.interceptor.PrincipalProxy 设置为您的操作。在您的操作中实现它并使用此对象来获取用户主体。

使用PrincipalAware 接口是获取用户主体的首选方式。来自 javadoc:

仅当 Action 在 servlet 环境中使用时,此接口才相关。 通过使用这个接口,您将不会被 servlet 环境所束缚。

【讨论】:

  • “不依赖于 servlet 环境”是什么意思?
  • @user1277546 正是这样;一旦您在代码中获得了 servlet 规范工件,它可能会变得更加难以测试。从依赖中抽象出来可以使测试更容易。
【解决方案2】:

它不与 Servlet 上下文集成吗?您应该可以通过以下方式获得它:

ServletActionContext.getRequest().getUserPrincipal().getName();

【讨论】:

    猜你喜欢
    • 2012-03-10
    • 2011-02-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-05-14
    • 1970-01-01
    相关资源
    最近更新 更多