Java request credentials from user via ntlm
Java request credentials from user via ntlm
When you need a Java servlet to trigger NTLM authentication — the kind where the browser automatically sends Windows credentials — setting the WWW-Authenticate: NTLM header and returning a 401 is the server-side trigger. The browser then handles the challenge-response exchange. The doPost receives the auth token for validation.
- Create a servlet like below
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
import javax.servlet.ServletException;
import java.io.IOException;
import javax.servlet.http.*;
@WebServlet(urlPatterns = {"/servlet/login"})
public class NTLMAuthenticationServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
System.out.println("Asking for creds.....");
resp.setHeader("WWW-Authenticate", "NTLM");
resp.setStatus(HttpServletResponse.SC_UNAUTHORIZED);
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
System.out.println("creds received.....”+req.getHeader("Authorization"));
}
}
This post is licensed under
CC BY 4.0
by the author.