[英]How to update the SecurityContextHolder username for the update for current user
在我的应用程序中,当前登录用户的usernam被更改时,我想更新当前用户的UserDetails主体。 我怎样才能做到这一点? 我通过以下方法获取当前用户名:
public UserDetails getSystemUser(){
try {
UserDetails principal = (UserDetails) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
return principal;
}catch (ClassCastException e){
return null;
}
}
据我所知,没有setusername方法。
请记住,此解决方案未经测试可能或不起作用。
所以首先是明确的背景。 然后创建一个新的空上下文,并使用更新的用户名和所需的所有OTHER INFO设置新创建的身份验证对象。
Authentication oldAuthenticationObject = SecurityContextHolder.getContext().getAuthentication(); SecurityContextHolder.clearContext(); SecurityContext context = SecurityContextHolder.createEmptyContext(); UsernamePasswordAuthenticationToken authentication = new UsernamePasswordAuthenticationToken( NEWUSERNAME, null, oldAuthenticationObject.getAuthorities()); context.setAuthentication(authentication); SecurityContextHolder.setContext(context);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.