简体   繁体   English

无法修复创建名为“springSecurityFilterChain”的 bean 的错误

[英]Cannot fix the error creating bean with name 'springSecurityFilterChain'

I've search in stackoverflow and many person got this error.我在stackoverflow中搜索过,很多人都遇到了这个错误。 I've tried all solution I got but no one works for me.我已经尝试了所有我得到的解决方案,但没有人适合我。 Below, there is my files and the stacktraces:下面是我的文件和堆栈跟踪:

SecurityConfig.java安全配置.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: 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 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));
    }
}

Stacktrace:堆栈跟踪:

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]

There are many error here, but the main error i got here is the first.这里有很多错误,但我在这里得到的主要错误是第一个。 I've try to @Override the Security class and delete the inheritance WebSecurityConfigurerAdapter bhut it doesn't work.我尝试@Override 安全 class 并删除 inheritance WebSecurityConfigurerAdapter 但它不起作用。

The first thing that is a bit suspicious is that the UserDetailsService is not autowired, therefore首先有点可疑的是 UserDetailsService 没有自动装配,因此

public class SecurityConfig extends WebSecurityConfigurerAdapter {
   @Autowired
   private UserDetailsService userDetailsService;

Can help you.能帮你。 The second thing that I find you can get rid off is the definition of the AuthenticationManager Bean, you are already using a the AuthenticationManagerBuilder.我发现您可以摆脱的第二件事是 AuthenticationManager Bean 的定义,您已经在使用 AuthenticationManagerBuilder。 Try to remove also this code尝试删除此代码

@Bean(BeanIds.AUTHENTICATION_MANAGER)
@Override
public AuthenticationManager authenticationManagerBean() throws Exception {
    return super.authenticationManagerBean();
}

And last but not least, your class UserDetailService should not implement the UserDetail Interface, they are two different concepts, the Service fetches Users, therefore implementing methods like isEnabled() or isAccountNonExpired() are meaningless.最后但同样重要的是,您的 class UserDetailService 不应该实现 UserDetail 接口,它们是两个不同的概念,服务获取用户,因此实现 isEnabled() 或 isAccountNonExpired() 之类的方法是没有意义的。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 创建名为“springSecurityFilterChain”的 bean 时出错 - Error creating bean with name 'springSecurityFilterChain' Spring 安全性:创建名为 springsecurityfilterchain 的 bean 时出错 - Spring Security: error creating bean with name springsecurityfilterchain 创建在WebSecurityConfiguration中定义的名称为&#39;springSecurityFilterChain&#39;的bean时出错 - Error creating bean with name 'springSecurityFilterChain' defined in WebSecurityConfiguration Spring Boot:创建名为“springSecurityFilterChain”的 bean 时出错 - Spring Boot: Error creating bean with name 'springSecurityFilterChain' 创建名为“springSecurityFilterChain”的 bean 在类路径资源 3 中定义时出错 - Error creating bean with name 'springSecurityFilterChain' defined in class path resource 3 Spring MVC 安全性:创建名为“springSecurityFilterChain”的 bean 时出错 - Spring MVC Security: Error creating bean with name 'springSecurityFilterChain' OAUTH2 spring 创建名为“springSecurityFilterChain”的bean时出错 - OAUTH2 spring Error creating bean with name 'springSecurityFilterChain' 在实现 spring 安全性时,在类路径资源中创建名为“springSecurityFilterChain”的 bean 时出错 - Error creating bean with name 'springSecurityFilterChain' defined in class path resource while implementing spring security 无法在Netbeans中使用gradle传递JVM args。 创建名称为“ springSecurityFilterChain”的bean时出错 - unable to pass JVM args using gradle in Netbeans. Error creating bean with name 'springSecurityFilterChain' 如何解决“使用名称创建bean时出错” - How to fix “Error creating bean with name”
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM