如何使用Waffle获取客户端用户名?

时间:2016-10-27 17:25:41

标签: java jsp tomcat waffle

我正在Tomcat 7上运行Windows 7。所有客户端也在运行Windows 7

我正在尝试在test.jsp页面上打印客户端用户名,因此我使用Waffle。这是WEB-INF/web.xml

 <filter>
  <filter-name>SecurityFilter</filter-name>
  <filter-class>waffle.servlet.NegotiateSecurityFilter</filter-class>
  <init-param>
    <param-name>impersonate</param-name>
    <param-value>true</param-value>
  </init-param>
</filter> 

这是我的test.jsp

<% 
    String userId = Secur32Util.getUserNameEx(Secur32.EXTENDED_NAME_FORMAT.NameSamCompatible); 
    out.println(userId); 
%> 

但是它始终会打印server计算机用户名。我在很多client台计算机上试过它,它总是打印server但不打印client用户ID

为什么呢?怎么纠正这个?

2 个答案:

答案 0 :(得分:2)

首先,将waffle-api.jar放在项目类路径中。然后将此xml代码放在web.xml上。

<filter>
    <filter-name>SecurityFilter</filter-name>
    <filter-class>waffle.servlet.NegotiateSecurityFilter</filter-class>
    <init-param>
        <param-name>principalFormat</param-name>
        <param-value>fqn</param-value>
    </init-param>
    <init-param>
        <param-name>roleFormat</param-name>
        <param-value>both</param-value>
    </init-param>
    <init-param>
        <param-name>allowGuestLogin</param-name>
        <param-value>true</param-value>
    </init-param>
    <init-param>
        <param-name>securityFilterProviders</param-name>
        <param-value>
        waffle.servlet.spi.NegotiateSecurityFilterProvider
        waffle.servlet.spi.BasicSecurityFilterProvider
    </param-value>
    </init-param>
    <init-param>
        <param-name>waffle.servlet.spi.NegotiateSecurityFilterProvider/protocols</param-name>
        <param-value>
        Negotiate
        NTLM
    </param-value>
    </init-param>
    <init-param>
        <param-name>waffle.servlet.spi.BasicSecurityFilterProvider/realm</param-name>
        <param-value>WaffleFilterDemo</param-value>
    </init-param>
</filter>
<filter-mapping>
    <filter-name>SecurityFilter</filter-name>
    <url-pattern>/*</url-pattern>
</filter-mapping>

并使用以下代码获取客户的用户名;

HttpServletRequest request = (HttpServletRequest)Executions.getCurrent().getNativeRequest();
String user = request.getRemoteUser();

有关详细信息,请访问:

https://github.com/dblock/waffle

信用转到klepon

资源链接:

  1. http://forum.zkoss.org/question/96532/get-clients-username/
  2. https://stackoverflow.com/a/5891022/2293534

答案 1 :(得分:1)

神奇的是:$pageContext.request.remoteUser