title: when implementing Spring Security configuration getting java.lang.StackOverFlow
body: <p>I'm trying to configure my <code>WebSecurityConfig</code> class for my <code>mySQL</code> database. What i m trying to achieve here is once user is created , automatically token will be created and assigned to the client. But i think i'm having problem about looking through all username's inside of the database.</p>
  
  <p>So here is my <code>UserController</code>;</p>
  
  <pre><code>@RestController
  public class UserController {
  @Autowired
  private IUserService userService;
  
  @Autowired
  private SecurityService securityService;
  
     @RequestMapping(value = "/createuser", method = RequestMethod.POST)
      public String createUser(@RequestBody User user, Model md) {
          if (userService.checkExistUserName(user.getUserName())) {
              md.addAttribute("LoginError", true);
              return "Username exists, try with another username";
          }
          User newUser = new User();
          newUser.setUserName(user.getUserName());
          newUser.setFirstname(user.getFirstname());
          newUser.setUserMail(user.getUserMail());
          newUser.setSurname(user.getSurname());
          newUser.setUserPassword(user.getUserPassword());
          userService.saveUser(user);
  
          /* Automatic login after register */
          securityService.autologin(user.getUserName(), user.getUserPassword());
  
          return user.getId().toString();
      }
  </code></pre>
  
  <p><strong>Here is the  <code>SecurityService</code>  class</strong> </p>
  
  <pre><code>@Service
  public class SecurityService {
      @Autowired
      IUserService userService;
  
  @Autowired
  private AuthenticationManager authenticationManager;
  
  @Autowired
  private UserDetailsService userDetailsService;
  
  public User activeUser() {
      Authentication auth = SecurityContextHolder.getContext().getAuthentication();
      String username = auth.getName();
      Optional&lt;User&gt; user = userService.getUserByName(username);
  
      if (user.isPresent()) {
          return user.get();
      }
      return null;
  }
  public void autologin(String username, String password) {
      UserDetails userDetails = userDetailsService.loadUserByUsername(username);
      UsernamePasswordAuthenticationToken usernamePasswordAuthenticationToken = new UsernamePasswordAuthenticationToken(userDetails, password, AuthorityUtils.createAuthorityList("USER"));
  
      authenticationManager.authenticate(usernamePasswordAuthenticationToken);
  
      if (usernamePasswordAuthenticationToken.isAuthenticated()) {
          SecurityContextHolder.getContext().setAuthentication(usernamePasswordAuthenticationToken);
      }
  }
  }
  </code></pre>
  
  <p><strong>Here is the  <code>WebSecurityConfig</code>  class</strong>  </p>
  
  <pre><code>@Configuration
  @EnableWebSecurity
  @Service
  public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
  
  @Autowired
  private UserDetailsService userDetailsService;
  
  @Override
  protected void configure(HttpSecurity http) throws Exception {
      // disable caching
      http.headers().cacheControl();
  
      http.csrf().disable() // disable csrf for our requests.
              .authorizeRequests()
              .antMatchers("/").permitAll()
              .antMatchers(HttpMethod.GET, "/login").permitAll()
              .antMatchers(HttpMethod.POST, "/createuser").permitAll()
              .antMatchers(HttpMethod.GET, "/user/1").permitAll()
              .anyRequest().authenticated()
              .and()
              // We filter the api/login requests
              .addFilterBefore(new JWTLoginFilter("/login", authenticationManager()), UsernamePasswordAuthenticationFilter.class)
              // And filter other requests to check the presence of JWT in header
              .addFilterBefore(new JWTAuthenticationFilter(), UsernamePasswordAuthenticationFilter.class);
  }
  
  @Override
  protected void configure(AuthenticationManagerBuilder auth) throws Exception {
      // Create a default account
  
      auth.userDetailsService(userDetailsService);
  }
  @Bean
  public UserDetailsService userDetailsService() {
      return super.userDetailsService();
  }
  }
  </code></pre>
  
  <p>When i create an user , it creates to database, but gives me that error below;</p>
  
  <pre><code>java.lang.StackOverflowError: null
      at org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter$UserDetailsServiceDelegator.loadUserByUsername(WebSecurityConfigurerAdapter.java:442) ~[spring-security-config-4.2.1.RELEASE.jar:4.2.1.RELEASE]
      at org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter$UserDetailsServiceDelegator.loadUserByUsername(WebSecurityConfigurerAdapter.java:442) ~[spring-security-config-4.2.1.RELEASE.jar:4.2.1.RELEASE]
      at org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter$UserDetailsServiceDelegator.loadUserByUsername(WebSecurityConfigurerAdapter.java:442) ~[spring-security-config-4.2.1.RELEASE.jar:4.2.1.RELEASE]
      at org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter$UserDetailsServiceDelegator.loadUserByUsername(WebSecurityConfigurerAdapter.java:442) ~[spring-security-config-4.2.1.RELEASE.jar:4.2.1.RELEASE]
      at org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter$UserDetailsServiceDelegator.loadUserByUsername(WebSecurityConfigurerAdapter.java:442) ~[spring-security-config-4.2.1.RELEASE.jar:4.2.1.RELEASE]
      at org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter$UserDetailsServiceDelegator.loadUserByUsername(WebSecurityConfigurerAdapter.java:442) ~[spring-security-config-4.2.1.RELEASE.jar:4.2.1.RELEASE]
      at org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter$UserDetailsServiceDelegator.loadUserByUsername(WebSecurityConfigurerAdapter.java:442) ~[spring-security-config-4.2.1.RELEASE.jar:4.2.1.RELEASE]
      at org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter$UserDetailsServiceDelegator.loadUserByUsername(WebSecurityConfigurerAdapter.java:442) ~[spring-security-config-4.2.1.RELEASE.jar:4.2.1.RELEASE]
      at org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter$UserDetailsServiceDelegator.loadUserByUsername(WebSecurityConfigurerAdapter.java:442) ~[spring-security-config-4.2.1.RELEASE.jar:4.2.1.RELEASE]
      at org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter$UserDetailsServiceDelegator.loadUserByUsername(WebSecurityConfigurerAdapter.java:442) ~[spring-security-config-4.2.1.RELEASE.jar:4.2.1.RELEASE]
      at org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter$UserDetailsServiceDelegator.loadUserByUsername(WebSecurityConfigurerAdapter.java:442) ~[spring-security-config-4.2.1.RELEASE.jar:4.2.1.RELEASE]
      at org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter$UserDetailsServiceDelegator.loadUserByUsername(WebSecurityConfigurerAdapter.java:442) ~[spring-security-config-4.2.1.RELEASE.jar:4.2.1.RELEASE]
      at org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter$UserDetailsServiceDelegator.loadUserByUsername(WebSecurityConfigurerAdapter.java:442) ~[spring-security-config-4.2.1.RELEASE.jar:4.2.1.RELEASE]
      at org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter$UserDetailsServiceDelegator.loadUserByUsername(WebSecurityConfigurerAdapter.java:442) ~[spring-security-config-4.2.1.RELEASE.jar:4.2.1.RELEASE]
      at org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter$UserDetailsServiceDelegator.loadUserByUsername(WebSecurityConfigurerAdapter.java:442) ~[spring-security-config-4.2.1.RELEASE.jar:4.2.1.RELEASE]
      at org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter$UserDetailsServiceDelegator.loadUserByUsername(WebSecurityConfigurerAdapter.java:442) ~[spring-security-config-4.2.1.RELEASE.jar:4.2.1.RELEASE]
      at org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter$UserDetailsServiceDelegator.loadUserByUsername(WebSecurityConfigurerAdapter.java:442) ~[spring-security-config-4.2.1.RELEASE.jar:4.2.1.RELEASE]
      at org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter$UserDetailsServiceDelegator.loadUserByUsername(WebSecurityConfigurerAdapter.java:442) ~[spring-security-config-4.2.1.RELEASE.jar:4.2.1.RELEASE]
      at org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter$UserDetailsServiceDelegator.loadUserByUsername(WebSecurityConfigurerAdapter.java:442) ~[spring-security-config-4.2.1.RELEASE.jar:4.2.1.RELEASE]
      at org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter$UserDetailsServiceDelegator.loadUserByUsername(WebSecurityConfigurerAdapter.java:442) ~[spring-security-config-4.2.1.RELEASE.jar:4.2.1.RELEASE]
      at org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter$UserDetailsServiceDelegator.loadUserByUsername(WebSecurityConfigurerAdapter.java:442) ~[spring-security-config-4.2.1.RELEASE.jar:4.2.1.RELEASE]
      at org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter$UserDetailsServiceDelegator.loadUserByUsername(WebSecurityConfigurerAdapter.java:442) ~[spring-security-config-4.2.1.RELEASE.jar:4.2.1.RELEASE]
      at org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter$UserDetailsServiceDelegator.loadUserByUsername(WebSecurityConfigurerAdapter.java:442) ~[spring-security-config-4.2.1.RELEASE.jar:4.2.1.RELEASE]
      at org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter$UserDetailsServiceDelegator.loadUserByUsername(WebSecurityConfigurerAdapter.java:442) ~[spring-security-config-4.2.1.RELEASE.jar:4.2.1.RELEASE]
  </code></pre>
  
  <p>It goes all the way bottom like that.</p>
  
  <p>So i think the problem here is <code>UserDetailService</code> doesn't know how to search usernames in the database or it might be something else. I might not configure the <code>WebSecurityConfig class</code> properly. So any Suggestion? </p>