title: update authorities of logged in user Spring security
body: <p><br>
  I have integrated spring security on a restful API with spring-boot,
  and now I am looking for a way to update authorities of an already logged user.
  <br>
  I have implemented a custom user for Spring authentication !
  I can get All principals (all logged in users) from the <code>sessionRegistry</code> like this way : <br><br>
          <code>List&lt;Object&gt; allPrincipals = sessionRegistry.getAllPrincipals();</code>
  <br><br>
  now when I try to update the authorities of a specific user, the authorities won't update I always got the same authorities until logging out and then loggin in.</p>
  
  <p>Here is my code : </p>
  
  <pre><code>List&lt;Object&gt; allPrincipals = sessionRegistry.getAllPrincipals();
  
  for (Object principal : allPrincipals) {
  
      if (principal instanceof SpringSecurityAuthentificatedUser) {
  
          SpringSecurityAuthentificatedUser principalInSession = (SpringSecurityAuthentificatedUser) principal;
          if(id.equals(principalInSession.getUser().getId().toString())){
  
              // updating the user's authorities
              principalInSession.getUser().setRole(role);
              principalInSession.getAuthorities().clear();
              principalInSession.getAuthorities().add(new SimpleGrantedAuthority(role));
  
              SpringSecurityAuthentificatedUser springSecurityAuthentificatedUser =
                      new SpringSecurityAuthentificatedUser(principalInSession.getUser(),principalInSession.getUser().getPassword(),principalInSession.getAuthorities());
  
  
              SecurityContextHolder.getContext().setAuthentication(
                      new UsernamePasswordAuthenticationToken(springSecurityAuthentificatedUser, principalInSession.getUser().getPassword(), principalInSession.getAuthorities())
              );
          }
  
      }
  }
  </code></pre>
  
  <p>could any one help me ?</p>