【问题标题】:Spring boot security cannot autowired a @RepositorySpring Boot 安全性无法自动装配 @Repository
【发布时间】:2017-10-18 10:33:24
【问题描述】:

当我在 spring boot 中添加安全配置时,我遇到了这个烦人的错误:

2017-05-18 15:23:29.160 WARN 1806 --- [main] ationConfigEmbeddedWebApplicationContext:在上下文初始化期间遇到异常-取消刷新尝试:org.springframework.beans.factory.UnsatisfiedDependencyException:创建名为“securityConfig”的bean时出错:不满意通过字段“userDetailsS​​ervice”表示的依赖关系;嵌套异常是 org.springframework.beans.factory.UnsatisfiedDependencyException:创建名为 'userDetailsS​​erviceImpl' 的 bean 时出错:通过字段 'userRepo' 表达的依赖关系不满足;嵌套异常是 org.springframework.beans.factory.BeanCreationException:创建名称为“userRepository”的 bean 时出错:设置 bean 时无法创建类型为 [org.springframework.orm.jpa.SharedEntityManagerCreator] 的内部 bean '(inner bean)#236c098'属性“实体管理器”;嵌套异常是 org.springframework.beans.factory.BeanCreationException:创建名称为“(内部 bean)#236c098”的 bean 时出错:设置构造函数参数时无法解析对 bean 'entityManagerFactory' 的引用;嵌套异常是 org.springframework.beans.factory.BeanCreationException:创建名称为“entityManagerFactory”的 bean 时出错:FactoryBean 的单例对象的后处理失败;嵌套异常是 org.springframework.beans.factory.UnsatisfiedDependencyException:创建名称为“chatController”的 bean 时出错:通过字段“userService”表示的依赖关系不满足;嵌套异常是 org.springframework.beans.factory.UnsatisfiedDependencyException:创建名为“userServiceImpl”的 bean 时出错:通过字段“userRepo”表示的依赖关系不满足;嵌套异常是 org.springframework.beans.factory.BeanCreationException:创建名为“userRepository”的 bean 时出错:FactoryBean 在创建对象时抛出异常;嵌套异常是 java.lang.NullPointerException

这是 springBootApplication:

public class Ailab4finalApplication {

    public static void main(String[] args) {
        SpringApplication.run(Ailab4finalApplication.class, args);
    }

    @Bean
    public MessageSource messageSource() {
        ResourceBundleMessageSource messageSource = new ResourceBundleMessageSource();
        messageSource.setBasename("validation");
        return messageSource;
    }
}

这是spring boot security的配置:

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

    @Autowired
    private Environment env;

    @Bean(name="passwordEncoder")
    public PasswordEncoder getPasswordEncoder() {
        return new BCryptPasswordEncoder();
    }

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http
                .authorizeRequests()
                    .antMatchers("/webjars/**", "/css/**", "/registration", "/", "/home").permitAll()
                    .anyRequest().authenticated()
                    .and()
                .formLogin()
                    .loginPage("/login")
                    .permitAll()
                    .and()
                .logout()
                    .permitAll();
        http.csrf().disable();
    }

    @Autowired
    public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception {
        auth.userDetailsService(userDetailsService).passwordEncoder(getPasswordEncoder());
    }
}

这个 userRepository 似乎不能被 Spring boot 自动装配:

@Repository
public interface UserRepository extends  CrudRepository<UserHibernate, Long> {
   @Query("select u from UserHibernate u where u.email = ?1")
   public UserHibernate findByEmail(String email);

   @Query("select u from UserHibernate u where u.nickname = ?1")
   public UserHibernate findByNickname(String nickname);

   @Query("select u from UserHibernate u where u.id = ?1")
   public UserHibernate findById(Long id);
}

最后,这是我的项目树:

src:
   -Ailab4finalApplication.java
   -DatabaseConfig.java
   -SecurityConfig.java
src/services:
   -SecurityService.java
   -SecurityServiceImpl.java
   -UserDetailsServiceImpl.java
   -UserService.java
   -UserServiceImpl.java
src/jpa_repositories:
   -SecurityService.java
   -SecurityServiceImpl.java
   -UserDetailsServiceImpl.java
   -UserService.java
   -UserServiceImpl.java

【问题讨论】:

  • 你的UserDetailsServiceImpl 怎么样?是否实现org.springframework.security.core.userdetails.UserDetailsService?
  • 发布完整的堆栈跟踪。
  • @SpringBootApplication?
  • 在此处为您的 UserDetailsS​​ervice 添加您的实现

标签: spring hibernate spring-boot spring-security spring-repositories


【解决方案1】:

我通过自动装配到 Securityconfing 我使用的所有服务和存储库来解决问题。

【讨论】:

    猜你喜欢
    • 2016-10-03
    • 2015-05-27
    • 1970-01-01
    • 1970-01-01
    • 2019-01-10
    • 2018-12-27
    • 2021-04-02
    • 2016-01-06
    • 2019-06-27
    相关资源
    最近更新 更多