title: Spring Security authentication using UserDetailsService
body: <p>I have some issues with Spring security authentication.
  Everywhere in my application everything works great (CRUD operations work well), but login attempt fails.</p>
  
  <p>Here's my code (I marked below with comments where userDAO is null which is cause of failed authentication):</p>
  
  <pre><code>@Service
  public class UserServiceImpl implements UserService, UserDetailsService {
  
      @Autowired
      UserDAO userDAO;
  
      @Override
      public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
          User user = userDAO.getUserByUsername(username); //userDAO == null Causing NPE
          if (user == null)
              throw new UsernameNotFoundException("Oops!");
  
          List&lt;SimpleGrantedAuthority&gt; authorities = Arrays.asList(new SimpleGrantedAuthority(user.getRole()));
  
          return new org.springframework.security.core.userdetails
                  .User(user.getLogin(), user.getPassword(), authorities);
      }
  
  @Override
      public List&lt;User&gt; getUsers() {
          return userDAO.getUsers();//userDAO !=null
      }
  //rest of code skipped
  </code></pre>
  
  <p>My SecurityConfig looks like this</p>
  
  <pre><code>@Configuration
  @EnableWebMvcSecurity
  @EnableGlobalMethodSecurity(prePostEnabled = true)
  public class SecurityConfig extends WebSecurityConfigurerAdapter {
  
   UserServiceImpl userService = new UserServiceImpl();
  
      @Override
      protected void configure(AuthenticationManagerBuilder auth) throws Exception {
          auth.userDetailsService(userService);
      }
  //rest of code skipped
  </code></pre>
  
  <p>I marked where i get NPE and i have no idea how to solve this. Whole Configuration is JavaBased and you can check it out out here for more details 
  <a href="https://github.com/spike994/GreatIntelligence" rel="nofollow">HERE</a> </p>
  
  <p>EDIT: getUsers() is invoked this way in controller:</p>
  
  <pre><code>@Controller
  public class LoginController {
      @Autowired
      UserService userService;
  
   @RequestMapping(value = "/dashboard")
      public ModelAndView userDashboard(){
          ModelAndView modelAndView = new ModelAndView("Dashboard");
          List&lt;User&gt; userList = userService.getUsers();
          modelAndView.addObject("users", userList);
          return modelAndView;
      }
  </code></pre>
  
  <p>And in this case (when invoking userService.getUsers()) userDAO is not null</p>
  
  <p>Tried to fix it like Bohuslav Burghardt suggested and i got</p>
  
  <pre><code> method userDetailsService in class org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder cannot be applied to given types;
    required: T
    found: com.gi.service.UserService
    reason: inferred type does not conform to upper bound(s)
      inferred: com.gi.service.UserService
      upper bound(s): org.springframework.security.core.userdetails.UserDetailsService
  </code></pre>
  
  <p>in line
  auth.userDetailsService(userService);</p>