org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder
没有被注入。无法弄清楚其中的原因。在stackoverflow内外都经历了几个问题,每次都完善我的搜索查询,只是希望找到任何人发布的类似问题。
在调试模式下运行项目时显示上下文初始化期间遇到异常,然后打印如下:
Application failed to start due to an exception
org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder' available: expected at least 1 bean which qualifies as autowire candidate.
Description:
Field encoder in com.codingethics.flightreservation.controller.UserController required a bean of type 'org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder' that could not be found.
Action:
Consider defining a bean of type 'org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder' in your configuration.
该错误在其消息中非常明显,但令人困惑,因为我已经使UserController类和字段编码器都有资格使用所需的注释进行自动接线。 此外,在这里我试图注入一个 Spring 提供的依赖项,而不是用户定义的依赖项。我不确定是否需要再进行配置。在同一个项目中,我以类似的方式成功地使用了JavaMailSender。
所以困扰我的是为什么这有效?
@Component
public class EmailUtil {
@Autowired
private JavaMailSender javaMailSender;
}
但这不是:用户控制器.java
@Controller
public class UserController {
@Autowired
private BCryptPasswordEncoder encoder;
}
非常感谢任何形式的帮助/指导。
您是否尝试过在@Configuration类中手动创建BCryptPasswordEncoderbean?
@Bean
public PasswordEncoder passwordEncoder() {
return new BCryptPasswordEncoder();
}
也许值得一试。