// authenticate user
if (!authorizationHeader.isNull()) {
// log the user in using the token
IWindowsIdentity windowsIdentity;
try {
windowsIdentity = this.providers.doFilter(request, response);
if (windowsIdentity == null) {
return;
}
} catch (IOException e) {
LOGGER.warn("error logging in user: {}", e.getMessage());
LOGGER.trace("{}", e);
sendUnauthorized(response, true);
return;
}
IWindowsImpersonationContext ctx = null;
try {
if (!this.allowGuestLogin && windowsIdentity.isGuest()) {
LOGGER.warn("guest login disabled: {}", windowsIdentity.getFqn());
sendUnauthorized(response, true);
return;
}
LOGGER.debug("logged in user: {} ({})", windowsIdentity.getFqn(), windowsIdentity.getSidString());
HttpSession session = request.getSession(true);
if (session == null) {
throw new ServletException("Expected HttpSession");
}
Subject subject = (Subject) session.getAttribute("javax.security.auth.subject");
if (subject == null) {
subject = new Subject();
}
WindowsPrincipal windowsPrincipal = null;
if (this.impersonate) {
windowsPrincipal = new AutoDisposableWindowsPrincipal(windowsIdentity, this.principalFormat,
this.roleFormat);
} else {
windowsPrincipal = new WindowsPrincipal(windowsIdentity, this.principalFormat, this.roleFormat);
}
LOGGER.debug("roles: {}", windowsPrincipal.getRolesString());
subject.getPrincipals().add(windowsPrincipal);
session.setAttribute("javax.security.auth.subject", subject);
LOGGER.info("successfully logged in user: {}", windowsIdentity.getFqn());
request.getSession().setAttribute(PRINCIPALSESSIONKEY, windowsPrincipal);
NegotiateRequestWrapper requestWrapper = new NegotiateRequestWrapper(request, windowsPrincipal);
if (this.impersonate) {
LOGGER.debug("impersonating user");
ctx = windowsIdentity.impersonate();
}
chain.doFilter(requestWrapper, response);
} finally {
if (this.impersonate && ctx != null) {
LOGGER.debug("terminating impersonation");
ctx.revertToSelf();
} else {
windowsIdentity.dispose();
}
}
return;
}