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

问题描述 投票:2回答:2

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

Authentication authentication = new UsernamePasswordAuthenticationToken(principal, credentials);
java spring spring-security
2个回答
4
投票

登录后,您可以使用以下代码更改用户名和密码:

Collection<SimpleGrantedAuthority> nowAuthorities =(Collection<SimpleGrantedAuthority>)SecurityContextHolder
                    .getContext().getAuthentication().getAuthorities();
UsernamePasswordAuthenticationToken authentication = new UsernamePasswordAuthenticationToken(username, password, nowAuthorities);
SecurityContextHolder.getContext().setAuthentication(authentication);

0
投票
Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        User userDetails = (User) authentication.getPrincipal();
        userDetails.setUsername("newusername");

我的类User实现了UserDetails。这个解决方案很简单,很有效。

© www.soinside.com 2019 - 2024. All rights reserved.