【问题标题】:Field passwordEncoder in package required a bean of type 'org.springframework.security.crypto.password.PasswordEncoder' that could not be found包中的字段 passwordEncoder 需要一个找不到的 'org.springframework.security.crypto.password.PasswordEncoder' 类型的 bean
【发布时间】:2021-01-18 18:22:48
【问题描述】:

我正在尝试使用配置文件在我的服务类中自动装配密码编码器:

@Configuration
@EnableWebSecurity
class SecurityConfiguration : WebSecurityConfigurerAdapter() {

@Bean
fun passwordEncoder(): PasswordEncoder {
    return BCryptPasswordEncoder()
}

Intellij 检测到 @Autowired var passwordEncoder 作为 autowired 的候选者。

@Service
class userServiceImpl : UserService {

@Autowired
private lateinit var passwordEncoder : PasswordEncoder

我知道正在调用配置,因为调用了配置文件的其他元素。

***************************
APPLICATION FAILED TO START
***************************
Description:
Field passwordEncoder in com.example.project.service.userServiceImpl required a bean of type 'org.springframework.security.crypto.password.PasswordEncoder' that could not be found.
 The injection point has the following annotations:
- @org.springframework.beans.factory.annotation.Autowired(required=true)
Action:
Consider defining a bean of type 'org.springframework.security.crypto.password.PasswordEncoder' in your configuration.

【问题讨论】:

    标签: spring spring-boot spring-security


    【解决方案1】:

    我在我的主类中为有问题的包添加了组件扫描注释,并消除了错误

        @SpringBootApplication
        @ComponentScan("com.example.project.config")
        class myApplication
    

    【讨论】:

    • 我认为最好只添加父包而不是特定包,以防止代码中可能出现的问题。
    • 由于上面的代码没有显示问题,所以没有人能从一开始就回答这个问题。这不应被视为答案,应删除整个问题,原因是缺乏细节。这表明在询问之前的研究很差
    猜你喜欢
    • 1970-01-01
    • 2019-10-21
    • 1970-01-01
    • 1970-01-01
    • 2020-05-09
    • 2019-12-05
    • 1970-01-01
    • 2018-12-14
    • 1970-01-01
    相关资源
    最近更新 更多