I want to consider a role as a user attribute, and not have an independent role class, so I do not need to have a table for roles in my database. But the general spring UserDetailsservice passes GrantedAuthority(i.e., Collection<GrantedAuthority> getAuthorities())as one of the parameters of the user part.
What I want to do is replace this general parameter with the GrantedAuthorityrole (String role) declared in my User class, as shown below.
@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;
}
}
And my customUserdetail class of service:
@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);
}
}
}
It returns an error since I passed user.getRole(), which is String, instead Collection<GrantedAuthority> getAuthorities(), which is the default type in spring security.
, ( ), ""?
Mind: !. spring.
!