Example usage for org.springframework.security.authentication.event AuthenticationFailureLockedEvent AuthenticationFailureLockedEvent

List of usage examples for org.springframework.security.authentication.event AuthenticationFailureLockedEvent AuthenticationFailureLockedEvent

Introduction

In this page you can find the example usage for org.springframework.security.authentication.event AuthenticationFailureLockedEvent AuthenticationFailureLockedEvent.

Prototype

public AuthenticationFailureLockedEvent(Authentication authentication, AuthenticationException exception) 

Source Link

Usage

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

@Override
public Authentication authenticate(Authentication req) throws AuthenticationException {
    logger.debug("Processing authentication request for " + req.getName());

    if (req.getCredentials() == null) {
        BadCredentialsException e = new BadCredentialsException("No password supplied");
        publish(new AuthenticationFailureBadCredentialsEvent(req, e));
        throw e;/*w ww . j  ava2 s  . c o  m*/
    }

    UaaUser user = getUaaUser(req);

    if (user == null) {
        logger.debug("No user named '" + req.getName() + "' was found for origin:" + origin);
        publish(new UserNotFoundEvent(req));
    } else {
        if (!accountLoginPolicy.isAllowed(user, req)) {
            logger.warn("Login policy rejected authentication for " + user.getUsername() + ", " + user.getId()
                    + ". Ignoring login request.");
            AuthenticationPolicyRejectionException e = new AuthenticationPolicyRejectionException(
                    "Your account has been locked because of too many failed attempts to login.");
            publish(new AuthenticationFailureLockedEvent(req, e));
            throw e;
        }

        boolean passwordMatches = ((CharSequence) req.getCredentials()).length() != 0
                && encoder.matches((CharSequence) req.getCredentials(), user.getPassword());

        if (!passwordMatches) {
            logger.debug("Password did not match for user " + req.getName());
            publish(new UserAuthenticationFailureEvent(user, req));
        } else {
            logger.debug(
                    "Password successfully matched for userId[" + user.getUsername() + "]:" + user.getId());

            if (!(allowUnverifiedUsers && user.isLegacyVerificationBehavior()) && !user.isVerified()) {
                publish(new UnverifiedUserAuthenticationEvent(user, req));
                logger.debug("Account not verified: " + user.getId());
                throw new AccountNotVerifiedException("Account not verified");
            }

            checkPasswordExpired(user.getPasswordLastModified());

            UaaAuthentication success = new UaaAuthentication(new UaaPrincipal(user), user.getAuthorities(),
                    (UaaAuthenticationDetails) req.getDetails());

            success.setAuthenticationMethods(Collections.singleton("pwd"));
            Date passwordNewerThan = getPasswordNewerThan();
            if (passwordNewerThan != null) {
                if (user.getPasswordLastModified() == null
                        || (passwordNewerThan.getTime() > user.getPasswordLastModified().getTime())) {
                    logger.info("Password change required for user: " + user.getEmail());
                    throw new PasswordChangeRequiredException(success, "User password needs to be changed");
                }
            }

            if (user.isPasswordChangeRequired()) {
                logger.info("Password change required for user: " + user.getEmail());
                throw new PasswordChangeRequiredException(success, "User password needs to be changed");
            }
            publish(new UserAuthenticationSuccessEvent(user, success));

            return success;
        }
    }

    BadCredentialsException e = new BadCredentialsException("Bad credentials");
    publish(new AuthenticationFailureBadCredentialsEvent(req, e));
    throw e;
}