0

The org.springframework.security.crypto.bcrypt.BCryptPasswordEncoderisn't getting injected. Not able to figure out the reason for this. Been through several questions both in and outside of stackoverflow, refining my searchquery everytime, just in the hope of finding a similar question posted by anyone.

Running the project in debug mode says Exception encountered during context initialization and then prints out below :

Application failed to start due to an exception
    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.

Description:

Field encoder in com.codingethics.flightreservation.controller.UserController 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 error is quite obvious in its message, yet confusing because I've already made both the UserController class and the field encoder eligible for autowiring using required annotations. Moreover,here I'm trying to inject a dependency that's Spring provided, not user defined one. I'm not sure if anymore configuration is required. In this same project, I had successfully used JavaMailSender in a similar way.

So what's bothering me is why does this work ? :

@Component
public class EmailUtil {

    @Autowired
    private JavaMailSender javaMailSender;

}

but this doesn't : UserController.java

    @Controller
    public class UserController {   

        @Autowired
        private BCryptPasswordEncoder encoder;

}

Any sort of help/guidance is highly appreciated.

Asif Kamran Malick
  • 2,409
  • 3
  • 25
  • 47
  • The error message is *exquisitely* specific: It understands that you're trying to inject, but *it doesn't have a bean available to do it*. (In this particular case, because password encoding varies, Spring Security made a deliberate decision to have you register your own encoder.) – chrylis -cautiouslyoptimistic- Apr 18 '18 at 22:13
  • @chrylis thanks a ton . that makes sense. but.. //it doesn't have a bean available to do it.// why is it so ,when I have that dependency downloaded in place. //Spring Security made a deliberate decision to have you register your own encoder// is this the intended behavior of Spring Security Framework and is it there in their documentation? just being curious. – Asif Kamran Malick Apr 18 '18 at 22:32

1 Answers1

2

Have you tried to create manually the BCryptPasswordEncoder bean inside a @Configuration class?

@Bean
public PasswordEncoder passwordEncoder() {
    return new BCryptPasswordEncoder();
}

Maybe is worth it trying it.

AntMor
  • 417
  • 6
  • 18
  • Thanks . Yes I did come across similar suggestions and that worked. But that wasn't satisfying, as I should have been able to inject the BCryptPasswordEncoder alone. – Asif Kamran Malick Apr 18 '18 at 22:23
  • @dur Okay. I'll indeed post another question for the why part. That was a part of the original question and is indeed important for me. the documentation too doesn't seem to have an answer for this. Thanks a ton. I appreciate your help. – Asif Kamran Malick Apr 19 '18 at 19:23