当前位置: 首页 > 知识库问答 >
问题:

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

万俟承望
2023-03-14
   @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;

}

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

}

共有1个答案

佴飞驰
2023-03-14

改变

@Autowired
BCryptPasswordEncoder crypt;

@Autowired
PasswordEncoder crypt

或更改passwordEncoder方法

@Bean
BCryptPasswordEncoder passwordEncoder() {

    return new BCryptPasswordEncoder();
}
 类似资料: