控制台报Encoded password does not look like BCrypt异常,这是因为Spring Security会把前台传来的字段加密,我们在自定义逻辑时,因为数据库里是明文,所以在查数据库时也应该进行encode加密。
new BCryptPasswordEncoder().encode(user.getPassword())
整体如下所示
@Service("userDetailsService")
@RequiredArgsConstructor
@Slf4j
public class SecurityUserDetailsServiceImpl implements UserDetailsService {
private final UserDao userDao;
@Override
public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
System.out.println(username);
User user = userDao.selectByUsername(username);
if(user==null)
{
throw new UsernameNotFoundException("用户不存在");
}
List<GrantedAuthority> auths =
AuthorityUtils.commaSeparatedStringToAuthorityList("admin,ROLE_sale");
user.setAuthorities(auths);
System.out.println(user);
user.setPassword(new BCryptPasswordEncoder().encode(user.getPassword()));
return user;
}
}