无法修复创建名为“springSecurityFilterChain”的 bean 的错误
Posted
技术标签:
【中文标题】无法修复创建名为“springSecurityFilterChain”的 bean 的错误【英文标题】:Cannot fix the error creating bean with name 'springSecurityFilterChain' 【发布时间】:2021-04-14 15:09:30 【问题描述】:我在 *** 中搜索过很多人都遇到了这个错误。我已经尝试了所有我得到的解决方案,但没有人适合我。 下面是我的文件和堆栈跟踪:
SecurityConfig.java
//Class contenant la configuration de la sécurité du projet
@EnableWebSecurity
@Configuration
public class SecurityConfig extends WebSecurityConfigurerAdapter
private UserDetailsService userDetailsService;
@Bean(BeanIds.AUTHENTICATION_MANAGER)
@Override
public AuthenticationManager authenticationManagerBean() throws Exception
return super.authenticationManagerBean();
@Override
public void configure(HttpSecurity httpSecurity) throws Exception
httpSecurity.csrf().disable()
.authorizeRequests()
.antMatchers("/api/v1/auth/**")
.permitAll()
.anyRequest()
.authenticated();
@Autowired
public void configureGlobal(AuthenticationManagerBuilder authenticationManagerBuilder) throws Exception
authenticationManagerBuilder.userDetailsService(userDetailsService)
.passwordEncoder(passwordEncoder());
@Bean
PasswordEncoder passwordEncoder()
return new BCryptPasswordEncoder();
UserDetailsService.java:
@Data
@Builder
public class UserDetailsService implements org.springframework.security.core.userdetails.UserDetails
private static final long serialVersionUID = 1L;
Set<GrantedAuthority> authorities = null;
private User user;
public User getUser()
return user;
public void setUser(User user)
this.user = user;
public Collection<? extends GrantedAuthority> getAuthorities()
return authorities;
public void setAuthorities(Set<GrantedAuthority> authorities)
this.authorities = authorities;
public String getPassword()
return user.getPassword();
public String getUsername()
return user.getUsername();
public boolean isAccountNonExpired()
return false;
public boolean isAccountNonLocked()
return false;
public boolean isCredentialsNonExpired()
return false;
public boolean isEnabled()
return true;
UserDetailsServiceImpl.java
@Service
@AllArgsConstructor
public class UserDetailsServiceImpl implements UserDetailsService
private final UserRepository userRepository;
@Override
@Transactional(readOnly = true)
public UserDetails loadUserByUsername(String username)
Optional<User> userOptional = userRepository.findByUsername(username);
User user = userOptional
.orElseThrow(() -> new UsernameNotFoundException("No user " +
"Found with username : " + username));
return new org.springframework.security
.core.userdetails.User(user.getUsername(), user.getPassword(),
user.isEnabled(), true, true,
true, getAuthorities("USER"));
private Collection<? extends GrantedAuthority> getAuthorities(String role)
return singletonList(new SimpleGrantedAuthority(role));
堆栈跟踪:
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'springSecurityFilterChain' defined in class path resource [org/springframework/security/config/annotation/web/configuration/WebSecurityConfiguration.class]: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [javax.servlet.Filter]: Factory method 'springSecurityFilterChain' threw exception; nested exception is java.lang.RuntimeException: Could not postProcess org.springframework.security.authentication.dao.DaoAuthenticationProvider@5c3924fd of type class org.springframework.security.authentication.dao.DaoAuthenticationProvider
at org.springframework.beans.factory.support.ConstructorResolver.instantiate(ConstructorResolver.java:658) ~[spring-beans-5.3.2.jar:5.3.2]
at org.springframework.beans.factory.support.ConstructorResolver.instantiateUsingFactoryMethod(ConstructorResolver.java:486) ~[spring-beans-5.3.2.jar:5.3.2]
Caused by: org.springframework.beans.BeanInstantiationException: Failed to instantiate [javax.servlet.Filter]: Factory method 'springSecurityFilterChain' threw exception; nested exception is java.lang.RuntimeException: Could not postProcess org.springframework.security.authentication.dao.DaoAuthenticationProvider@5c3924fd of type class org.springframework.security.authentication.dao.DaoAuthenticationProvider
at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:185) ~[spring-beans-5.3.2.jar:5.3.2]
at org.springframework.beans.factory.support.ConstructorResolver.instantiate(ConstructorResolver.java:653) ~[spring-beans-5.3.2.jar:5.3.2]
Caused by: java.lang.RuntimeException: Could not postProcess org.springframework.security.authentication.dao.DaoAuthenticationProvider@7d7a8d23 of type class org.springframework.security.authentication.dao.DaoAuthenticationProvider
at org.springframework.security.config.annotation.configuration.AutowireBeanFactoryObjectPostProcessor.postProcess(AutowireBeanFactoryObjectPostProcessor.java:69) ~[spring-security-config-5.4.2.jar:5.4.2]
at org.springframework.security.config.annotation.SecurityConfigurerAdapter$CompositeObjectPostProcessor.postProcess(SecurityConfigurerAdapter.java:118) ~[spring-security-config-5.4.2.jar:5.4.2]
at org.springframework.security.config.annotation.SecurityConfigurerAdapter.postProcess(SecurityConfigurerAdapter.java:79) ~[spring-security-config-5.4.2.jar:5.4.2]
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'org.springframework.security.authentication.dao.DaoAuthenticationProvider@7d7a8d23': Invocation of init method failed; nested exception is java.lang.IllegalArgumentException: A UserDetailsService must be set
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1788) ~[spring-beans-5.3.2.jar:5.3.2]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:420) ~[spring-beans-5.3.2.jar:5.3.2]
at org.springframework.security.config.annotation.configuration.AutowireBeanFactoryObjectPostProcessor.postProcess(AutowireBeanFactoryObjectPostProcessor.java:65) ~[spring-security-config-5.4.2.jar:5.4.2]
... 45 common frames omitted
Caused by: java.lang.IllegalArgumentException: A UserDetailsService must be set
at org.springframework.util.Assert.notNull(Assert.java:201) ~[spring-core-5.3.2.jar:5.3.2]
at org.springframework.security.authentication.dao.DaoAuthenticationProvider.doAfterPropertiesSet(DaoAuthenticationProvider.java:85) ~[spring-security-core-5.4.2.jar:5.4.2]
at org.springframework.security.authentication.dao.AbstractUserDetailsAuthenticationProvider.afterPropertiesSet(AbstractUserDetailsAuthenticationProvider.java:119) ~[spring-security-core-5.4.2.jar:5.4.2]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1847) ~[spring-beans-5.3.2.jar:5.3.2]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1784) ~[spring-beans-5.3.2.jar:5.3.2]
这里有很多错误,但我在这里得到的主要错误是第一个。我尝试 @Override 安全类并删除继承 WebSecurityConfigurerAdapter 但它不起作用。
【问题讨论】:
【参考方案1】:首先有点可疑的是 UserDetailsService 没有自动装配,因此
public class SecurityConfig extends WebSecurityConfigurerAdapter
@Autowired
private UserDetailsService userDetailsService;
可以帮助你。我发现您可以摆脱的第二件事是 AuthenticationManager Bean 的定义,您已经在使用 AuthenticationManagerBuilder。尝试删除此代码
@Bean(BeanIds.AUTHENTICATION_MANAGER)
@Override
public AuthenticationManager authenticationManagerBean() throws Exception
return super.authenticationManagerBean();
最后但同样重要的是,你的类 UserDetailService 不应该实现 UserDetail 接口,它们是两个不同的概念,Service 获取用户,因此实现 isEnabled() 或 isAccountNonExpired() 之类的方法是没有意义的。
【讨论】:
目前,您回答的一部分帮助我解决了这个问题。当我尝试添加注释@Autowired
时,出现错误。我正在使用@SpringBootApplication
注释。这个注解代表@Configuration
、@EnableAutoConfiguration
和@ComponentScan
根据spring参考。一旦我改回分别使用@Configuration
、@EnableAutoConfiguration
和@ComponentScan
,错误就停止了。
在这里我得到了解决方案 og @Autowired
问题:***.com/questions/26889970/…以上是关于无法修复创建名为“springSecurityFilterChain”的 bean 的错误的主要内容,如果未能解决你的问题,请参考以下文章