Example usage for org.springframework.security.core.userdetails UserCache removeUserFromCache

List of usage examples for org.springframework.security.core.userdetails UserCache removeUserFromCache

Introduction

In this page you can find the example usage for org.springframework.security.core.userdetails UserCache removeUserFromCache.

Prototype

void removeUserFromCache(String username);

Source Link

Document

Removes the specified user from the cache.

Usage

From source file:grails.plugin.springsecurity.SpringSecurityUtils.java

/**
 * Rebuild an Authentication for the given username and register it in the security context.
 * Typically used after updating a user's authorities or other auth-cached info.
 * <p/>/*from   w  w  w  .  java 2s.c  o  m*/
 * Also removes the user from the user cache to force a refresh at next login.
 *
 * @param username the user's login name
 * @param password optional
 */
public static void reauthenticate(final String username, final String password) {
    UserDetailsService userDetailsService = getBean("userDetailsService");
    UserCache userCache = getBean("userCache");

    UserDetails userDetails = userDetailsService.loadUserByUsername(username);
    SecurityContextHolder.getContext().setAuthentication(new UsernamePasswordAuthenticationToken(userDetails,
            password == null ? userDetails.getPassword() : password, userDetails.getAuthorities()));
    userCache.removeUserFromCache(username);
}