Web应用程序中的NTLM身份验证(java)

我正在使用以下过滤器在我的Web应用程序中启用NTLM身份验证。

我得到Windows浏览器身份验证提示。运行正常。除了以下事实外- * 两种情况均无错误。

*在每种情况下都将打印用户名(正确或相反),工作站等。

package com.test;

import java.io.IOException;

import java.io.PrintStream;

import javax.servlet.Filter;

import javax.servlet.FilterChain;

import javax.servlet.FilterConfig;

import javax.servlet.ServletException;

import javax.servlet.ServletRequest;

import javax.servlet.ServletResponse;

import javax.servlet.http.HttpServletRequest;

import javax.servlet.http.HttpServletResponse;

import jcifs.ntlmssp.Type3Message;

import com.sun.xml.internal.ws.util.StringUtils;

import sun.misc.BASE64Decoder;

import sun.misc.BASE64Encoder;

public class NTLMUserFilter implements Filter {

private FilterConfig filterConfig = null;

private String userDomain = null;

public void init(FilterConfig filterConfig) throws ServletException {

this.filterConfig = filterConfig;

}

public void doFilter(ServletRequest req, ServletResponse res,

FilterChain chain) {

HttpServletRequest request = (HttpServletRequest) req;

HttpServletResponse response = (HttpServletResponse) res;

String username = null;

//first, get the user agent

String useragent = request.getHeader("user-agent");

//if you're using IE, you can continue

if ((useragent.indexOf("MSIE") > -1)){

//Always do the ntlm check (for IE POST back)

try{

String auth = request.getHeader("Authorization");

if (auth == null)

{

response.setHeader("WWW-Authenticate", "NTLM");

response.setStatus(response.SC_UNAUTHORIZED);

response.setContentLength(0) ;

response.flushBuffer();

return;

}

if (auth.startsWith("NTLM "))

{

byte[] msg = new sun.misc.BASE64Decoder().decodeBuffer(auth.substring(5));

int off = 0, length, offset;

if (msg[8] == 1)

{

byte z = 0;

byte[] msg1 = {(byte)'N', (byte)'T', (byte)'L', (byte)'M', (byte)'S', (byte)'S', (byte)'P', z,(byte)2, z, z, z, z, z, z, z,(byte)40, z, z, z, (byte)1, (byte)130, z, z,z, (byte)2, (byte)2, (byte)2, z, z, z, z, z, z, z, z, z, z, z, z};

response.setHeader("WWW-Authenticate", "NTLM " + new sun.misc.BASE64Encoder().encodeBuffer(msg1));

response.setStatus(response.SC_UNAUTHORIZED);

response.setContentLength(0) ;

response.flushBuffer();

return;

}

else if (msg[8] == 3)

{

//Did Authentication Succeed? All this is always printed.

Type3Message type3 = new Type3Message(msg);

System.out.println("osUser: " + type3.getUser());

System.out.println("osRemoteHost: + " + type3.getWorkstation());

System.out.println("osDomain: " + type3.getDomain());

}

}

}catch(Exception e){

System.out.println(e) ;

}

//System.out.println("Suc);

}

try {

chain.doFilter(req, res);

} catch (IOException e) {

System.out.println(e);

} catch (ServletException e) {

System.out.println(e);

}

}

public void destroy()

{

this.filterConfig = null;

}

}


web.xml很简单:

<filter>

<filter-name>ntlmFilter</filter-name>

<filter-class>

com.test.NTLMUserFilter

</filter-class>

</filter>

<!-- Filter mapping configuration -->

<filter-mapping>

<filter-name>ntlmFilter</filter-name>

<url-pattern>/*</url-pattern>

</filter-mapping>

回答:

您收到的是Type 3消息,但是除了打印出详细信息之外,您什么都没做。此时,您需要验证客户的响应,并发送200(如果已授权)或401(如果未授权)。

但是,您传递的Type

1消息是由静态字节组成的,尽管它会诱使客户端发送回响应,但大多数情况下是毫无意义的。自己实现一个完整的NTLM身份验证堆栈不是不可能的,但是您拥有的代码将根本无法工作。

您可以研究Java的NTLM解决方案,或者(假设您使用的是Windows)可以调用必要的身份验证功能,例如AcceptSecurityContextJNI。

以上是 Web应用程序中的NTLM身份验证(java) 的全部内容, 来源链接: utcz.com/qa/426285.html

回到顶部