title: Spring Security (java.lang.NullPointerException: null at org.springframework.security.authentication.ProviderManager.authenticate)
body: <p>I have problem with spring security.
  I'm new in it, so can't understand why it work from time to time. (every 2nd or 3rd restart of main class i get null pointer from ProviderManager). As i think the loadUserByUsername method of my implementation of UserDetailsService do not calls by spring at that time.</p>
  
  <p><strong>Error StackTrace</strong></p>
  
  <pre><code>2018-06-27 05:07:39.180  INFO 5556 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet        : FrameworkServlet 'dispatcherServlet': initialization completed in 15 ms
  2018-06-27 05:07:40.984  WARN 5556 --- [nio-8080-exec-2] o.a.c.util.SessionIdGeneratorBase        : Creation of SecureRandom instance for session ID generation using [SHA1PRNG] took [101] milliseconds.
  2018-06-27 05:07:42.285 ERROR 5556 --- [nio-8080-exec-4] o.a.c.c.C.[.[.[/].[dispatcherServlet]    : Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception
  java.lang.NullPointerException: null
      at org.springframework.security.authentication.ProviderManager.authenticate(ProviderManager.java:164) ~[spring-security-core-5.0.5.RELEASE.jar:5.0.5.RELEASE]
      at org.springframework.security.authentication.ProviderManager.authenticate(ProviderManager.java:199) ~[spring-security-core-5.0.5.RELEASE.jar:5.0.5.RELEASE]
      at org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter.attemptAuthentication(UsernamePasswordAuthenticationFilter.java:94) ~[spring-security-web-5.0.5.RELEASE.jar:5.0.5.RELEASE]
  </code></pre>
  
  <p><strong>MyConfig</strong></p>
  
  <pre><code>@Configuration
  @ComponentScan("hello")
  public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
  
      Logger log = LoggerFactory.getLogger(WebSecurityConfig1.class);
  
      @Autowired
      public PasswordEncoder passwordEncoder;
  
      private AuthenticationProvider authenticationProvider;
  
      @Autowired
      @Qualifier("daoAuthenticationProvider")
      public void setAuthenticationProvider(AuthenticationProvider authenticationProvider) {
          this.authenticationProvider = authenticationProvider;
  
      }
  
  
      @Bean
      public DaoAuthenticationProvider daoAuthenticationProvider(PasswordEncoder passwordEncoder,
                                                                 UserDetailsService userDetailsService){ 
          DaoAuthenticationProvider daoAuthenticationProvider = new DaoAuthenticationProvider();
          daoAuthenticationProvider.setPasswordEncoder(passwordEncoder);
          daoAuthenticationProvider.setUserDetailsService(userDetailsService);
          return daoAuthenticationProvider;
      }
  
      @Autowired
      public void configureAuthManager(AuthenticationManagerBuilder authenticationManagerBuilder){
          authenticationManagerBuilder.authenticationProvider(authenticationProvider);
      }
  
  
      @Override
      protected void configure(HttpSecurity http) throws Exception {
          http
                  .authorizeRequests()
                  .antMatchers("/", "/home").permitAll()
                  .anyRequest().authenticated()
                  .and()
                  .formLogin()
                  .loginPage("/login")
                  .permitAll()
                  .and()
                  .logout()
                  .permitAll();
      }
  }
  </code></pre>
  
  <p><strong>UserDetailsService</strong></p>
  
  <pre><code>@Service("userDetailsService")
  public class UDservice implements UserDetailsService {
  
      Logger log = LoggerFactory.getLogger(UDservice1.class);
  
      @Autowired
      UserRepository repository;
  
      public UserDetails converter(User user) {
          UserDetailsImpl userDetails = new UserDetailsImpl();
          userDetails.setUsername(user.getUsername());
          userDetails.setPassword(user.getPwd());
          userDetails.setEnabled(user.getEnabled());
          Collection&lt;SimpleGrantedAuthority&gt; authorities = new ArrayList&lt;&gt;();
          authorities.add(new SimpleGrantedAuthority(user.getRole()));
          userDetails.setAuthorities(authorities);
          return userDetails;
      }
  
  
      @Override
      public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
          if (username != null){
           return  converter(repository.findByUsername(username));
          }
          else
              throw new UsernameNotFoundException("null at name");
      }
  }
  </code></pre>