2015-04-03 56 views
0

我想在此用戶登錄系統時更改用戶名。我實現了我自己的隱窩算法並不能得到真正的密碼,以新的身份驗證,並把它放在登錄時在彈簧安全中更改用戶名

Authentication authentication = new UsernamePasswordAuthenticationToken(principal, credentials); 
+0

這應該回答你的問題。 http://stackoverflow.com/a/14174404/1251350 – 2015-04-03 14:40:45

+0

@FarajFarook謝謝 – ans 2015-04-03 15:45:07

回答

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

我的類用戶實現的UserDetails。這個解決方案很簡單,它的工作。

0

登錄後,你可以用這個代碼更改用戶名和密碼:

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

這將改變oauth以及? – 2017-11-09 16:55:14

+0

是的。爲了確保您的用戶名是否被更改或者沒有更改用戶名後使用這些代碼,我建議您使用此代碼SecurityContextHolder.getContext()。getAuthentication()。getName();獲取用戶名,您將能夠看到更改的用戶名。 – Kai 2017-11-10 03:53:23

+0

不,我今天沒有測試訪問令牌在數據庫中仍然沒有改變! – 2017-11-10 03:54:39