当我将安全配置添加到spring boot时,我遇到了这个恼人的错误:
2017-05-18 15:23:29.160 WARN 1806 --- [ main] ationConfigEmbeddedWebApplicationContext : Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'securityConfig': Unsatisfied dependency expressed through field 'userDetailsService'; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'userDetailsServiceImpl': Unsatisfied dependency expressed through field 'userRepo'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'userRepository': Cannot create inner bean '(inner bean)#236c098' of type [org.springframework.orm.jpa.SharedEntityManagerCreator] while setting bean property 'entityManager'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name '(inner bean)#236c098': Cannot resolve reference to bean 'entityManagerFactory' while setting constructor argument; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'entityManagerFactory': Post-processing of FactoryBean's singleton object failed; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'chatController': Unsatisfied dependency expressed through field 'userService'; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'userServiceImpl': Unsatisfied dependency expressed through field 'userRepo'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'userRepository': FactoryBean threw exception on object creation; nested exception is 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());
}
}
这个似乎无法通过Spring启动自动装配的 userRepository :
@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
答案 0 :(得分:0)
我通过自动装配到安全配置我使用的所有服务和存储库来解决问题。