How to get the client username with Waffle?

2019-07-07 06:46发布

问题:

I'm running Tomcat 7 on Windows 7. All clients are running Windows 7 too .

I'm trying to print the client username on a test.jsp page so I use Waffle . Here is the 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> 

Here is my test.jsp

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

However it always prints the server computer username . I tried it on many client machines, and it always printed the server but not the client userid

Why? How to correct this?

回答1:

The magic is: $pageContext.request.remoteUser



回答2:

First, put waffle-api.jar on your project classpath. then put this xml code on your 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>

and use below code to get your clients username ;

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

for more detail you can visit :

https://github.com/dblock/waffle

Credit Goes to klepon

Resource Link:

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