encoded password does not look like bcrypt
在做登陆时,有时会遇到这个问题,是因为从数据查出来的password不是加密的,而前台传过来的是加密的,所以匹配不上,因此,我们需要把数据库查出来的进行加密
@Service
public class CustomUserServiceImpl implements UserDetailsService {
@Resource
UserMapper UserMapper;
@Override
public UserDetails loadUserByUsername(String s) throws UsernameNotFoundException {
User user = UserMapper.loadUserByUsername(s);
if (user.getName() == null){ //如果用户名不存在,就抛出下面的信息
throw new UsernameNotFoundException("userName" + s + "Not found in the database");
}
//用户存在的话就返回hr对象
//解决spring security密码加密问题需要先转化
// System.out.println(user);
//进行加密!
String a=new BCryptPasswordEncoder().encode(user.getPassword());
user.setPassword(a);
return user;
}
}