我想在此用户登录系统时更改用户名。我实现了自己的crypt算法,无法获得真正的密码来进行新的身份验证并将其放入
Authentication authentication = new UsernamePasswordAuthenticationToken(principal, credentials);
答案 0 :(得分:4)
登录后,您可以使用以下代码更改用户名和密码:
Collection<SimpleGrantedAuthority> nowAuthorities =(Collection<SimpleGrantedAuthority>)SecurityContextHolder
.getContext().getAuthentication().getAuthorities();
UsernamePasswordAuthenticationToken authentication = new UsernamePasswordAuthenticationToken(username, password, nowAuthorities);
SecurityContextHolder.getContext().setAuthentication(authentication);
答案 1 :(得分:0)
Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
User userDetails = (User) authentication.getPrincipal();
userDetails.setUsername("newusername");
我的类User实现了UserDetails。这个解决方案很简单,很有效。