title: Handling error: NestedServletException, Handler dispatch failed; nested exception is java.lang.StackOverflowError
body: <p>I took these codes from github and they work good a lone but when put them in my project i have errors
  I'm trying to implement oauth2 in spring boot but i have these two errors</p>
  
  <pre><code>INFO 28433 --- [nio-8090-exec-1] o.s.s.o.provider.endpoint.TokenEndpoint  : Handling error: NestedServletException, Handler dispatch failed; nested exception is java.lang.StackOverflowError
  WARN 28433 --- [nio-8090-exec-1] .m.m.a.ExceptionHandlerExceptionResolver : Resolved exception caused by Handler execution: org.springframework.web.util.NestedServletException: Handler dispatch failed; nested exception is java.lang.StackOverflowError
  </code></pre>
  
  <p>this is my oauth2 configuration class</p>
  
  <pre><code>@Configuration
  public class OAuth2ServerConfiguration {
  
      private static final String RESOURCE_ID = "restservice";
  
      @Configuration
      @EnableResourceServer
      protected static class ResourceServerConfiguration extends ResourceServerConfigurerAdapter {
  
          @Override
          public void configure(ResourceServerSecurityConfigurer resources) {
              // @formatter:off
              resources.resourceId(RESOURCE_ID);
              // @formatter:on
          }
  
          @Override
          public void configure(HttpSecurity http) throws Exception {
              // @formatter:off
              http.authorizeRequests().antMatchers("/users").hasRole("ADMIN").antMatchers("/greeting").authenticated();
              // @formatter:on
          }
  
      }
  
      @Configuration
      @EnableAuthorizationServer
      protected static class AuthorizationServerConfiguration extends AuthorizationServerConfigurerAdapter {
  
          private TokenStore tokenStore = new InMemoryTokenStore();
  
          @Autowired
          @Qualifier("authenticationManagerBean")
          private AuthenticationManager authenticationManager;
  
          @Autowired
          private CustomUserDetailsService userDetailsService;
  
          @Override
          public void configure(AuthorizationServerEndpointsConfigurer endpoints) throws Exception {
  
              endpoints.authenticationManager(authenticationManager);
              endpoints.allowedTokenEndpointRequestMethods(HttpMethod.GET, HttpMethod.POST);
  
              endpoints.tokenStore(this.tokenStore).authenticationManager(this.authenticationManager)
                      .userDetailsService(userDetailsService);
              // @formatter:on
          }
  
          @Override
          public void configure(ClientDetailsServiceConfigurer clients) throws Exception {
  
              clients.inMemory().withClient("clientapp")
                      // .authorizedGrantTypes("password", "refresh_token")
                      // .authorizedGrantTypes("password", "authorization_code", "refresh_token")
                      .authorizedGrantTypes("client-credentials", "password", "refresh_token")
                      .authorities("ROLE_USER")
                      .scopes("read", "write")
                      .resourceIds(RESOURCE_ID)
                      .secret("123456")
                      .accessTokenValiditySeconds(30 * 24 * 3600);
  
          }
  
          @Bean
          @Primary
          public DefaultTokenServices tokenServices() {
              DefaultTokenServices tokenServices = new DefaultTokenServices();
              tokenServices.setSupportRefreshToken(true);
              tokenServices.setTokenStore(this.tokenStore);
              return tokenServices;
          }
  
      }
  
  }
  </code></pre>
  
  <p>and CustomUserDetails class</p>
  
  <pre><code>@Service
  public class CustomUserDetailsService implements UserDetailsService {
  
      private final CustomerRepository userRepository;
  
      @Autowired
      public CustomUserDetailsService(CustomerRepository userRepository) {
          this.userRepository = userRepository;
      }
  
      @Override
      public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
          Customers user = userRepository.findByUsername(username);
          if (user == null) {
              throw new UsernameNotFoundException(String.format("User %s does not exist!", username));
          }
          return new UserRepositoryUserDetails(user);
      }
  
      private final static class UserRepositoryUserDetails extends Customers implements UserDetails {
  
          private static final long serialVersionUID = 1L;
  
          private UserRepositoryUserDetails(Customers user) {
              super(user);
          }
  
          @Override
          public Collection&lt;? extends GrantedAuthority&gt; getAuthorities() {
              return getRoles();
          }
  
          @Override
          public String getUsername() {
              return getUsername();
          }
  
          @Override
          public boolean isAccountNonExpired() {
              return true;
          }
  
          @Override
          public boolean isAccountNonLocked() {
              return true;
          }
  
          @Override
          public boolean isCredentialsNonExpired() {
              return true;
          }
  
          @Override
          public boolean isEnabled() {
              return true;
          }
  
      }
  
  }
  </code></pre>
  
  <p>I have tried to make a lot of changes but nothing work,it always give me the same error,So what is the reason here?</p>