这是编译过程中出现的错误:
创建名为“registrationController”的Bean时出错:未满足通过字段“passwordEncoder”表示的依赖关系;嵌套的异常是一个不满足的依赖关系异常:创建名为“webSecurityConfig”的Bean时出错:通过字段“userDetailsService”表示的依赖关系未满足;
我假设在注册UserRepo时为私有userRepo;找不到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";
}
}
Web安全配置
@Configuration
@启用Web安全公共类Web安全配置扩展Web安全配置适配器{
@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);
}
}
2条答案
按热度按时间rqcrx0a61#
Denis.堆栈跟踪的最后一行说:
这意味着当你试图在
configure
方法中使用“编码器”bean时,它正处于创建阶段。类似的问题已经在this问题中解决了。我认为你应该按照这些问题的答案所建议的那样做--使encoder()
方法成为静态的。62lalag42#
发生此错误的原因是,您在注入
PasswordEncoder
的同一个类中创建它。最好的解决方案是根本不对
PasswordEncoder
(或CurrentUserService
)进行自动布线。看起来,这些示例仅在
configure(AuthenticationManagerBuilder auth)
方法中使用,这是冗余的。将
PasswordEncoder
和UserDetailsService
注册为bean就足以让Spring Security检测到它们并在您的配置中使用它们。因此,只需将
@Autowired
从PasswordEncoder
中删除。