protected boolean authenticateUser(HttpServletRequest request) {

        try {
            String username = DatatypeHelper.safeTrimOrNullString(request.getParameter(usernameAttribute));
            String password = DatatypeHelper.safeTrimOrNullString(request.getParameter(passwordAttribute));

            SimpleCallbackHandler cbh = new SimpleCallbackHandler(username, password);

            javax.security.auth.login.LoginContext jaasLoginCtx = new javax.security.auth.login.LoginContext(
                    jaasConfigName, cbh);

            jaasLoginCtx.login();
            log.debug("Successfully authenticated user {}", username);

            Subject subject = jaasLoginCtx.getSubject();
            Set<Principal> principals = subject.getPrincipals();

            if (principals.isEmpty()) {
                request.setAttribute(LoginHandler.PRINCIPAL_NAME_KEY, username);
            } else {
                Principal principal = principals.iterator().next();
                String principalName = DatatypeHelper.safeTrimOrNullString(principal.getName());
                if (principalName == null) {
                    request.setAttribute(LoginHandler.PRINCIPAL_NAME_KEY, username);
                } else {
                    request.setAttribute(LoginHandler.PRINCIPAL_NAME_KEY, principal.getName());
                }
                request.setAttribute(LoginHandler.SUBJECT_KEY, jaasLoginCtx.getSubject());
            }

            return true;
        } catch (LoginException e) {
            log.debug("User authentication failed", e);
            return false;
        }
    }