Example usage for org.springframework.security.authentication UsernamePasswordAuthenticationToken getName

List of usage examples for org.springframework.security.authentication UsernamePasswordAuthenticationToken getName

Introduction

In this page you can find the example usage for org.springframework.security.authentication UsernamePasswordAuthenticationToken getName.

Prototype

public String getName() 

Source Link

Usage

From source file:org.springframework.security.ui.ntlm.NtlmAuthenticationFilter.java

/**
 * Authenticates the user credentials acquired from NTLM against the Spring
 * Security <code>AuthenticationManager</code>.
 *
 * @param request the <code>HttpServletRequest</code> object.
 * @param response the <code>HttpServletResponse</code> object.
 * @param session the <code>HttpSession</code> object.
 * @param auth the <code>NtlmPasswordAuthentication</code> object.
 * @throws IOException// w  ww .  j  av  a2s  . c  om
 */
private void authenticate(final HttpServletRequest request, final HttpServletResponse response,
        final HttpSession session, final NtlmPasswordAuthentication auth) throws IOException {
    final Authentication authResult;
    final UsernamePasswordAuthenticationToken authRequest;
    final Authentication backupAuth;

    authRequest = new NtlmUsernamePasswordAuthenticationToken(auth, stripDomain);
    authRequest.setDetails(authenticationDetailsSource.buildDetails(request));

    // Place the last username attempted into HttpSession for views
    //       session.setAttribute(UsernamePasswordAuthenticationFilter.SPRING_SECURITY_LAST_USERNAME_KEY, authRequest.getName());
    // Replace in your code by :
    // SecurityContextHolder.getContext().getAuthentication().getPrincipal();

    // Backup the current authentication in case of an AuthenticationException
    backupAuth = SecurityContextHolder.getContext().getAuthentication();

    try {
        // Authenticate the user with the authentication manager
        authResult = authenticationManager.authenticate(authRequest);
    } catch (AuthenticationException failed) {
        if (LOGGER.isInfoEnabled()) {
            LOGGER.info("Authentication request for user: " + authRequest.getName() + " failed: "
                    + failed.toString());
        }

        // Reset the backup Authentication object and rethrow the AuthenticationException
        SecurityContextHolder.getContext().setAuthentication(backupAuth);

        if (retryOnAuthFailure && (failed instanceof AuthenticationCredentialsNotFoundException
                || failed instanceof InsufficientAuthenticationException)) {
            LOGGER.debug("Restart NTLM authentication handshake due to AuthenticationException");
            session.setAttribute(STATE_ATTR, BEGIN);
            throw new NtlmBeginHandshakeException();
        }

        throw failed;
    }

    // Set the Authentication object with the valid authentication result
    SecurityContextHolder.getContext().setAuthentication(authResult);
}