创建名为"注册控制器"的 Bean 时出错:通过字段"密码编码器"表示的不满足依赖关系;



这是编译过程中出现的错误:

创建名为"注册控制器"的 Bean 时出错:通过字段"passwordEncoder"表示的未满足的依赖项; 嵌套异常是 org.springframework.beans.factory.UnsatisfiedDependencyException:创建名称为"webSecurityConfig"的 Bean 时出错:通过字段"userDetailsService"表示的不满意依赖项;

我假设在私人用户存储库的注册中; 找不到 bin。我再也不知道可能出了什么问题。帮助我找出为什么会出现此错误。

注册控制器

@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";
}
}

网络安全配置

@Configuration

@EnableWebSecurity public class WebSecurityConfig extensions 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();
}

接口用户存储库

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

日志

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?

详情服务

public class MyUserDetailsService implements UserDetailsService {
@Autowired
UserRepo userRepository;

@Override
public UserDetails loadUserByUsername(String username) throws 
UsernameNotFoundException {
return userRepository.findByUsername(username);
}
}

Denis。 堆栈跟踪的最后一行显示:

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

这意味着"编码器"bean 在您尝试在方法中使用它时处于创建阶段configure。类似的问题已经在这个问题中解决了。我认为您应该按照这些问题的答案做同样的事情 - 使encoder()方法静态。

发生此错误是因为您在注入它的同一类中创建PasswordEncoder

最好的解决方案是根本不自动连接PasswordEncoder(或CurrentUserService)。

看起来,这些实例仅用于configure(AuthenticationManagerBuilder auth)方法,这是多余的。

PasswordEncoderUserDetailsService注册为 bean 就足以让 Spring Security 检测到它们并在您的配置中使用它们。

因此,只需从PasswordEncoder中删除@Autowired即可。

相关内容

  • 没有找到相关文章

最新更新