title: Roles not getting fetched from database along with the user object
body: <p>I am creating a user registration flow with spring boot (2.1.3.RELEASE) . With the help of few articles I am able to successfully add a user along with its roles and user is able to login into the system. The problem is when user is successfully loged-in, the authentication obect has empty role even when I can see th correct role mapping in mysql database (honestly I am not able to get exactly how roles are fetched from database when findByUserName method is called.
  Below is my code:</p>
  
  <p>Entity objects
  1. User.java</p>
  
  <pre><code>public class User implements UserDetails {
  
      private static final long serialVersionUID = 1L;
  
      public User() {
          //Verification flow 2. set enabled = false
          this.enabled = false;
      }
  
      @Id
      @GeneratedValue(strategy = GenerationType.IDENTITY)
      @Column(name = "user_unique_number")
      private long id;
  
      @UniqueUser(groups = UniqueUserOrder.class)
      @Column(name = "username", length = 60,nullable = false, unique = true)
      private String username;
  
      @Column(name = "email", nullable = false, unique = true)
      private String email;
  
      @Column(name = "password", nullable = false)
      private String password;
  
      @OneToMany(mappedBy = "user", cascade = CascadeType.ALL, fetch = FetchType.EAGER)
      private Set&lt;UserRole&gt; userRoles = new HashSet&lt;&gt;();
  
      @Override
      public Collection&lt;? extends GrantedAuthority&gt; getAuthorities() {
          Set&lt;GrantedAuthority&gt; authorities = new HashSet&lt;&gt;();
          userRoles.forEach(ur -&gt; authorities.add(new Authority(ur.getRole().getName())));
          return authorities;
      }
  
      public Set&lt;UserRole&gt; getUserRoles() {
          return userRoles;
      }
  
      public void setUserRoles(Set&lt;UserRole&gt; userRoles) {
          this.userRoles = userRoles;
      }
  
      ...//OTHER GETTERS AND SETTERS
  }
  </code></pre>
  
  <ol start="2">
  <li><p>Roles.java</p>
  
  <p>public class Role</p>
  
  <pre><code>{@Id
  @GeneratedValue(strategy = GenerationType.IDENTITY)
  @Column(name = "id")
  private int id;
  
  private String name;
  
  @OneToMany(mappedBy = "role", cascade = CascadeType.ALL, fetch = FetchType.LAZY)
  private Set&lt;UserRole&gt; userRoles = new HashSet&lt;&gt;();
  
  public Role() {
  
  }
  
  public Role(RolesEnum rolesEnum) {
      this.id = rolesEnum.getId();
      this.name = rolesEnum.getRoleName();
  }
  
  
  public Set&lt;UserRole&gt; getUserRoles() {
      return userRoles;
  }
  
  public void setUserRoles(Set&lt;UserRole&gt; userRoles) {
      this.userRoles = userRoles;
  }
  
  ...//OTHER GETTERS AND SETTERS }
  </code></pre></li>
  <li><p>UserRole.java</p>
  
  <p>public class UserRole {</p>
  
  <pre><code>public UserRole() {}
  
  @Id
  @GeneratedValue(strategy = GenerationType.IDENTITY)
  @Column(name = "id")
  private long id;
  
  public UserRole(User user, Role role) {
      this.user = user;
      this.role = role;
  }
  
  @ManyToOne(fetch = FetchType.EAGER)
  @JoinColumn(name = "user_id")
  private User user;
  
  
  @ManyToOne(fetch = FetchType.EAGER)
  @JoinColumn(name = "role_id")
  private Role role;
  
  ...//OTHER GETTERS AND SETTERS
  </code></pre>
  
  <p>}</p></li>
  </ol>
  
  <p>4.RolesEnum.java</p>
  
  <pre><code>public enum RolesEnum {
  
      ADMIN(1, "ROLE_ADMIN"),
      BASIC(2, "ROLE_BASIC");
  
      private final int id;
  
      private final String roleName;
  
      ...//OTHER GETTERS AND SETTERS
  
  }
  </code></pre>
  
  <p>New user is getting created as below:</p>
  
  <pre><code>...
  ...
  String encryptedPassword = passwordEncoder.encode(adminPassword);
  user.setPassword(encryptedPassword);
  user.setUsername(adminUsername);
  user.setEmail(adminEmail);
  user.setUserCreateTime(LocalDateTime.now());
  
  Set&lt;UserRole&gt; userRoles = new HashSet&lt;&gt;();
  UserRole userRole = new UserRole();
  userRole.setUser(user);
  userRole.setRole(new Role(RolesEnum.ADMIN));
  userRoles.add(userRole);
  user.getUserRoles().addAll(userRoles);
  
  user.setAccountNonLocked(true);
  user.setEnabled(true);
  user.setAccountNonExpired(true);
  user.setCredentialsNonExpired(true);
  user = userRepository.save(user);
  ...
  ...
  </code></pre>
  
  <p>At this point user is added successfully along with the roles in database</p>
  
  <p>User is also able to successfully log-in but the problem is after logging, authentication object has an empty list of roles
  Below is the code which is failing</p>
  
  <pre><code>public class SecurityConfig extends WebSecurityConfigurerAdapter {
  ......
  ......
  private AuthenticationSuccessHandler loginSuccessHandler() {
          return (request, response, **authentication**) -&gt; {
              Collection&lt;? extends GrantedAuthority&gt; authorities
               = authentication.getAuthorities();
              for (GrantedAuthority grantedAuthority : authorities) {
                  if (grantedAuthority.getAuthority().equals("ROLE_ADMIN")) {
                      isAdmin = true;
                      break;
                  } else if (grantedAuthority.getAuthority().equals("ROLE_ADMIN")) {
                      isBasic = true;
                      break;
                  }
              }
  
                if (isAdmin) { return "/admin/"; } else if (isBasic) { return
                "/profile.html"; } else { throw new IllegalStateException(); }
  
              response.sendRedirect("/");
          };
      }
  ......
  ......
  </code></pre>
  
  <p>When i inspect authentication object using eclipse the roles are not getting fetched from database
  Here is the code for fetching user@Override</p>
  
  <pre><code>public User findByUserName(String username) {
          return userRepository.findByUsername(username);
  }
  </code></pre>
  
  <p>Do I need to add some additional logic to fetch roles along with user or Spring handles it behind the scene? Please let me know what I am doing wrong here... Thanks</p>