if ((username == null) || "".equals(username)) { username = "NONE_PROVIDED"; }
    public Authentication authenticate(Authentication authentication)        throws AuthenticationException {        // Determine username        String username = authentication.getPrincipal().toString();        if (authentication.getPrincipal() instanceof UserDetails) {            username = ((UserDetails) authentication.getPrincipal())                .getUsername();        }        boolean cacheWasUsed = true;        UserDetails user = this.userCache.getUserFromCache(username);        if (user == null) {            cacheWasUsed = false;            try {                user = getUserFromBackend(username);            } catch (BadCredentialsException ex) {                if (this.context != null) {                    context.publishEvent(new AuthenticationFailureUsernameNotFoundEvent(                            authentication,                            new User(username, "*****", false,                                new GrantedAuthority[0])));                }                throw ex;            }        }        if (!user.isEnabled()) {            if (this.context != null) {                context.publishEvent(new AuthenticationFailureDisabledEvent(                        authentication, user));            }            throw new DisabledException("User is disabled");        }        if (!isPasswordCorrect(authentication, user)) {            // Password incorrect, so ensure we're using most current password            if (cacheWasUsed) {                cacheWasUsed = false;                user = getUserFromBackend(username);            }            if (!isPasswordCorrect(authentication, user)) {                if (this.context != null) {                    context.publishEvent(new AuthenticationFailurePasswordEvent(                            authentication, user));                }                throw new BadCredentialsException("Bad credentials presented");            }        }        if (!cacheWasUsed) {            // Put into cache            this.userCache.putUserInCache(user);            // As this appears to be an initial login, publish the event            if (this.context != null) {                context.publishEvent(new AuthenticationSuccessEvent(                        authentication, user));            }        }        Object principalToReturn = user;        if (forcePrincipalAsString) {            principalToReturn = user.getUsername();        }        return createSuccessAuthentication(principalToReturn, authentication,            user);    }