title: Spring Boot and OAuth2 server
body: <p>I'm trying to implement a simple OAuth2 server with Spring Boot.
  In the first step I added in the main class the annotation @EnableAuthorizationServer</p>
  <pre><code>@SpringBootApplication
  @EnableAuthorizationServer
  public class PocApplication {
  
      public static void main(String[] args) {
          SpringApplication.run(PocApplication.class, args);
      }
  }
  </code></pre>
  <p>and I use the &quot;application.yml&quot; file to configure all the OAuth2 elements.</p>
  <pre><code>server:
    port: 8282
  spring:
    security:
      user: 
        name: root
        password: root
        roles: ADMIN,USER
  security:
    oauth2:
      client:
        client-id: mobile 
        client-secret: pin
        access-token-validity-seconds: 3600
        authorized-grant-types: client_credential,password
        scope: READ, WRITE
      authorization:
        check-token-access: permitAll
  </code></pre>
  <p>With this configuration everything works fine. <br>
  In the second step I tried to switch the configuration from yml file to java class.<br>
  These are the classes that I created</p>
  <pre><code>@Configuration
  public class AuthServerConfigurations extends WebSecurityConfigurerAdapter implements AuthorizationServerConfigurer {
  
      @Bean
      public AuthenticationManager authenticationManagerBean() throws Exception {
          return super.authenticationManagerBean();
      }
  
      @Autowired
      AuthenticationManager authenticationManager;
      
      PasswordEncoder passwordEncoder = PasswordEncoderFactories.createDelegatingPasswordEncoder();
      
      @Override
      public void configure(AuthorizationServerSecurityConfigurer security) throws Exception {
          security.checkTokenAccess(&quot;permitAll()&quot;);
      }
  
      @Override
      public void configure(ClientDetailsServiceConfigurer client) throws Exception {
          client.inMemory().withClient(&quot;mobile&quot;).secret(passwordEncoder.encode(&quot;pin&quot;)).scopes(&quot;READ&quot;, &quot;WRITE&quot;).authorizedGrantTypes(&quot;password&quot;, &quot;client_credential&quot;);
      }
  
      @Override
      public void configure(AuthorizationServerEndpointsConfigurer endpoint) throws Exception {
          endpoint.authenticationManager(authenticationManager);
      }
  }
  </code></pre>
  <p>and</p>
  <pre><code>@Configuration
  public class UserConfiguration extends GlobalAuthenticationConfigurerAdapter {
  
      PasswordEncoder passwordEncoder=PasswordEncoderFactories.createDelegatingPasswordEncoder();
  
  
      @Override
      public void init(AuthenticationManagerBuilder auth) throws Exception {
          auth.inMemoryAuthentication()
              .withUser(&quot;root&quot;).password(passwordEncoder.encode(&quot;root&quot;)).roles(&quot;USER&quot;,&quot;ADMIN&quot;);
      }
  }
  </code></pre>
  <p>With this configuration the app doesn't work.
  When I run the project via maven I see that Spring generate a new &quot;client-id&quot; and &quot;client-secret&quot; instead to use &quot;mobile&quot; and &quot;pin&quot;</p>
  <pre><code>security.oauth2.client.client-id = 27234511-14a5-4b94-9cd3-ffc77c5189f3
  security.oauth2.client.client-secret = 5895bf76-6b30-4160-9f13-1cbad7b986e5
  </code></pre>
  <p>this is the log</p>
  <pre><code>  .   ____          _            __ _ _
   /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
  ( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
   \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
    '  |____| .__|_| |_|_| |_\__, | / / / /
   =========|_|==============|___/=/_/_/_/
   :: Spring Boot ::                (v2.6.0)
  
  2022-02-02 09:55:12.291  INFO 113288 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat initialized with port(s): 8282 (http)
  2022-02-02 09:55:12.413  INFO 113288 --- [           main] o.apache.catalina.core.StandardService   : Starting service [Tomcat]
  2022-02-02 09:55:12.414  INFO 113288 --- [           main] org.apache.catalina.core.StandardEngine  : Starting Servlet engine: [Apache Tomcat/9.0.55]
  2022-02-02 09:55:12.711  INFO 113288 --- [           main] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring embedded WebApplicationContext
  2022-02-02 09:55:12.712  INFO 113288 --- [           main] w.s.c.ServletWebServerApplicationContext : Root WebApplicationContext: initialization completed in 5978 ms
  2022-02-02 09:55:13.407  INFO 113288 --- [           main] .s.s.UserDetailsServiceAutoConfiguration :
  
  Using generated security password: 940524c4-8efa-4d7d-826b-864ff4b48392
  
  2022-02-02 09:55:13.826  INFO 113288 --- [           main] a.OAuth2AuthorizationServerConfiguration : Initialized OAuth2 Client
  
  security.oauth2.client.client-id = 27234511-14a5-4b94-9cd3-ffc77c5189f3
  security.oauth2.client.client-secret = 5895bf76-6b30-4160-9f13-1cbad7b986e5
  
  
  2022-02-02 09:55:15.130  INFO 113288 --- [           main] o.s.s.web.DefaultSecurityFilterChain     : Will secure Or [Ant [pattern='/oauth/token'], Ant [pattern='/oauth/token_key'], Ant [pattern='/oauth/check_token']] with [org.springframework.security.web.context.request.async.WebAsyncManagerIntegrationFilter@41bfa9e9, org.springframework.security.web.context.SecurityContextPersistenceFilter@13ed066e, org.springframework.security.web.header.HeaderWriterFilter@68ef01a5, org.springframework.security.web.authentication.logout.LogoutFilter@5ae15, org.springframework.security.web.authentication.www.BasicAuthenticationFilter@1bbddada, org.springframework.security.web.savedrequest.RequestCacheAwareFilter@590765c4, org.springframework.security.web.servletapi.SecurityContextHolderAwareRequestFilter@64b018f3, org.springframework.security.web.authentication.AnonymousAuthenticationFilter@68b7d0ef, org.springframework.security.web.session.SessionManagementFilter@7126e26, org.springframework.security.web.access.ExceptionTranslationFilter@4872669f, org.springframework.security.web.access.intercept.FilterSecurityInterceptor@59c500f7]
  2022-02-02 09:55:15.301  INFO 113288 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat started on port(s): 8282 (http) with context path ''
  </code></pre>
  <p>Why the java class configuration  doesn't work?<br>
  Thanks</p>