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