title: Spring security oauth2 - Can't access /oauth/token route
body: <p>I've just begun to make an authentication server for my rest api by following this tutorial : <a href="https://jugbd.org/2017/09/19/implementing-oauth2-spring-boot-spring-security/" rel="nofollow noreferrer">https://jugbd.org/2017/09/19/implementing-oauth2-spring-boot-spring-security/</a>. Everything went well until the very end where I just couldn't access /oauth/token route in order to authenticate.</p>
  
  <p>I think I need a little more explanations in order to understand fully this authentication.</p>
  
  <p>Thanks you,
  Matthieu Meunier</p>
  
  <p>PS : Here are my classes :</p>
  
  <p>ResourceServerConfig.java</p>
  
  <pre><code>@Configuration
  @EnableResourceServer
  public class ResourceServerConfig extends ResourceServerConfigurerAdapter {
      @Override
      public void configure(HttpSecurity http) throws Exception {
          http.authorizeRequests()
                  .antMatchers("/").permitAll()
                  .antMatchers("/api/secure/**").authenticated();
      }
  }
  </code></pre>
  
  <p>AuthorizationServerConfig.java</p>
  
  <pre><code>@Configuration
  @EnableAuthorizationServer
  public class AuthorizationServerConfig extends AuthorizationServerConfigurerAdapter {
      @Autowired
      private AuthenticationManager authenticationManager;
  
      @Override
      public void configure(AuthorizationServerSecurityConfigurer serverSecurityConfigurer){
          serverSecurityConfigurer
                  .tokenKeyAccess("permitAll()")
                  .checkTokenAccess("isAuthenticated()")
                  .allowFormAuthenticationForClients();
      }
  
      @Override
      public void configure(ClientDetailsServiceConfigurer clients) throws Exception{
          clients.inMemory().withClient("android-client")
                  .authorizedGrantTypes("client-credentials", "password", "refresh_token")
                  .authorities("ROLE_CLIENT", "ROLE_ANDROID_CLIENT")
                  .scopes("read", "write", "trust")
                  .resourceIds("oauth2resource")
                  .accessTokenValiditySeconds(5000)
                  .secret("android-secret").refreshTokenValiditySeconds(50000);
      }
      @Override
      public void configure(AuthorizationServerEndpointsConfigurer endpoints){
          endpoints.authenticationManager(authenticationManager)
                  .allowedTokenEndpointRequestMethods(HttpMethod.GET, HttpMethod.POST);
      }
  }
  </code></pre>
  
  <p>Just below my Main</p>
  
  <pre><code>@Autowired
      CustomUserDetailsService userDetailsService;
  
      @Autowired
      public void authenticationManager(AuthenticationManagerBuilder builder) throws Exception{
          builder.userDetailsService(userDetailsService);
      }
  </code></pre>
  
  <p>And finally my CustomUserDetailsService and the UserService</p>
  
  <p>CustomUserDetailsService.java</p>
  
  <pre><code>@Service
  public class CustomUserDetailsService implements UserDetailsService {
  
      private final UserService userService;
  
      @Autowired
      public CustomUserDetailsService(UserService userService) {
          this.userService = userService;
      }
  
      @Override
      public UserDetails loadUserByUsername(String email) throws UsernameNotFoundException {
          return this.userService.findByEmail(email);
      }
  }
  </code></pre>
  
  <p>UserService.java</p>
  
  <pre><code>@Service
  public class UserService {
  
      @Autowired
      MembreRepository membreRepository;
  
      public UserDetails findByEmail(String email){
          return membreRepository.findOneByEmail(email);
      }
  }
  </code></pre>