erase-credentials配置

转自: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()清楚这些信息。

原文地址:https://www.cnblogs.com/drizzlewithwind/p/6209526.html