没有可用的“org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder”类型的合格bean

Par*_*tan 5 java spring-security spring-boot

我正在使用 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 findByUsername(String userName);

    }
Run Code Online (Sandbox Code Playgroud)

安全配置:

   @Configuration
   @EnableWebSecurity
    public class SecurityConfiguration extends 
   WebSecurityConfigurerAdapter {

@Autowired
UserPrincipleDetailsService user;

@Autowired
private SimpleAuthenticationSuccessHandler successHandler;

@Override
protected void configure(AuthenticationManagerBuilder auth) throws 
    Exception {

    auth.authenticationProvider(daoAuthenticationProvider());


}

@Override
protected void configure(HttpSecurity http) throws Exception {

    http.authorizeRequests()

        .antMatchers("/assets/css/**").permitAll()
        .antMatchers("/img/**").permitAll()
        .antMatchers("/home").permitAll()
        .antMatchers("/register/**").permitAll()
        .antMatchers("/registerUser").permitAll()
        .antMatchers("/admin/**").hasRole("ADMIN")
        .antMatchers("/user/**").hasAnyRole("ADMIN","USER")
        .anyRequest().authenticated()
        .and()
        .csrf().disable()
        .formLogin()
        .successHandler(successHandler)
        .loginPage("/home").permitAll()
        .loginProcessingUrl("/signin")
        .failureUrl("/home?error=true")


        .and()
        .logout().logoutRequestMatcher(new 
    AntPathRequestMatcher("/logout"))
        .logoutSuccessUrl("/home")
        .and()
        .exceptionHandling().accessDeniedPage("/home");

}

@Bean
DaoAuthenticationProvider daoAuthenticationProvider() {

    DaoAuthenticationProvider dao = new 
    DaoAuthenticationProvider();
    dao.setPasswordEncoder(passwordEncoder());
    dao.setUserDetailsService(user);

    return dao;


}

@Bean
PasswordEncoder passwordEncoder() {

    return new BCryptPasswordEncoder();
}

}
Run Code Online (Sandbox Code Playgroud)

Sha*_*dra 8

改变

@Autowired
BCryptPasswordEncoder crypt;
Run Code Online (Sandbox Code Playgroud)

@Autowired
PasswordEncoder crypt
Run Code Online (Sandbox Code Playgroud)

或更改 passwordEncoder 方法

@Bean
BCryptPasswordEncoder passwordEncoder() {

    return new BCryptPasswordEncoder();
}
Run Code Online (Sandbox Code Playgroud)