猿问

将 Collection<Object> 转换为 List<String>

我在String List从对象Collection中的角色中获取用户角色时遇到问题User。我想获取用户角色以在我的 on方法中List实现它,在该方法中,我将角色名称作为列表传递给类以使社交登录可用。将不胜感激的解决方案。UserServiceImlementationloadUserByUsernameSocialUserDetailsImplementation

用户服务实现:


@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;

}

鸿蒙传说
浏览 903回答 3
3回答

慕的地10843

做这样的事情,List<Object> roleNames =&nbsp; (Collectors.toList(user.getRoles()));List<String> roleNamesString= new List<string> ();for(Object a: roleNames){&nbsp; &nbsp;roleNameString.add(String.valueOf(a));}您可能需要覆盖 Role 类的 toString 方法,具体取决于模型的复杂性。

胡说叔叔

基本上,您不需要将角色转换为List<String>您可以List<GrantedAuthority>通过以下方式构建的角色user.getRoles().stream()&nbsp; &nbsp; .map(role -> new SimpleGrantedAuthority(role.getRoleName()))&nbsp; &nbsp; .collect(Collectors.toList());如果你真的需要,List<String>那么你可以做到user.getRoles().stream()&nbsp; &nbsp; .map(Role::getRoleName)&nbsp; &nbsp; .collect(Collectors.toList());

慕后森

在这部分List<String>&nbsp;roleNames&nbsp;=&nbsp;&nbsp;(Collectors.toList(user.getRoles()));您的角色名称包含 user.getRoles().toString() 列表,而不是预期的角色名称;尝试List<String>&nbsp;roleNames&nbsp;=&nbsp;&nbsp;user.getRoles().stream() &nbsp;&nbsp;&nbsp;&nbsp;.map(Role::getName) &nbsp;&nbsp;&nbsp;&nbsp;.collect(Collectors.toList());此外,将瞬态添加到 ROLE_USER 和 ROLE_ADMIN 字段。
随时随地看视频慕课网APP

相关分类

Java
我要回答