J'ai un problème avec l'obtention des rôles utilisateur en tant que String List à partir de Collection des rôles dans l'objet User. Je veux obtenir des rôles d'utilisateur en tant que List pour l'implémenter dans ma méthode UserServiceImlementation on loadUserByUsername dans laquelle je passe rolenames sous forme de liste à la classe SocialUserDetailsImplementation pour rendre la connexion sociale disponible. Sera reconnaissant pour la solution.

Modèle utilisateur :

@Entity
@Table(uniqueConstraints = @UniqueConstraint(columnNames = "email"))
public class User {

    public static final String ROLE_USER = "ROLE_USER";
    public static final String ROLE_ADMIN = "ROLE_ADMIN";

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    @Column(name ="user_id", nullable = false)
    private Long userId;

    private String firstName;
    private String lastName;
    private String email;
    private String password;
    private String userName;

    @ManyToMany(fetch = FetchType.EAGER, cascade = CascadeType.ALL)
    @JoinTable(
            name = "users_roles",
            joinColumns = @JoinColumn(
                    name = "user_id", referencedColumnName = "user_id"),
            inverseJoinColumns = @JoinColumn(
                    name = "role_id", referencedColumnName = "id"))
    private Collection<Role> roles;

    @Column(name = "enabled")
    private boolean enabled;


    public User() {
        super();
        this.enabled=false;

    }

    public User(String firstName, String lastName, String email, String password) {
        this.firstName = firstName;
        this.lastName = lastName;
        this.email = email;
        this.password = password;
    }

    public User(String firstName, String lastName, String email, String password, Collection<Role> roles) {
        this.firstName = firstName;
        this.lastName = lastName;
        this.email = email;
        this.password = password;
        this.roles = roles;
    }

    public User(String firstName, String lastName, String email, String password, String userName, Collection<Role> roles) {
        this.firstName = firstName;
        this.lastName = lastName;
        this.email = email;
        this.password = password;
        this.userName = userName;
        this.roles = roles;
    }

    public Long getUserId() {
        return userId;
    }

    public void setUserId(Long userId) {
        this.userId = userId;
    }

    public String getFirstName() {
        return firstName;
    }

    public void setFirstName(String firstName) {
        this.firstName = firstName;
    }

    public String getLastName() {
        return lastName;
    }

    public void setLastName(String lastName) {
        this.lastName = lastName;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public boolean isEnabled() {
        return enabled;
    }

    public void setEnabled(boolean enabled) {
        this.enabled = enabled;
    }

    public Collection<Role> getRoles() {
        return roles;
    }

    public void setRoles(Collection<Role> roles) {
        this.roles = roles;
    }

    public String getUserName() {
        return userName;
    }

    public void setUserName(String userName) {
        this.userName = userName;
    }


}

UserServiceImpl :

@Service
public class UserServiceImpl implements UserService {

    @Autowired
    private UserRepository userRepository;




    @Override
    public UserDetails loadUserByUsername(String userName) throws UsernameNotFoundException {
        User user = userRepository.findByUserName(userName);
        if (user == null) {
            throw new UsernameNotFoundException("Invalid username or password.");
        }


       List<String> roleNames =  (Collectors.toList(user.getRoles()));



        List<GrantedAuthority> grantList = new ArrayList<GrantedAuthority>();
        if (roleNames != null) {
            for (String role : roleNames) {
                GrantedAuthority authority = new SimpleGrantedAuthority(role);
                grantList.add(authority);
            }
        }


        SocialUserDetailsImpl userDetails = new SocialUserDetailsImpl(user,  roleNames);
                return userDetails;
    }

SocialUserDetailsImpl :

public class SocialUserDetailsImpl implements SocialUserDetails {

private static final long serialVersionUID = 1L;
private List<GrantedAuthority> list = new ArrayList<GrantedAuthority>();
private User user;

public SocialUserDetailsImpl(User user, List<String> roleNames) {
    this.user = user;

    for (String roleName : roleNames) {

        GrantedAuthority grant = new SimpleGrantedAuthority(roleName);
        this.list.add(grant);
    }
}

@Override
public String getUserId() {
    return this.user.getUserId() + "";
}

@Override
public String getUsername() {
    return user.getUserName();
}

@Override
public Collection<? extends GrantedAuthority> getAuthorities() {
    return list;
}

@Override
public String getPassword() {
    return user.getPassword();
}

@Override
public boolean isAccountNonExpired() {
    return true;
}

@Override
public boolean isAccountNonLocked() {
    return true;
}

@Override
public boolean isCredentialsNonExpired() {
    return true;
}

@Override
public boolean isEnabled() {
    return this.user.isEnabled();
}

}
-2
D.Skowron 27 janv. 2019 à 17:48

3 réponses

Meilleure réponse

Dans cette partie

List<String> roleNames =  (Collectors.toList(user.getRoles()));

Vos roleNames contiennent une liste de user.getRoles (). toString () au lieu des noms de rôle attendus;

Essayer

List<String> roleNames =  user.getRoles().stream()
    .map(Role::getName)
    .collect(Collectors.toList());

Ajoutez également transitoire aux champs ROLE_USER et ROLE_ADMIN.

0
Bor Laze 27 janv. 2019 à 15:06

Faites quelque chose comme ça,

List<Object> roleNames =  (Collectors.toList(user.getRoles()));
List<String> roleNamesString= new List<string> ();

for(Object a: roleNames){
   roleNameString.add(String.valueOf(a));
}

Vous devrez probablement remplacer la méthode toString de la classe Role, en fonction de la complexité du modèle.

-1
Bon 27 janv. 2019 à 15:08

En gros, vous n'avez pas besoin de convertir les rôles en List<String>, vous pouvez créer votre List<GrantedAuthority> en

user.getRoles().stream()
    .map(role -> new SimpleGrantedAuthority(role.getRoleName()))
    .collect(Collectors.toList());

Si vous en avez vraiment besoin List<String>, vous pouvez le faire

user.getRoles().stream()
    .map(Role::getRoleName)
    .collect(Collectors.toList());
0
aBnormaLz 27 janv. 2019 à 15:39