0
votes

When i added security configuration to spring boot, i came into this annoying error:

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

this is the 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;
    }
}

this is the configuration of 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());
    }
}

and this the userRepository that seems cannot be autowired by 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);
}

and finally, this is my project tree:

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
1
how does your UserDetailsServiceImpl look like ? Does it implement org.springframework.security.core.userdetails.UserDetailsService?pvpkiran
Post the full stack trace.M. Deinum
@SpringBootApplication?Jaiwo99
Add your Implementation for your UserDetailsService herePraneeth Ramesh

1 Answers

0
votes

I resolve the problem by Autowiring into Securityconfing all the services and repositories that i used.