Example usage for org.springframework.security.core AuthenticationException getStackTrace

List of usage examples for org.springframework.security.core AuthenticationException getStackTrace

Introduction

In this page you can find the example usage for org.springframework.security.core AuthenticationException getStackTrace.

Prototype

public StackTraceElement[] getStackTrace() 

Source Link

Document

Provides programmatic access to the stack trace information printed by #printStackTrace() .

Usage

From source file:org.cloudfoundry.identity.uaa.authentication.manager.ChainedAuthenticationManager.java

@Override
public Authentication authenticate(Authentication authentication) throws AuthenticationException {
    if (authentication == null) {
        return authentication;
    }//from  w ww.  j  a v  a  2  s  .  c o  m
    UsernamePasswordAuthenticationToken output = null;
    if (authentication instanceof UsernamePasswordAuthenticationToken) {
        output = (UsernamePasswordAuthenticationToken) authentication;
    } else {
        output = new UsernamePasswordAuthenticationToken(authentication.getPrincipal(),
                authentication.getCredentials(), authentication.getAuthorities());
        output.setDetails(authentication.getDetails());
    }
    boolean authenticated = false;
    Authentication auth = null;
    AuthenticationException lastException = null;
    boolean lastResult = false;
    boolean shallContinue = true;
    if (delegates == null || delegates.length == 0) {
        throw new ProviderNotFoundException("No available authentication providers.");
    }
    for (int i = 0; shallContinue && i < delegates.length; i++) {

        boolean shallAuthenticate = (i == 0)
                || (lastResult && IF_PREVIOUS_TRUE.equals(delegates[i].getRequired()))
                || ((!lastResult) && IF_PREVIOUS_FALSE.equals(delegates[i].getRequired()));

        if (shallAuthenticate) {
            if (logger.isDebugEnabled()) {
                logger.debug("Attempting chained authentication of " + output + " with manager:"
                        + delegates[i].getAuthenticationManager() + " required:" + delegates[i].getRequired());
            }
            Authentication thisAuth = null;
            try {
                thisAuth = delegates[i].getAuthenticationManager().authenticate(auth != null ? auth : output);
            } catch (AuthenticationException x) {
                if (logger.isDebugEnabled()) {
                    logger.debug("Chained authentication exception:" + x.getMessage() + " at:"
                            + (x.getStackTrace().length > 0 ? x.getStackTrace()[0] : "(no stack trace)"));
                }
                lastException = x;
                if (delegates[i].getStopIf() != null) {
                    for (Class<? extends AuthenticationException> exceptionClass : delegates[i].getStopIf()) {
                        if (exceptionClass.isAssignableFrom(x.getClass())) {
                            shallContinue = false;
                            break;
                        }
                    }
                }
            }
            lastResult = thisAuth != null && thisAuth.isAuthenticated();

            if (lastResult) {
                authenticated = true;
                auth = thisAuth;
            } else {
                authenticated = false;
                auth = null;
            }

        } else {
            shallContinue = false;
        }
        if (logger.isDebugEnabled()) {
            logger.debug("Chained Authentication status of " + output + " with manager:" + delegates[i]
                    + "; Authenticated:" + authenticated);
        }
    }
    if (authenticated) {
        return auth;
    } else if (lastException != null) {
        //we had at least one authentication exception, throw it
        throw lastException;
    } else {
        //not authenticated, but return the last of the result
        return auth;
    }
}