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



当我删除它的作品,但密码未编码。

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;

}

    }

UserServiceInterface:
   @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/

10-10 03:19