首页 文章

BCryptPasswordEncoder不作为spring-boot-starter-security的一部分导入

提问于
浏览
1

找不到bean BCryptPasswordEncoder虽然我通过maven依赖和spring-boot-starter-security导入它

我尝试使用BCryptPasswordEncoder进行密码散列,但是当我启动项目时出现错误:

com.websystique.springboot.controller.RestApiController中构造函数的参数0需要一个无法找到的类型为'org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder'的bean .

我正在使用spring-boot,我只是在我的控制器中使用它,如:

@Autowired BCryptPasswordEncoder bCryptPasswordEncoder;

1 回答

  • 1

    @Configuration 类中添加BcryptPasswordEncoder bean . 它不会自动声明 .

    @Bean
    public PasswordEncoder passwordEncoder() {
        return new BCryptPasswordEncoder();
    }
    

    (可选)并像这样注入 .

    @Autowired
    private PasswordEncoder passwordEncoder;
    

相关问题