Link to home
Start Free TrialLog in
Avatar of dbasplus
dbasplusFlag for Australia

asked on

Change tomcat filter from NTLMv1 to NTLMv2 or Kerberos

We have an old Application running on Tomcat 5.5 that uses a NTLMv1 filter to verify the users.
We need to upgrade the JAVA filter to use NTLMv2 or Kerberos.

The NTLM code from the filter is included below:

//NLTM Module here
                  String auth = request.getHeader("Authorization");
                  
                  if (auth == null)
                  {
                    response.setStatus(HttpServletResponse.SC_UNAUTHORIZED);
                    response.setHeader("WWW-Authenticate", "NTLM");
                    //response.flushBuffer();
                    LogManager.debug("First Authentication check failed",AuthenticationFilter.class);
                    return;
                  }
                  if (auth.startsWith("NTLM"))
                  {
                    byte[] msg = new sun.misc.BASE64Decoder().decodeBuffer(auth.substring(5));
                    LogManager.debug("NTLM response="+new String(msg, Charset.forName("ISO-8859-1")),AuthenticationFilter.class);
                    int off = 0, length, offset;
                    if (msg[8] == 1)
                    {
                        LogManager.debug("Second Authentication check failed",AuthenticationFilter.class);
                      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.setStatus(HttpServletResponse.SC_UNAUTHORIZED);
                      response.setHeader("WWW-Authenticate", "NTLM " + new sun.misc.BASE64Encoder().encodeBuffer(msg1).trim());
                      //response.flushBuffer();
                      //response.sendError(response.SC_UNAUTHORIZED);
                      LogManager.debug("Sending unauthorized and flushing buffer",AuthenticationFilter.class);
                      return;
                    }
                    else if (msg[8] == 3)
                    {
                        LogManager.debug("Third Authentication check passed",AuthenticationFilter.class);
                      off = 30;
            
                      //get details
                      length = msg[off+17]*256 + msg[off+16];
                      offset = msg[off+19]*256 + msg[off+18];
                      remoteHost = cleanString(new String(msg, offset, length));
            
                      length = msg[off+1]*256 + msg[off];
                      offset = msg[off+3]*256 + msg[off+2];
                      domain = cleanString(new String(msg, offset, length));
            
                      length = msg[off+9]*256 + msg[off+8];
                      offset = msg[off+11]*256 + msg[off+10];
                      username = cleanString(new String(msg, offset, length));
                        
                        LogManager.info(new StringBuilder("User [").append(username).append("] NTLM Authenticated...domain[").append(domain).append("] RemoteHost: [").append(remoteHost).append("]").toString(),AuthenticationFilter.class);
                    
                  //Userid check here
ASKER CERTIFIED SOLUTION
Avatar of gheist
gheist
Flag of Belgium image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of btan
btan

will suggest go for Kerberos (need AD or LDAP directory store) if possible as compared to NTLM as the latter is weaker for v2 using HMAC-MD5. May want to check out  http://spnego.sourceforge.net/spnego_tomcat.html tutorial to try to config Tomcat to use spnego. Some run through stated http://webmoli.com/2009/08/29/single-sign-on-in-java-platform/
Avatar of dbasplus

ASKER

Go with Waffle.
Add the jar files to the lib directory of the webapp.
Update the web.xml for the webapp (use the example from the Waffle website).
Thats it.
We had to remove the previous bit of code (above) that did the authentication as Waffle handles this amazingly well.
To just use NTLM (NTMLv1 or NTMLv2 what ever is available) change the protocol in the setup.
We tried countless other solutions and this was just too simple not to use. We had an old versions of Tomcat and Java that were also handles without updating. Thank you to all the people who contributed to these libraries.