Here is the exact code to do it my friend.
Redirect or whatever you want, after the System.out.println
-Jesse

=========================================
import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
import java.util.*;

public class AuthenticationServlet extends HttpServlet {
  // Step 2: Challenge message
  final private static byte[] CHALLENGE_MESSAGE =
  {(byte)'N', (byte)'T', (byte)'L', (byte)'M', (byte)'S', (byte)'S', (byte)'P', 0,
  2, 0, 0, 0, 0, 0, 0, 0,
  40, 0, 0, 0, 1, (byte)130, 0, 0,
  0, 2, 2, 2, 0, 0, 0, 0, // nonce
  0, 0, 0, 0, 0, 0, 0, 0};

  private String user;

  /**
  * Obtain the network ID from the HTTP request
  */
  public void doPost(HttpServletRequest req, HttpServletResponse res) throws 
IOException, ServletException {
    try {
      String auth = req.getHeader("Authorization");

      if (auth == null)
      {
        res.setContentLength(0);
        res.setStatus(res.SC_UNAUTHORIZED);
        res.setHeader("WWW-Authenticate", "NTLM");
        res.flushBuffer();

        return;
      }

      if (!auth.startsWith("NTLM ")) {
        return;
      }

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

      // Step 1: Negotiation message received
      if (msg[8] == 1)
      {
        // Send challenge message (Step 2)
        res.setContentLength(2);
        res.setStatus(res.SC_UNAUTHORIZED);
        res.setHeader("WWW-Authenticate", "NTLM " + new 
sun.misc.BASE64Encoder().encodeBuffer(CHALLENGE_MESSAGE));
        res.flushBuffer();
        return;
      }

      // Step 3: Authentication message received
      if (msg[8] == 3)
      {
        int off = 30;
        int length, offset;

        length = (msg[off+1]<<8) + msg[off];
        offset = (msg[off+3]<<8) + msg[off+2];
        String domain = new String(msg, offset, length);

        length = (msg[off+9]<<8) + msg[off+8];
        offset = (msg[off+11]<<8) + msg[off+10];
        user = new String(msg, offset, length);

        length = (msg[off+17]<<8) + msg[off+16];
        offset = (msg[off+19]<<8) + msg[off+18];
        String ws = new String(msg, offset, length);

        System.out.println("Username: " + removeBlanks(user) + " Domain: " + 
removeBlanks(domain) + " Workstation: " + removeBlanks(ws));

      }
    }
    catch (Throwable ex){
      ex.printStackTrace();
    }
  }

  /**
  * Removes non-printable characters from a string
  */
  private String removeBlanks(String s) {
    StringBuffer sb = new StringBuffer();
    for (int i = 0; i < s.length(); i++) {
      char c = s.charAt(i);
      if (c > ' ')
      sb.append(c);
    }
    return sb.toString();
  }

}

-----Original Message-----
From: Tim Funk [mailto:[EMAIL PROTECTED]
Sent: Friday, December 05, 2003 11:58 AM
To: Tomcat Users List
Subject: Re: Basic Authentication


http://jakarta.apache.org/tomcat/faq/windows.html#ntlm

-Tim

Bui, Bao-Ha D wrote:
> Hi all,
> 
> I need to capture the WinNT account name of users to a jsp page.  
> 
> We have Active Directory at our company.  We can have a basic login form
> (that standard pop up login form from Window).  
> 
> Could anyone tell me where to start and how to set it up?  I have looked at
> the HowTo for Tomcat Realm on Apache website but not quite get it.  
> 


---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]


---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to