很多人在学习security的时候都应该写过这个类,我想问一下,如果不写死,使用数据库连接的方式该怎么写,参数我看只有一个username,并没有密码,登录的时候是如何比对的?
代码如下
@Service
public class UserDetailServiceImpl implements UserDetailsService {
@Autowired
private PasswordEncoder passwordEncoder;
@Override
public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
//1.根据用户去数据库查询,如果不存在则抛UsernameNotFoundException异常
if (!"admin".equals(username))
throw new UsernameNotFoundException("用户名不存在");
//2.比较密码(注册时已经加密过),如果匹配成功则返回UserDetails
String password = passwordEncoder.encode("123");
System.out.println(password);
System.out.println("进入权限访问");
return new User(username,password, AuthorityUtils.
commaSeparatedStringToAuthorityList("admin,normal,ROLE_abc,/success.html,/insert,/delete"));
}
}