Spring Boot注入PasswordEncoder失败

文章讲述了在使用SpringBoot时,尝试通过@Autowired自动装配PasswordEncoder进行登录密码校验时遇到的错误,以及在配置类和启动类中提供PasswordEncoder两种解决方案。
摘要由CSDN通过智能技术生成

问题

以@Autowired方式注入PasswordEncoder对登录密码进行校验,启动时报错如下

Description:

Field userService in com.lyx.springboot.controller.UserController 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.

 在配置类里已经以@Bean形式声明了PasswordEncoder,但是不生效。

@AutoConfiguration
@EnableConfigurationProperties(SecurityProperties.class)
public class SecurityConfiguration {

    @Resource
    private SecurityProperties securityProperties;

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

解决方法 

第一种方法:在启动类里以@Bean声明

@SpringBootApplication(scanBasePackages = "com.lyx.springboot.*")
public class SpringbootApplication {


    public static void main(String[] args) {
        SpringApplication.run(SpringbootApplication.class, args);
    }

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

第二种方法:在应用的代码处创建实例实现PasswordEncoder接口,如我用到的是BCryptPassword

boolean result =  new BCryptPasswordEncoder().matches(userDTO.getPassword(), userLogin.getPassword());

  • 3
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
Spring Boot项目中,可以通过添加Spring Security依赖来使用PasswordEncoder。可以按照以下步骤来导入: 1. 在pom.xml文件中添加以下依赖: ```xml <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-security</artifactId> </dependency> ``` 2. 创建一个实现了WebSecurityConfigurerAdapter的配置类,并覆盖configure()方法。在该方法中,可以使用PasswordEncoder来配置AuthenticationManagerBuilder: ```java @Configuration @EnableWebSecurity public class SecurityConfig extends WebSecurityConfigurerAdapter { @Autowired private DataSource dataSource; @Override protected void configure(AuthenticationManagerBuilder auth) throws Exception { auth.jdbcAuthentication().dataSource(dataSource) .passwordEncoder(passwordEncoder()); } @Bean public PasswordEncoder passwordEncoder() { return new BCryptPasswordEncoder(); } } ``` 在上面的配置类中,使用了BCryptPasswordEncoder来加密密码。可以根据需要选择其他的PasswordEncoder实现类。 3. 在应用程序中使用PasswordEncoder来加密密码。例如,可以使用以下代码来创建一个新用户: ```java @Autowired private UserService userService; @Autowired private PasswordEncoder passwordEncoder; public void createNewUser(String username, String password) { String encodedPassword = passwordEncoder.encode(password); User user = new User(username, encodedPassword); userService.save(user); } ``` 在上面的代码中,使用了@Autowired注解来注入PasswordEncoder和UserService。createNewUser()方法首先使用PasswordEncoder来加密密码,然后创建一个新的User对象,并将其保存到数据库中。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

LemonSmile_

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值