Home > database >  SecurityConfiguration required bean of type BCryptPasswordEncoder
SecurityConfiguration required bean of type BCryptPasswordEncoder

Time:06-03

I tried looking at a couple similar questions however, seems they use different methods/class structures

I'm also using Lombok (not sure if this is an issue)

Here is the error:

Parameter 1 of constructor in com.example.javaspringboot.Security.SecurityConfiguration required a bean of type 'org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder' that could not be found.

Action:

Consider defining a bean of type 'org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder' in your configuration.

The class it references is the following:

@Configuration @EnableWebSecurity @RequiredArgsConstructor
public class SecurityConfiguration extends WebSecurityConfigurerAdapter {

    private final UserDetailsService uds; // VERIFY LOGIN ATTEMPTS
    private final BCryptPasswordEncoder bcpe; // ENCODING PASSWORDS

    @Override
    protected void configure(AuthenticationManagerBuilder auth) throws Exception {
        auth.userDetailsService(uds).passwordEncoder(bcpe);
    }

    @Override
    protected void configure(HttpSecurity http) throws Exception {
//        super.configure(http); //USES COOKIES LOOK MORE INTO THIS
        http.csrf().disable();
        http.sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS);
        http.authorizeRequests().anyRequest().permitAll();
        http.addFilter(new CustomAuthenticationFilter(authenticationManagerBean()));
    }

    @Bean
    @Override
    public AuthenticationManager authenticationManagerBean() throws Exception{ // NO IDEA 
        return super.authenticationManagerBean();
    }

}

The only other class it is being used is in the main application class, as shown:


@SpringBootApplication
public class JavaSpringBootApplication {
    // this runs on init
    public static void main(String[] args) {
        SpringApplication.run(JavaSpringBootApplication.class, args);
    }

    @Bean
    PasswordEncoder passwordEncoder(){ // NEEDED TO ALLOW PASSWORD ENCODER INSIDE SECURITY
        return new BCryptPasswordEncoder();
    }
        };
    }
}

It's a little confusing considering the class it specifies already has @Bean outlined.

Any suggestions are appreciated.

CodePudding user response:

In your main class, the return type is wrong (or you need to change the class in your security config).

public class SecurityConfiguration extends WebSecurityConfigurerAdapter {
    ...
    private final BCryptPasswordEncoder bcpe;
    ...
}

Requires BCryptPasswordEncoder. However

public class JavaSpringBootApplication {
    @Bean
    PasswordEncoder passwordEncoder(){ // NEEDED TO ALLOW PASSWORD ENCODER INSIDE SECURITY
        return new BCryptPasswordEncoder();
    }
}

return PasswordEncoder.

So, either change return type of your bean to BCryptPasswordEncoder or autowire PasswordEncoder in your config.

  • Related