java - 没有可用的 'org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder' 类型的合格 bean

标签 java spring-boot spring-security

我正在使用 BCryptPasswordEncoder 来编码我的密码,但是当我使用它的编码函数在 UserService 类的保存函数中对密码进行编码时,它给了我这个错误:

Error creating bean with name 'userController': Unsatisfied dependency expressed through field 'userServiceInter'; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'userService': Unsatisfied dependency expressed through field 'crypt'; nested exception is 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. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}

当我删除它时,它可以工作,但密码未编码。

UserService 类:

   @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;

}

    }

用户服务接口(interface):

   @Service
    public interface UserServiceInterface {


public void save(User user);

public User findByUsername(String userName);

    }

安全配置:

   @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();
}

}

最佳答案

改变

@Autowired
BCryptPasswordEncoder crypt;

@Autowired
PasswordEncoder crypt

或更改passwordEncoder方法

@Bean
BCryptPasswordEncoder passwordEncoder() {

    return new BCryptPasswordEncoder();
}

关于java - 没有可用的 'org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder' 类型的合格 bean,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58202592/

相关文章:

spring-mvc - 访问 Spring Boot Zuul Service 路由的身份验证

java - 如果在某个固定时间后挂起,则停止 Spring Scheduled 执行

spring - 如何在 spring-security-oauth 中获取经过身份验证的用户

java - 如何在java 1.8中从org.jboss.jca.adapters.jdbc.jdk8.WrappedConnectionJDK8转换为oracle.jdbc.OracleConnection

java - 而不是 JSON 数据链接返回 .txt 文件如何在 android 中获取 JSON 数据

java - 如何在 IE 中打开 wsdl 文件

java - SpringBoot1.5 application.properties 格式错误\uxxxx编码

spring - Spring 安全网址中的双星正则表达式意味着什么

spring-boot - Spring Security 5.7 - 如何返回自定义 UserDetails

java - 如何授权对我的 GAE REST 服务器的请求?