@Service public class MyUserDetailService implements UserDetailsService { @Autowired private UserInfoDao dao; @Override public UserDetails loadUserByUsername(String s) throws UsernameNotFoundException { User user = null; UserInfo userInfo = null; if (s != null){ userInfo = dao.findByUserName(s); if( userInfo != null) { ArrayList<GrantedAuthority> list = new ArrayList<>(); //角色必须以ROLE_开头 list.add(new SimpleGrantedAuthority("ROLE_" + userInfo.getRole())); //创建user对象 user = new User(userInfo.getUserName(),userInfo.getPassword(),list); }else{ throw new UsernameNotFoundException("错误"); } }else{ throw new UsernameNotFoundException("错误"); } return user; } }