3

I'm newbie in Spring-World, I have a Spring Boot application with Spring Security and JPA. Also have CrudRepository and UserDetailsService, see below

Application class

@Configuration
@ComponentScan
@EnableAutoConfiguration
public class Application {
    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }
}

UserDao

@Repository
public interface UserDao extends CrudRepository<User, Long> {
    public Collection<User> findByName(String name);
}

ApiUserDetailsService

@Component
public class ApiUserDetailsService implements UserDetailsService {

    @Autowired
    private UserDao dao;

    @Override
    public UserDetails loadUserByUsername(final String username) throws UsernameNotFoundException {
        assert dao != null;
        ...
    }
}

Security Config

@Configuration
@EnableWebSecurity
public class HttpBasicAuthConfig extends WebSecurityConfigurerAdapter {

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http.csrf().disable();
        http.requestCache().requestCache(new NullRequestCache());
        http.httpBasic();
        http.authorizeRequests().anyRequest().authenticated();
    }


    @Autowired
    public void registerAuthentication(AuthenticationManagerBuilder auth) throws Exception {
        auth.userDetailsService(new ApiUserDetailsService());
    }
}

Why Autowired dao is always null? What I do wrong?

Dmitriy Tarasov
  • 1,949
  • 20
  • 37
  • One suggestion might be that your component not being scanned. @ComponentScan - "may be specified to define specific packages to scan. If specific * packages are not defined scanning will occur from the package of the class with this annotation" – birya Aug 27 '14 at 21:16
  • 1
    You are using `Spring Data`?, Your UserDao is a interface, also in your registerAuthentication you are creating a new `ApiUserDetailsService`, you need to autowired it!. – Arturo Volpe Aug 27 '14 at 21:48
  • possible duplicate of [Why is my Spring @Autowired field null?](http://stackoverflow.com/questions/19896870/why-is-my-spring-autowired-field-null) – chrylis -cautiouslyoptimistic- Aug 27 '14 at 23:28

1 Answers1

9

You are creating the ApiUserDetailsService manually, in the the method:

@Autowired
public void registerAuthentication(AuthenticationManagerBuilder auth) throws Exception {
    auth.userDetailsService(new ApiUserDetailsService());
}

What you want is:

@Configuration
@EnableWebSecurity
@EnableJpaRepositories(basePackages = {"your.package.dao"})
public class HttpBasicAuthConfig extends WebSecurityConfigurerAdapter {

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http.csrf().disable();
        http.requestCache().requestCache(new NullRequestCache());
        http.httpBasic();
        http.authorizeRequests().anyRequest().authenticated();
    }

    // register ApiUserDetailsService as a bean  
    @Bean 
    public UserDetailsService apiUserDetailsService() {
        return new ApiUserDetailsService();      
    } 

    @Autowired
    public void registerAuthentication(AuthenticationManagerBuilder auth) throws     Exception {
        // get the autowired bean from Spring
        auth.userDetailsService(apiUserDetailsService());
    }
}
Ricardo Veguilla
  • 3,107
  • 1
  • 18
  • 17