为了账号安全,请及时绑定邮箱和手机立即绑定

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

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

鸿蒙传说 2022-05-25 09:50:00
我在String List从对象Collection中的角色中获取用户角色时遇到问题User。我想获取用户角色以在我的 on方法中List实现它,在该方法中,我将角色名称作为列表传递给类以使社交登录可用。将不胜感激的解决方案。UserServiceImlementationloadUserByUsernameSocialUserDetailsImplementation用户服务实现:@Servicepublic 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);    }}@Overridepublic String getUserId() {    return this.user.getUserId() + "";}@Overridepublic String getUsername() {    return user.getUserName();}@Overridepublic Collection<? extends GrantedAuthority> getAuthorities() {    return list;}@Overridepublic String getPassword() {    return user.getPassword();}@Overridepublic boolean isAccountNonExpired() {    return true;}@Overridepublic boolean isAccountNonLocked() {    return true;}
查看完整描述

3 回答

?
慕的地10843

TA贡献1785条经验 获得超8个赞

做这样的事情,


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

List<String> roleNamesString= new List<string> ();


for(Object a: roleNames){

   roleNameString.add(String.valueOf(a));

}

您可能需要覆盖 Role 类的 toString 方法,具体取决于模型的复杂性。


查看完整回答
反对 回复 2022-05-25
?
胡说叔叔

TA贡献1804条经验 获得超8个赞

基本上,您不需要将角色转换为List<String>您可以List<GrantedAuthority>通过以下方式构建的角色


user.getRoles().stream()

    .map(role -> new SimpleGrantedAuthority(role.getRoleName()))

    .collect(Collectors.toList());

如果你真的需要,List<String>那么你可以做到


user.getRoles().stream()

    .map(Role::getRoleName)

    .collect(Collectors.toList());


查看完整回答
反对 回复 2022-05-25
?
慕后森

TA贡献1802条经验 获得超5个赞

在这部分

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

您的角色名称包含 user.getRoles().toString() 列表,而不是预期的角色名称;

尝试

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

此外,将瞬态添加到 ROLE_USER 和 ROLE_ADMIN 字段。


查看完整回答
反对 回复 2022-05-25
  • 3 回答
  • 0 关注
  • 902 浏览

添加回答

举报

0/150
提交
取消
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号