当前用户获取信息
我们在SecurityContextHolder内存储目前与应用程序交互的主要细节。Spring Security使用一个Authentication对象来表示这些信息。 你通常不需要创建一个自我认证的对象,但它是很常见的用户查询的Authentication对象。你可以使用以下代码块-从你的应用程序的任何部分-获得当前身份验证的用户的名称,例如:
获取登录用户信息
Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
获取登录用户的权限
Collection<? extends GrantedAuthority> authorities = SecurityContextHolder.getContext().getAuthentication().getAuthorities();
当我们登录成功后,用户的登录信息都保存在一个抽象类 AbstractAuthenticationProcessingFilter 拦截并做登录处理后 最后会调用
successfulAuthentication(request, response, chain, authResult);
方法 successfulAuthentication 中可以明确看到 信息保存到了SecurityContextHolder.getContext().setAuthentication(authResult);
所以获取的时候 可以
Authentication authentication = SecurityContextHolder.getContext().getAuthentication();