轉自:Spring Security怎樣不讓默認的ProviderManager清除密碼等信息
<authentication-manager erase-credentials="false"> ... </authentication-manager>
erase-credentials默認為true,會在ProviderManager(默認的AuthenticationManager實現)的方法
public Authentication authenticate(Authentication authentication) throws AuthenticationException
返回前調用
((CredentialsContainer)result).eraseCredentials();
清除credentials等信息,所以我們使用
SecurityContextImpl securityContextImpl = (SecurityContextImpl) request.getSession().getAttribute("SPRING_SECURITY_CONTEXT"); Authentication authentication = securityContextImpl.getAuthentication(); // 登錄密碼,未加密的 String password = (String)(authentication.getCredentials());
password總是為null。
將erase-credentials設置為false后,不會清除這些保密信息,但是建議在使用完之后自己調用eraseCredentials()清楚這些信息。