严重:例外,将上下文初始事件发送给听众实例

发布于 2025-02-11 22:33:24 字数 4916 浏览 0 评论 0原文

我尝试在运行时使用JDBC进行弹簧安全登录 我的SecurityConfig中的例外

严重:例外将上下文初始事件发送给听众 类的实例 org.springframework.beans.factory.beancreationexception:错误创建使用名称'securityConfig'的BEAN:自动注入 依赖性失败;嵌套异常是 org.springframework.beans.factory.beancreationexception:不能 Autowire Field:私人 org.springframework.security.core.userdetails.userdetailsservice com.changepond.config.securityconfig.userdetailsservice;嵌套 例外是 org.springframework.beans.factory.nosuchbeandefinitionException:否 合格的豆类 [org.springframework.security.core.userdetails.userdetailsservice] 发现依赖性:预期至少有1个豆类有资格 此依赖性的自动候选人。依赖性注释: {@org.springframework.beans.factory.annotation.autowired(必需= true)} 在 org.springframework.beans.factory.annotation.autowiredannotationbeanbeanpostprocessor.postprocesspropertyvalues(autowiredannotationbeantationbeanpostprocessor.java:292) 在 org.springframework.beans.factory.support.abstractautowirecapablebeanfactory.populatebean(Abstractautowirecapablebeanfactory.java:1185) 在 org.springframework.beans.factory.support.abstractautowirecapablebeanfactory.docreatebean(Abstractautowirecapablebeanfactory.java:537) 在 org.springframework.beans.factory.support.abstractautowirecapablebeanfactory.createbean(Abstractautowirecapablebeanfactory.java:475) Caused by: org.springframework.beans.factory.BeanCreationException: Could not autowire field: private org.springframework.security.core.userdetails.UserDetailsS​​ervice com.changepond.config.SecurityConfig.userDetailsS​​ervice;嵌套的例外是org.springframework.beans.factory.nosuchbeandefinitionException:没有合格的豆类[org.springframework.security.core.core.core.core.userdetails.userdetailsservice],以依赖于依赖的bean,至少可以自动鉴定自动化的1 bean。依赖项注释:{@org.springframework.beans.factory.annotation.autowired(quilt = true)} at org.springframework.beans.factory.annotation.autowiredannotationbeanpostprocessor $ autowiredfieldelement.ignd(autowiredannotationbeantationbeantationbeantationbeantationbeanpostprocessor.java:508) 由:org.springframework.beans.factory.nosuchbeandefinitionException:无资格的类型[org.springframework.security.core.core.core.core.userdetails.userdetailsserservice],至少可以依靠1 bean,这是自动化对于这种依赖性。依赖项注释:{@org.springframework.beans.factory.annotation.autowired(quilt = true)} atorg.springframework.beans.factory.support.defaultlistablebeanfactory.raisenosuchbeandefinitionException(DefaultListableBeanFactory.java:1100)

SecurityConfig类

@Configuration
    @EnableWebSecurity
    @ComponentScan({ "com.xxxxx" })
    public class SecurityConfig extends WebSecurityConfigurerAdapter {
            
        private final UserDetailsService userDetailsService;
        
        @Autowired
        public SecurityConfig(UserDetailsService userDetailsService) {
            this.userDetailsService = userDetailsService;
        }
    
        @Bean
        public BCryptPasswordEncoder bCryptPasswordEncoder()
        {
            return new BCryptPasswordEncoder();
            }
    
        @Override
        protected void configure(HttpSecurity http) throws Exception {
            http.csrf().disable();
            http.authorizeRequests()
             .antMatchers("/loginPage")
             .permitAll()
                .and()
                    .formLogin().loginPage("/loginPage")
                    .defaultSuccessUrl("/homepage")
                    .failureUrl("/loginPage?error")
                    .usernameParameter("username").passwordParameter("password")                
                .and()
                    .logout().logoutSuccessUrl("/loginPage?logout"); 
                
        }
        
        
        @Autowired
        protected void configureGlobal(AuthenticationManagerBuilder auth) throws Exception {
              auth.userDetailsService(userDetailsService)
                  .passwordEncoder(new BCryptPasswordEncoder());
            }
        
    }

LoginService类

@Transactional
@Service
public class LoginService {
    
    private final LoginRepo loginRepo;
        
    @Autowired
    public LoginService(LoginRepo loginRepo) {
        this.loginRepo = loginRepo;
    }

    public Login findByUsername(String username) {
        return loginRepo.findByUsername(username);
    }
}

logIndetailsserviceimpl类

@Transactional  
public class LoginDetailsServiceImpl implements UserDetailsService{

    private final LoginRepo loginRepo;
    
    @Autowired
    public LoginDetailsServiceImpl(LoginRepo loginRepo) {
        this.loginRepo = loginRepo;
    }

    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        Login login = loginRepo.findByUsername(username);
    return new org.springframework.security.core.userdetails.User(login.getUsername(), login.getPassword(), null);
}
}

I try to do Spring Security Login using JDBC when i run i got this
exception in my SecurityConfig

SEVERE: Exception sending context initialized event to listener
instance of class
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'securityConfig': Injection of autowired
dependencies failed; nested exception is

org.springframework.beans.factory.BeanCreationException: Could not
autowire field: private
org.springframework.security.core.userdetails.UserDetailsService
com.changepond.config.SecurityConfig.userDetailsService; nested
exception is
org.springframework.beans.factory.NoSuchBeanDefinitionException: No
qualifying bean of type
[org.springframework.security.core.userdetails.UserDetailsService]
found for dependency: expected at least 1 bean which qualifies as
autowire candidate for this dependency. Dependency annotations:
{@org.springframework.beans.factory.annotation.Autowired(required=true)}
at
org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:292)
at
org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1185)
at
org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:537)
at
org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:475)
Caused by: org.springframework.beans.factory.BeanCreationException: Could not autowire field: private org.springframework.security.core.userdetails.UserDetailsService com.changepond.config.SecurityConfig.userDetailsService; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [org.springframework.security.core.userdetails.UserDetailsService] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:508)
Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [org.springframework.security.core.userdetails.UserDetailsService] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
at org.springframework.beans.factory.support.DefaultListableBeanFactory.raiseNoSuchBeanDefinitionException(DefaultListableBeanFactory.java:1100)

SecurityConfig class

@Configuration
    @EnableWebSecurity
    @ComponentScan({ "com.xxxxx" })
    public class SecurityConfig extends WebSecurityConfigurerAdapter {
            
        private final UserDetailsService userDetailsService;
        
        @Autowired
        public SecurityConfig(UserDetailsService userDetailsService) {
            this.userDetailsService = userDetailsService;
        }
    
        @Bean
        public BCryptPasswordEncoder bCryptPasswordEncoder()
        {
            return new BCryptPasswordEncoder();
            }
    
        @Override
        protected void configure(HttpSecurity http) throws Exception {
            http.csrf().disable();
            http.authorizeRequests()
             .antMatchers("/loginPage")
             .permitAll()
                .and()
                    .formLogin().loginPage("/loginPage")
                    .defaultSuccessUrl("/homepage")
                    .failureUrl("/loginPage?error")
                    .usernameParameter("username").passwordParameter("password")                
                .and()
                    .logout().logoutSuccessUrl("/loginPage?logout"); 
                
        }
        
        
        @Autowired
        protected void configureGlobal(AuthenticationManagerBuilder auth) throws Exception {
              auth.userDetailsService(userDetailsService)
                  .passwordEncoder(new BCryptPasswordEncoder());
            }
        
    }

LoginService class

@Transactional
@Service
public class LoginService {
    
    private final LoginRepo loginRepo;
        
    @Autowired
    public LoginService(LoginRepo loginRepo) {
        this.loginRepo = loginRepo;
    }

    public Login findByUsername(String username) {
        return loginRepo.findByUsername(username);
    }
}

LoginDetailsServiceImpl class

@Transactional  
public class LoginDetailsServiceImpl implements UserDetailsService{

    private final LoginRepo loginRepo;
    
    @Autowired
    public LoginDetailsServiceImpl(LoginRepo loginRepo) {
        this.loginRepo = loginRepo;
    }

    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        Login login = loginRepo.findByUsername(username);
    return new org.springframework.security.core.userdetails.User(login.getUsername(), login.getPassword(), null);
}
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文