title: Error creating bean with name 'securityFilterChain'
body: <p>I am Learning Spring Boot and i am trying to apply OAuth2 to my rest API. but now I Am  getting an error that i does not understand.I have looked up so many answers but it The Error is  as :</p>
  <pre><code>org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'org.springframework.security.config.annotation.web.configuration.WebSecurityConfiguration': Unsatisfied dependency expressed through method 'setFilterChains' parameter 0; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'securityFilterChain' defined in class path resource [com/application/pms/config/WebSecurityConfig.class]: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.springframework.security.web.SecurityFilterChain]: Factory method 'securityFilterChain' threw exception; nested exception is java.lang.NoClassDefFoundError: org/springframework/security/oauth2/client/web/OAuth2AuthorizedClientRepository
      at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredMethodElement.resolveMethodArguments(AutowiredAnnotationBeanPostProcessor.java:767)
      at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredMethodElement.inject(AutowiredAnnotationBeanPostProcessor.java:719)
      at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:119)
      at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessProperties(AutowiredAnnotationBeanPostProcessor.java:399)
      at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1431)
      at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:619)
      at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:542)
      at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:335)
      at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:234)
      at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:333)
      at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:208)
      at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:953)
      at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:918)
      at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:583)
      at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.refresh(ServletWebServerApplicationContext.java:145)
      at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:740)
      at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:415)
      at org.springframework.boot.SpringApplication.run(SpringApplication.java:303)
      at org.springframework.boot.SpringApplication.run(SpringApplication.java:1312)
      at org.springframework.boot.SpringApplication.run(SpringApplication.java:1301)
      at com.application.pms.ProductManagementSystemApplication.main(ProductManagementSystemApplication.java:10)
      at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
      at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:77)
      at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
      at java.base/java.lang.reflect.Method.invoke(Method.java:568)
      at org.springframework.boot.devtools.restart.RestartLauncher.run(RestartLauncher.java:49)
  Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'securityFilterChain' defined in class path resource [com/application/pms/config/WebSecurityConfig.class]: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.springframework.security.web.SecurityFilterChain]: Factory method 'securityFilterChain' threw exception; nested exception is java.lang.NoClassDefFoundError: org/springframework/security/oauth2/client/web/OAuth2AuthorizedClientRepository
      at org.springframework.beans.factory.support.ConstructorResolver.instantiate(ConstructorResolver.java:658)
      at org.springframework.beans.factory.support.ConstructorResolver.instantiateUsingFactoryMethod(ConstructorResolver.java:638)
      at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateUsingFactoryMethod(AbstractAutowireCapableBeanFactory.java:1352)
      at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:1195)
      at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:582)
      at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:542)
      at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:335)
      at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:234)
      at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:333)
      at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:208)
      at org.springframework.beans.factory.config.DependencyDescriptor.resolveCandidate(DependencyDescriptor.java:276)
      at org.springframework.beans.factory.support.DefaultListableBeanFactory.addCandidateEntry(DefaultListableBeanFactory.java:1607)
      at org.springframework.beans.factory.support.DefaultListableBeanFactory.findAutowireCandidates(DefaultListableBeanFactory.java:1571)
      at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveMultipleBeans(DefaultListableBeanFactory.java:1460)
      at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1347)
      at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:1309)
      at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredMethodElement.resolveMethodArguments(AutowiredAnnotationBeanPostProcessor.java:759)
      ... 25 more
  Caused by: org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.springframework.security.web.SecurityFilterChain]: Factory method 'securityFilterChain' threw exception; nested exception is java.lang.NoClassDefFoundError: org/springframework/security/oauth2/client/web/OAuth2AuthorizedClientRepository
      at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:185)
      at org.springframework.beans.factory.support.ConstructorResolver.instantiate(ConstructorResolver.java:653)
      ... 41 more
  Caused by: java.lang.NoClassDefFoundError: org/springframework/security/oauth2/client/web/OAuth2AuthorizedClientRepository
      at org.springframework.security.config.annotation.web.builders.HttpSecurity.oauth2Login(HttpSecurity.java:2550)
      at com.application.pms.config.WebSecurityConfig.securityFilterChain(WebSecurityConfig.java:44)
      at com.application.pms.config.WebSecurityConfig$$EnhancerBySpringCGLIB$$68f91368.CGLIB$securityFilterChain$0(&lt;generated&gt;)
      at com.application.pms.config.WebSecurityConfig$$EnhancerBySpringCGLIB$$68f91368$$FastClassBySpringCGLIB$$d7d14cc5.invoke(&lt;generated&gt;)
      at org.springframework.cglib.proxy.MethodProxy.invokeSuper(MethodProxy.java:244)
      at org.springframework.context.annotation.ConfigurationClassEnhancer$BeanMethodInterceptor.intercept(ConfigurationClassEnhancer.java:331)
      at com.application.pms.config.WebSecurityConfig$$EnhancerBySpringCGLIB$$68f91368.securityFilterChain(&lt;generated&gt;)
      at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
      at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:77)
      at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
      at java.base/java.lang.reflect.Method.invoke(Method.java:568)
      at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:154)
      ... 42 more
  Caused by: java.lang.ClassNotFoundException: org.springframework.security.oauth2.client.web.OAuth2AuthorizedClientRepository
      at java.base/jdk.internal.loader.BuiltinClassLoader.loadClass(BuiltinClassLoader.java:641)
      at java.base/jdk.internal.loader.ClassLoaders$AppClassLoader.loadClass(ClassLoaders.java:188)
      at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:520)
      ... 54 more
  </code></pre>
  <p>Here is my Configuration Class:</p>
  <pre><code>@EnableWebSecurity
  public class WebSecurityConfig {
  
      private static final String[] allowed= {&quot;/&quot;,&quot;/register&quot;,&quot;/resendVerifyToken&quot;,&quot;/verifyRegistration&quot;};
      
      @Bean
      @Lazy
      public PasswordEncoder passwordEncoder() {
          return new BCryptPasswordEncoder(11);
      }
      
  
      @Bean
      @Order(Ordered.HIGHEST_PRECEDENCE)
      SecurityFilterChain securityFilterChain(HttpSecurity http) throws Exception {
          OAuth2AuthorizationServerConfiguration.applyDefaultSecurity(http);
          
          http.csrf()
          .and()
          .csrf().disable()
          .authorizeHttpRequests()
          .antMatchers(allowed)
          .permitAll()
          .antMatchers(&quot;/v1/**&quot;)
          .authenticated()
          .and()
          .oauth2Login(oauth2login -&gt; 
                       oauth2login.loginPage(&quot;/oauth2/authorization/api-client-oidc&quot;))
          .oauth2Client(Customizer.withDefaults()).formLogin(Customizer.withDefaults());
           
           
           return http.build();
          
          
      }
      @Autowired
      private CustomAuthenticationProvider customAuthenticationProvider;
  
     
  
      @Autowired
      public void bindAuthenticationProvider(AuthenticationManagerBuilder authenticationManagerBuilder) {
          authenticationManagerBuilder
                  .authenticationProvider(customAuthenticationProvider);
      }
  }
  </code></pre>
  <p>The Other Configuration Class is:</p>
  <p>@Configuration(proxyBeanMethods = false)
  public class AuthorizationServerConfig implements WebMvcConfigurer {</p>
  <pre><code>    @Override
      public void addInterceptors(InterceptorRegistry registry) {
      
          registry.addInterceptor(new LoggingInterceptor());
      }
  
      @Autowired
      @Lazy
      private PasswordEncoder passwordEncoder;
  
     
  
      @Bean
      public RegisteredClientRepository registeredClientRepository() {
          RegisteredClient registeredClient = RegisteredClient.withId(UUID.randomUUID().toString())
                  .clientId(&quot;api-client&quot;)
                  .clientSecret(passwordEncoder.encode(&quot;secret&quot;))
                  .clientAuthenticationMethod(ClientAuthenticationMethod.CLIENT_SECRET_BASIC)
                  .authorizationGrantType(AuthorizationGrantType.AUTHORIZATION_CODE)
                  .authorizationGrantType(AuthorizationGrantType.PASSWORD)
                  .authorizationGrantType(AuthorizationGrantType.REFRESH_TOKEN)
                  .redirectUri(&quot;http://127.0.0.1:8080/login/oauth2/code/api-client-oidc&quot;)
                  .redirectUri(&quot;http://127.0.0.1:8080/authorized&quot;)
                  .scope(OidcScopes.OPENID)
                  .scope(&quot;api.read&quot;)
                  .clientSettings(ClientSettings.builder().requireAuthorizationConsent(true).build())
                  .build();
  
  
          return new InMemoryRegisteredClientRepository(registeredClient);
      }
  
      @Bean
      public JWKSource&lt;SecurityContext&gt; jwkSource() {
          RSAKey rsaKey = generateRsa();
          JWKSet jwkSet = new JWKSet(rsaKey);
          return (jwkSelector, securityContext) -&gt; jwkSelector.select(jwkSet);
      }
  
      private static RSAKey generateRsa() {
          KeyPair keyPair = generateRsaKey();
          RSAPublicKey publicKey = (RSAPublicKey) keyPair.getPublic();
          RSAPrivateKey privateKey = (RSAPrivateKey) keyPair.getPrivate();
          return new RSAKey.Builder(publicKey)
                  .privateKey(privateKey)
                  .keyID(UUID.randomUUID().toString())
                  .build();
      }
  
      private static KeyPair generateRsaKey() {
          KeyPair keyPair;
          try {
              KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance(&quot;RSA&quot;);
              keyPairGenerator.initialize(2048);
              keyPair = keyPairGenerator.generateKeyPair();
          } catch (Exception ex) {
              throw new IllegalStateException(ex);
          }
          return keyPair;
      }
  
      @Bean
      public ProviderSettings providerSettings() {
          return ProviderSettings.builder()
                  .issuer(&quot;http://auth-server:9000&quot;)
                  .build();
      }
  }
  </code></pre>
  <p>The Application.Yml file is as:</p>
  <pre><code>server:
    port: 9000
  spring:
    security:
      oauth2:
        client:
          registration:
            api-client-oidc:
              provider: spring
              client-id: api-client
              client-secret: secret
              authorization-grant-type: authorization_code
              redirect-uri: &quot;http://127.0.0.1:8080/login/oauth2/code/{registrationId}&quot;
              scope: openid
              client-name: api-client-oidc
            api-client-authorization-code:
              provider: spring
              client-id: api-client
              client-secret: secret
              authorization-grant-type: authorization_code
              redirect-uri: &quot;http://127.0.0.1:8080/authorized&quot;
              scope: api.read
              client-name: api-client-authorization-code
          provider:
            spring:
              issuer-uri: http://auth-server:9000
    datasource:
      url: jdbc:mysql://127.0.0.1:3306/product-security?useSSL=false&amp;serverTimezone=UTC
      username: root
      password: root
      driver-class-name: com.mysql.cj.jdbc.Driver
    jpa: 
      properties:
        hibernate:
          dialect: 
              org:
                hibernate:
                  dialect:
                    MySQL5InnoDBDialect
      show-sql: true
      hibernate:
        ddl-auto: update
    main:
      allow-circular-references: true      
  logging:
    level:
      org:
        hibernate:
          SQL:
            DEBUG
      root:
        debug
      com:
        application:
          pms:
            debug  
  </code></pre>
  <p>The Dependencies Added are:</p>
  <pre><code>&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;
  &lt;project xmlns=&quot;http://maven.apache.org/POM/4.0.0&quot; xmlns:xsi=&quot;http://www.w3.org/2001/XMLSchema-instance&quot; xsi:schemaLocation=&quot;http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd&quot;&gt;
      &lt;modelVersion&gt;4.0.0&lt;/modelVersion&gt;
      &lt;parent&gt;
          &lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
          &lt;artifactId&gt;spring-boot-starter-parent&lt;/artifactId&gt;
          &lt;version&gt;2.6.5&lt;/version&gt;
          &lt;relativePath /&gt; &lt;!-- lookup parent from repository --&gt;
      &lt;/parent&gt;
      &lt;groupId&gt;com.springboot.app&lt;/groupId&gt;
      &lt;artifactId&gt;ProductManagementSystem&lt;/artifactId&gt;
      &lt;version&gt;0.0.1-SNAPSHOT&lt;/version&gt;
      &lt;name&gt;ProductManagementSystem&lt;/name&gt;
      &lt;description&gt;Product Management System&lt;/description&gt;
      &lt;properties&gt;
          &lt;java.version&gt;17&lt;/java.version&gt;
      &lt;/properties&gt;
      &lt;dependencies&gt;
          &lt;dependency&gt;
              &lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
              &lt;artifactId&gt;spring-boot-starter-data-jpa&lt;/artifactId&gt;
          &lt;/dependency&gt;
  
          &lt;dependency&gt;
              &lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
              &lt;artifactId&gt;spring-boot-starter-thymeleaf&lt;/artifactId&gt;
          &lt;/dependency&gt;
          &lt;dependency&gt;
              &lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
              &lt;artifactId&gt;spring-boot-starter-web&lt;/artifactId&gt;
          &lt;/dependency&gt;
          &lt;dependency&gt;
              &lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
              &lt;artifactId&gt;spring-boot-starter&lt;/artifactId&gt;
              &lt;exclusions&gt;
                  &lt;exclusion&gt;
                      &lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
                      &lt;artifactId&gt;spring-boot-starter-logging&lt;/artifactId&gt;
                  &lt;/exclusion&gt;
              &lt;/exclusions&gt;
          &lt;/dependency&gt;
  
          &lt;dependency&gt;
              &lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
              &lt;artifactId&gt;spring-boot-starter-log4j2&lt;/artifactId&gt;
  
          &lt;/dependency&gt;
          &lt;dependency&gt;
              &lt;groupId&gt;org.thymeleaf.extras&lt;/groupId&gt;
              &lt;artifactId&gt;thymeleaf-extras-springsecurity5&lt;/artifactId&gt;
          &lt;/dependency&gt;
  
          &lt;dependency&gt;
              &lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
              &lt;artifactId&gt;spring-boot-devtools&lt;/artifactId&gt;
              &lt;scope&gt;runtime&lt;/scope&gt;
              &lt;optional&gt;true&lt;/optional&gt;
          &lt;/dependency&gt;
          &lt;dependency&gt;
              &lt;groupId&gt;mysql&lt;/groupId&gt;
              &lt;artifactId&gt;mysql-connector-java&lt;/artifactId&gt;
              &lt;scope&gt;runtime&lt;/scope&gt;
          &lt;/dependency&gt;
          &lt;dependency&gt;
              &lt;groupId&gt;org.projectlombok&lt;/groupId&gt;
              &lt;artifactId&gt;lombok&lt;/artifactId&gt;
              &lt;optional&gt;true&lt;/optional&gt;
          &lt;/dependency&gt;
          &lt;dependency&gt;
              &lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
              &lt;artifactId&gt;spring-boot-starter-test&lt;/artifactId&gt;
              &lt;scope&gt;test&lt;/scope&gt;
          &lt;/dependency&gt;
          &lt;dependency&gt;
              &lt;groupId&gt;org.springframework.security&lt;/groupId&gt;
              &lt;artifactId&gt;spring-security-test&lt;/artifactId&gt;
              &lt;scope&gt;test&lt;/scope&gt;
          &lt;/dependency&gt;
          &lt;dependency&gt;
              &lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
              &lt;artifactId&gt;spring-boot-starter-validation&lt;/artifactId&gt;
          &lt;/dependency&gt;
          &lt;dependency&gt;
              &lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
              &lt;artifactId&gt;spring-boot-starter-security&lt;/artifactId&gt;
          &lt;/dependency&gt;
          &lt;!-- https://mvnrepository.com/artifact/org.springframework.security/spring-security-oauth2-jose --&gt;
          &lt;dependency&gt;
              &lt;groupId&gt;org.springframework.security&lt;/groupId&gt;
              &lt;artifactId&gt;spring-security-oauth2-jose&lt;/artifactId&gt;
          &lt;/dependency&gt;
  
          &lt;dependency&gt;
              &lt;groupId&gt;org.springframework.security&lt;/groupId&gt;
              &lt;artifactId&gt;spring-security-oauth2-authorization-server&lt;/artifactId&gt;
              &lt;version&gt;0.2.3&lt;/version&gt;
          &lt;/dependency&gt;
          
              &lt;dependency&gt;
          &lt;groupId&gt;org.springframework.security.oauth.boot&lt;/groupId&gt;
          &lt;artifactId&gt;spring-security-oauth2-autoconfigure&lt;/artifactId&gt;
          &lt;version&gt;2.1.0.RELEASE&lt;/version&gt;
      &lt;/dependency&gt;
  
      &lt;build&gt;
          &lt;plugins&gt;
              &lt;plugin&gt;
                  &lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
                  &lt;artifactId&gt;spring-boot-maven-plugin&lt;/artifactId&gt;
                  &lt;configuration&gt;
                      &lt;excludes&gt;
                          &lt;exclude&gt;
                              &lt;groupId&gt;org.projectlombok&lt;/groupId&gt;
                              &lt;artifactId&gt;lombok&lt;/artifactId&gt;
                          &lt;/exclude&gt;
                      &lt;/excludes&gt;
                  &lt;/configuration&gt;
              &lt;/plugin&gt;
          &lt;/plugins&gt;
      &lt;/build&gt;
  
  &lt;/project&gt;
  </code></pre>
  <p>Any Help and suggestions would be Appreciated.</p>