0
votes

This is the error that appeared during compilation:

Error creating bean with name 'registrationController': Unsatisfied dependency expressed through field 'passwordEncoder'; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'webSecurityConfig': Unsatisfied dependency expressed through field 'userDetailsService';

I am assuming that in the Registration for private UserRepo userRepo; does not find bin.I have no more ideas what could be wrong.Help me figure out why I get this error.

RegistrationController

@Controller
@RequestMapping("/registration")
public class RegistrationController {

@Autowired
private PasswordEncoder passwordEncoder;

@Autowired
private UserRepo userRepo;

@GetMapping
public String registration(){
    return "registration";
}

@PostMapping
public String processRegistration(RegistrationForm registrationForm){
    userRepo.save(registrationForm.toUser(passwordEncoder));
    return "redirect:/login";
}

}

WebSecurityConfig

@Configuration

@EnableWebSecurity public class WebSecurityConfig extends WebSecurityConfigurerAdapter {

@Autowired
private UserDetailsService userDetailsService;

// шифрование пароля
@Bean
public PasswordEncoder encoder(){
    return new BCryptPasswordEncoder(10);
}

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

@Override
protected void configure(HttpSecurity http) throws Exception {
    http.authorizeRequests()
            .antMatchers("/","/home","login","registration").permitAll()
            .anyRequest().authenticated()
            .and()
            .formLogin()
            .loginPage("/login").defaultSuccessUrl("/").and()
            .logout().logoutUrl("/logout").logoutSuccessUrl("/").permitAll();

}

interface UserRepo

@NoRepositoryBean
public interface UserRepo extends CrudRepository<User,Long> {
  User findByUsername(String name);
}

Log

  org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 
 'registrationController': Unsatisfied dependency expressed through field 'passwordEncoder'; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'webSecurityConfig': Unsatisfied dependency expressed through field 'userDetailsService'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'inMemoryUserDetailsManager' defined in class path resource [org/springframework/boot/autoconfigure/security/servlet/UserDetailsServiceAutoConfiguration.class]: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.springframework.security.provisioning.InMemoryUserDetailsManager]: Factory method 'inMemoryUserDetailsManager' threw exception; nested exception is org.springframework.beans.factory.BeanCurrentlyInCreationException: Error creating bean with name 'encoder': Requested bean is currently in creation: Is there an unresolvable circular reference?
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.resolveFieldValue(AutowiredAnnotationBeanPostProcessor.java:660) ~[spring-beans-5.3.6.jar:5.3.6]
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:640) ~[spring-beans-5.3.6.jar:5.3.6]
at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:119) ~[spring-beans-5.3.6.jar:5.3.6]
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessProperties(AutowiredAnnotationBeanPostProcessor.java:399) ~[spring-beans-5.3.6.jar:5.3.6]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1413) ~[spring-beans-5.3.6.jar:5.3.6]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:601) ~[spring-beans-5.3.6.jar:5.3.6]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:524) ~[spring-beans-5.3.6.jar:5.3.6]
at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:335) ~[spring-beans-5.3.6.jar:5.3.6]
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:234) ~[spring-beans-5.3.6.jar:5.3.6]
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:333) ~[spring-beans-5.3.6.jar:5.3.6]
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:208) ~[spring-beans-5.3.6.jar:5.3.6]
at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:944) ~[spring-beans-5.3.6.jar:5.3.6]
at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:918) ~[spring-context-5.3.6.jar:5.3.6]
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:583) ~[spring-context-5.3.6.jar:5.3.6]
at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.refresh(ServletWebServerApplicationContext.java:144) ~[spring-boot-2.4.5.jar:2.4.5]
at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:782) [spring-boot-2.4.5.jar:2.4.5]
at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:774) [spring-boot-2.4.5.jar:2.4.5]
at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:439) [spring-boot-2.4.5.jar:2.4.5]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:339) [spring-boot-2.4.5.jar:2.4.5]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1340) [spring-boot-2.4.5.jar:2.4.5]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1329) [spring-boot-2.4.5.jar:2.4.5]
at com.security.Registration.RegistrationApplication.main(RegistrationApplication.java:10) [classes/:na]
  Caused by: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean 
  with name 'webSecurityConfig': Unsatisfied dependency expressed through field 'userDetailsService'; 
  nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean 
  with name 'inMemoryUserDetailsManager' defined in class path resource 
  [org/springframework/boot/autoconfigure/security/servlet/UserDetailsServiceAutoConfiguration.class]: 
  Bean instantiation via factory method failed; nested exception is 
  org.springframework.beans.BeanInstantiationException: Failed to instantiate 
  [org.springframework.security.provisioning.InMemoryUserDetailsManager]: Factory method 
  'inMemoryUserDetailsManager' threw exception; nested exception is 
  org.springframework.beans.factory.BeanCurrentlyInCreationException: Error creating bean with name 
   'encoder': Requested bean is currently in creation: Is there an unresolvable circular reference?

DetailsService

public class MyUserDetailsService implements UserDetailsService {

@Autowired
UserRepo userRepository;


@Override
public UserDetails loadUserByUsername(String username) throws 
  UsernameNotFoundException {
    return userRepository.findByUsername(username);
  }
}
1
Last line of the stacktrace says this 'encoder': Requested bean is currently in creation: Is there an unresolvable circular reference which is pretty much self explanatory. There would be circular reference to the dependencies being injected which results in this error.madteapot
I don't quite understand what to do in this case. I need it in the controller to encrypt the password ... What should I do then?Deniss Zadorozhniy
Could you provide UserDetailsService implementation?Singgih Suryo P
Added to the descriptionDeniss Zadorozhniy
Your User class is org.springframework.security.core.userdetails.User or your custom user ?Changemyminds

1 Answers

0
votes

Denis. The last line of the stacktrace says:

    org.springframework.beans.factory.BeanCurrentlyInCreationException: Error creating bean with name 
   'encoder': Requested bean is currently in creation: Is there an unresolvable circular reference?

This means that "encoder" bean is at it's creation stage at the time you are trying to use it inside configure method. Similar problem was already solved in this question. I think you should do the same as the answer to those question suggests - make encoder() method static.