Example usage for org.springframework.security.provisioning MutableUserDetails setPassword

List of usage examples for org.springframework.security.provisioning MutableUserDetails setPassword

Introduction

In this page you can find the example usage for org.springframework.security.provisioning MutableUserDetails setPassword.

Prototype

void setPassword(String password);

Source Link

Usage

From source file:org.springframework.security.provisioning.InMemoryUserDetailsManager.java

public void changePassword(String oldPassword, String newPassword) {
    Authentication currentUser = SecurityContextHolder.getContext().getAuthentication();

    if (currentUser == null) {
        // This would indicate bad coding somewhere
        throw new AccessDeniedException(
                "Can't change password as no Authentication object found in context " + "for current user.");
    }/*from w  ww . ja  va  2 s. co m*/

    String username = currentUser.getName();

    logger.debug("Changing password for user '" + username + "'");

    // If an authentication manager has been set, re-authenticate the user with the
    // supplied password.
    if (authenticationManager != null) {
        logger.debug("Reauthenticating user '" + username + "' for password change request.");

        authenticationManager.authenticate(new UsernamePasswordAuthenticationToken(username, oldPassword));
    } else {
        logger.debug("No authentication manager set. Password won't be re-checked.");
    }

    MutableUserDetails user = users.get(username);

    if (user == null) {
        throw new IllegalStateException("Current user doesn't exist in database.");
    }

    user.setPassword(newPassword);
}

From source file:org.springframework.security.provisioning.InMemoryUserDetailsManager.java

@Override
public UserDetails updatePassword(UserDetails user, String newPassword) {
    String username = user.getUsername();
    MutableUserDetails mutableUser = this.users.get(username.toLowerCase());
    mutableUser.setPassword(newPassword);
    return mutableUser;
}