Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Change username in spring security when logged in

I want to change username while this user logged in system. I implemented my own crypt algorithm and cannot get real password to make a new authentication and put it in

Authentication authentication = new UsernamePasswordAuthenticationToken(principal, credentials);
like image 639
ans Avatar asked Apr 03 '15 14:04

ans


People also ask

Why does the session ID change when I authenticate through Spring Security?

Why does the session Id change when I authenticate through Spring Security? With the default configuration, Spring Security invalidates the existing session when the user authenticates and creates a new one, transferring the session data to it.


1 Answers

After Logging in, you can change your username and password with this code:

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

SecurityContextHolder.getContext().setAuthentication(authentication);
like image 109
Kai - Kazuya Ito Avatar answered Oct 22 '22 16:10

Kai - Kazuya Ito