我正在使用 BCryptPasswordEncoder 对我的密码进行编码,但是当我使用它的 encode 函数在 UserService 类的保存函数中对密码进行编码时,它给了我这个错误:
创建名为“userController”的 bean 时出错:通过字段“userServiceInter”表达的不满足的依赖关系;嵌套异常是 org.springframework.beans.factory.UnsatisfiedDependencyException:创建名为 'userService' 的 bean 时出错:通过字段 'crypt' 表示不满足的依赖关系;嵌套异常是 org.springframework.beans.factory.NoSuchBeanDefinitionException:没有可用的“org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder”类型的合格 bean:预计至少有 1 个 bean 有资格作为自动装配候选。依赖注解:{@org.springframework.beans.factory.annotation.Autowired(required=true)}
当我删除它时,它可以工作,但密码没有编码。
用户服务类:
@Service
public class UserService implements UserServiceInterface{
@Autowired
UserRepository repo;
@Autowired
BCryptPasswordEncoder crypt;
@Autowired
RoleRepository roleRepo;
public void save(User user) {
user.setPassword(crypt.encode(user.getPassword()));
Role role = roleRepo.findByRole("USER");
user.setRoles(new HashSet<Role>(Arrays.asList(role)));
repo.save(user);
}
@Override
public User findByUsername(String userName) {
User user = repo.findByUserName(userName);
return user;
}
}
Run Code Online (Sandbox Code Playgroud)
用户服务接口:
@Service
public interface UserServiceInterface {
public void save(User user);
public User …Run Code Online (Sandbox Code Playgroud)