登录时更改spring security中的用户名

时间:2015-04-03 14:35:36

标签: java spring spring-security

我想在此用户登录系统时更改用户名。我实现了自己的crypt算法,无法获得真正的密码来进行新的身份验证并将其放入

Authentication authentication = new UsernamePasswordAuthenticationToken(principal, credentials);

2 个答案:

答案 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。这个解决方案很简单,很有效。

相关问题