第一招 ——Login Failed: A granted authority textual representation is required
在使用Spring Security配置用户登录时,在测试页面登录时出现错误提示"Login Failed: A granted authority textual representation is required"
“ authorities.add(new SimpleGrantedAuthority(roles.getRoleName()));” 程序走到这里就报错了。
public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
Collection<GrantedAuthority> authorities = new ArrayList<>();
//从数据库中读取用户信息
Users users = usersService.selectByName(username);
//判断用户是否存在
if (users==null){
throw new UsernameNotFoundException("用户不存在");
}
//添加权限
List<UsersRoles> usersRoles = usersRolesService.listByUserId(users.getUserId());
for (UsersRoles usersRole : usersRoles) {
Roles roles = rolesService.selectById(usersRole.getRoleId());
authorities.add(new SimpleGrantedAuthority(roles.getRoleName()));
}
//返回UserDetails实体类
return new User(users.getUsername(),users.getUserPassword(),authorities);
}
解决方案:
原因是因为 roles.getRoleName() 没有值 因为我的数据库role-name原因 改成role_name就好了 我都醉了;