title: Spring Security - How to get the roles assigned to user
body: <p>I am implementing JWT Role Based Authorization. I am testing my apis through postman.</p>
  <p>The users first makes a <code>POST</code> request and registers itself where we pass in the firstname, empid and password. The user on successfull registarion returns a response including a column <code>roles</code> which is <code>null</code> in the starting. Then I manually assign the user role. I have two roles in my<code>ROLE</code> table i.e ADMIN or USER which I assign them manually.</p>
  <p>After this, the user needs to authenticate or sign in in order for the token to be gnenrated.</p>
  <p>Upto this point everything is correct. The token is being generated but it returns me the roles values null even though I have assigned the user ADMIN role manually. How do I get the roles assigned to user? please help. I am pasting some code below:</p>
  <p>Below is my api for user authentication : AuthenticationController</p>
  <pre><code>    final Authentication authentication = authenticationManager.authenticate(
              new UsernamePasswordAuthenticationToken(
                  authenticationRequest.getEmpID(),
                  authenticationRequest.getPswd()
              )
      );
      SecurityContextHolder.getContext().setAuthentication(authentication);
      
      final String token = jwtTokenUtil.generateToken(userDetails 
        ,authentication);
  
  
      List&lt;String&gt; roles = authentication.getAuthorities().stream()
      .map(item -&gt; item.getAuthority())
      .collect(Collectors.toList()); 
  
        
  </code></pre>
  <p>As you can see here I have roles defined. How do I get the roles when authenticating the user?</p>
  <p>I added this as mentioned above:</p>
  <pre><code> List&lt;String&gt; roles = authentication.getAuthorities().stream()
      .map(item -&gt; item.getAuthority())
      .collect(Collectors.toList()); 
  </code></pre>
  <p>It does not seem to do anything. What else I am missing?</p>
  <p>Users.java:</p>
  <pre><code>public class Users {
  
  @Id
  @GeneratedValue(strategy = GenerationType.AUTO)
  @Column(name = &quot;USER_ID&quot;)
  private Long id;
  </code></pre>
  <p>//other columns</p>
  <pre><code>@ManyToMany(cascade = CascadeType.ALL, fetch = FetchType.EAGER)
  @JoinTable(name = &quot;CONFIG_USERROLE&quot;, joinColumns = @JoinColumn(name = 
  &quot;USER_ID&quot;), inverseJoinColumns = @JoinColumn(name = &quot;ROLE_ID&quot;))
  private Set&lt;Role&gt; roles; 
  
  public Users(){
  
  }
  </code></pre>
  <p>Role.java:</p>
  <pre><code> public class Role {
  
  @Id
  @GeneratedValue(strategy = GenerationType.AUTO)
  @Column(name = &quot;ROLE_ID&quot;)
  private Long id;
  
  @Column(name = &quot;ROLE&quot;)
  private String role;
  </code></pre>