title: Stack overflow calling Spring authenticationManager.authenticate
body: <p>I started with a spring boot starter project version 2.3.5, but when I call <code>authenticationManager.authenticate</code> I get a stack overflow error.</p>
  <pre><code>java.lang.StackOverflowError: null
      at ch.qos.logback.classic.Logger.callTurboFilters(Logger.java:751) ~[logback-classic-1.2.3.jar:na]
      at ch.qos.logback.classic.Logger.isDebugEnabled(Logger.java:469) ~[logback-classic-1.2.3.jar:na]
      at ch.qos.logback.classic.Logger.isDebugEnabled(Logger.java:465) ~[logback-classic-1.2.3.jar:na]
      at org.apache.commons.logging.LogAdapter$Slf4jLog.isDebugEnabled(LogAdapter.java:310) ~[spring-jcl-5.2.10.RELEASE.jar:5.2.10.RELEASE]
      at org.springframework.security.authentication.ProviderManager.authenticate(ProviderManager.java:186) ~[spring-security-core-5.3.5.RELEASE.jar:5.3.5.RELEASE]
      at org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter$AuthenticationManagerDelegator.authenticate(WebSecurityConfigurerAdapter.java:524) ~[spring-security-config-5.3.5.RELEASE.jar:5.3.5.RELEASE]
      at org.springframework.security.authentication.ProviderManager.authenticate(ProviderManager.java:219) ~[spring-security-core-5.3.5.RELEASE.jar:5.3.5.RELEASE]
      at org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter$AuthenticationManagerDelegator.authenticate(WebSecurityConfigurerAdapter.java:524) ~[spring-security-config-5.3.5.RELEASE.jar:5.3.5.RELEASE]
  </code></pre>
  <p>Initially I did not use the user variable and instead created created the new <code>UsernamePasswordAuthenticationToken</code> inside the <code>authenticationManager.authenticate</code> either way I get the stack overflow error. I also have tried <code>@PostMapping</code> instead of <code>@RequestMapping</code> and Method. All of these cause the same failure.
  I get the printline that says I am &quot;in /Authenticate&quot; with the following.</p>
  <pre><code>2020-11-09 16:20:32.141 DEBUG 10456 --- [nio-8080-exec-2] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped to io.javabrains.springsecurityjwt.HelloResource#createAuthenticationToken(AuthenticationRequest)
  2020-11-09 16:20:32.188 DEBUG 10456 --- [nio-8080-exec-2] m.m.a.RequestResponseBodyMethodProcessor : Read &quot;application/json;charset=UTF-8&quot; to [io.javabrains.springsecurityjwt.models.AuthenticationRequest@758a32c5]
  in /Authenticate
  2020-11-09 16:20:32.197 DEBUG 10456 --- [nio-8080-exec-2] o.s.web.servlet.DispatcherServlet        : Failed to complete request: org.springframework.web.util.NestedServletException: Handler dispatch failed; nested exception is java.lang.StackOverflowError
  2020-11-09 16:20:32.201 ERROR 10456 --- [nio-8080-exec-2] o.a.c.c.C.[.[.[/].[dispatcherServlet]    : Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Handler dispatch failed; nested exception is java.lang.StackOverflowError] with root cause
  </code></pre>
  <p>Here is the full code of the Class
  Thank you for any info or things I should try!</p>
  <pre><code>package io.javabrains.springsecurityjwt;
  
  import io.javabrains.springsecurityjwt.models.AuthenticationRequest;
  import io.javabrains.springsecurityjwt.models.AuthenticationResponse;
  import io.javabrains.springsecurityjwt.services.MyUserDetailsService;
  import io.javabrains.springsecurityjwt.util.JwtUtil;
  import org.springframework.beans.factory.annotation.Autowired;
  import org.springframework.http.ResponseEntity;
  import org.springframework.security.authentication.AuthenticationManager;
  import org.springframework.security.authentication.BadCredentialsException;
  import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
  import org.springframework.security.core.userdetails.UserDetails;
  import org.springframework.web.bind.annotation.RequestBody;
  import org.springframework.web.bind.annotation.RequestMapping;
  import org.springframework.web.bind.annotation.RequestMethod;
  import org.springframework.web.bind.annotation.RestController;
  
  @RestController
  public class HelloResource {
  
      @Autowired
      private AuthenticationManager authenticationManager;
  
      @Autowired
      private MyUserDetailsService userDetailsService;
  
      @Autowired
      private JwtUtil jwtTokenUtil;
  
      @RequestMapping( &quot;/hello&quot;)
      public String hello() {
          return &quot;Hello World&quot;;
      }
  
      @RequestMapping(value = &quot;/authenticate&quot;, method = RequestMethod.POST)
      public ResponseEntity&lt;?&gt; createAuthenticationToken(@RequestBody AuthenticationRequest authenticationRequest) throws Exception {
          UsernamePasswordAuthenticationToken user = new UsernamePasswordAuthenticationToken(authenticationRequest.getUsername(), authenticationRequest.getPassword());
          System.out.println(&quot;in /Authenticate&quot;);
          try {
              authenticationManager.authenticate(
                      user
  //                  new UsernamePasswordAuthenticationToken(authenticationRequest.getUsername(), authenticationRequest.getPassword())
              );
              System.out.println(&quot;in Try in /Authenticate&quot;);
          } catch (BadCredentialsException e) {
              throw new Exception(&quot;Incorrect Username or Password&quot;, e);
          }
          System.out.println(&quot;outside the try catch&quot;);
  
          final UserDetails userDetails = userDetailsService
                  .loadUserByUsername(authenticationRequest.getUsername());
          final String jwt = jwtTokenUtil.generateToken(userDetails);
  
          return ResponseEntity.ok(new AuthenticationResponse(jwt));
  
      }
  }
  </code></pre>