title: Excluding GrantedAuthority from userDetails in spring security
body: <p>I want to consider role as an attribute of a user rather than having an independent role class, hence I don't need to have a table for roles in my DB. But the generic spring <code>UserDetails</code> service passes <code>GrantedAuthority</code> (i.e. <code>Collection&lt;GrantedAuthority&gt; getAuthorities())</code> as one of the user detail parameters. </p>
  
  <p>What I want to do is replacing this generic <code>GrantedAuthority</code> parameter with a role (String role) declared in my 'User' class as shown below. </p>
  
  <pre><code>@Entity(name="usr")
  public class User {
      @Id
      @Column(unique=true)
      private String username;
      private String password;
      private String role;
  
      public String getUsername() {               
          return username;        
      }
  
      public void setUsername(String username) {          
          this.username = username;       
      }
  
      public String getPassword() {
          return password;
      }
  
      public void setPassword(String password) {
          this.password = password;
      }
  
      public String getRole() {
          return role;
      }
  
      public void setRole(String role) {
          this.role = role;
      }
  }
  </code></pre>
  
  <p>And my customUserdetail service class:</p>
  
  <pre><code>@Service
  @Transactional(readOnly = true)
  public class CustomUserDetailsService implements UserDetailsService {
  
      @Autowired
      private UserRepository repository;
  
      public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {               
          try {
              x.y.User  user = repository.findByUsername(username);
  
              boolean enabled = true;
              boolean accountNonExpired = true;
              boolean credentialsNonExpired = true;
              boolean accountNonLocked = true;
  
              return new User(
                      user.getUsername(), 
                      user.getPassword(),
                      enabled,
                      accountNonExpired,
                      credentialsNonExpired,
                      accountNonLocked,
                      user.getRole());        
  
          } catch (Exception e) {
              throw new RuntimeException(e);
          }
      }               
  }
  </code></pre>
  
  <p>It returns an error since I passed <code>user.getRole()</code>, which is String, instead of <code>Collection&lt;GrantedAuthority&gt; getAuthorities()</code>, which is the default role type in spring security. </p>
  
  <p>how can I customize it so that I can use the role (which is a string type) I've declared as an attribute of the 'User' class?</p>
  
  <p><code>Mind:</code> the normal security flow must be preserved!. meaning, without affecting the normal spring security flow.</p>
  
  <p>Thank you in advance!.</p>