title: Keycloak + spring cloud gateway + authentication and authorization
body: <p>I'm trying to use keycloak for authentication and also authorization in the spring cloud gateway itself. But I'm getting the below error.</p>
  <pre><code>Caused by: org.springframework.beans.factory.BeanCreationException: 
  Error creating bean with name 'springSecurityFilterChain' defined in class path resource [poc/apigateway/SecurityConfig.class]: 
  Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: 
  Failed to instantiate [org.springframework.security.web.server.SecurityWebFilterChain]: 
  Factory method 'springSecurityFilterChain' threw exception; nested exception is java.lang.IllegalArgumentException: 
  clientRegistrationRepository cannot be null
  </code></pre>
  <p>My application.properties</p>
  <pre><code>spring.application.name=api-gateway
  
  spring.cloud.gateway.default-filters=TokenRelay
  
  spring.cloud.gateway.routes[0].id=product-service
  spring.cloud.gateway.routes[0].uri=http://localhost:8009
  spring.cloud.gateway.routes[0].predicates[0]=Path=/api/product
  
  spring.cloud.gateway.routes[1].id=order-service
  spring.cloud.gateway.routes[1].uri=http://localhost:8008
  spring.cloud.gateway.routes[1].predicates[0]=Path=/api/order
  
  spring.security.oauth2.client.provider.keycloak.issuer-uri=http://localhost:8080/auth/realms/microservice-realm
  spring.security.oauth2.client.registration.spring-cloud-gateway-client.client-id=spring-cloud-gateway-client
  spring.security.oauth2.client.registration.spring-cloud-gateway-client.client-secret=d1b3670c-f1c3-480c-9cda-8e107aec7d5b
  spring.security.oauth2.client.registration.spring-cloud-gateway-client.scope=openid, profile, roles
  spring.security.oauth2.client.registration.spring-cloud-gateway-client.authorization-grant-type=authorization_code
  spring.security.oauth2.client.registration.spring-cloud-gateway-client.redirect-uri=http://localhost:8005/login/oauth2/code/spring-cloud-gateway-client
  
  spring.security.oauth2.resourceserver.jwt.issuer-uri=http://localhost:8080/auth/realms/microservice-realm
  
  keycloak.realm=microservice-realm
  keycloak.resource=spring-cloud-gateway-client
  keycloak.auth-server-url=http://localhost:8080/auth
  keycloak.public-client=true
  
  keycloak.securityConstraints[0].authRoles[0]=app-user
  keycloak.securityConstraints[0].securityCollections[0].patterns[0]=/api/*
  </code></pre>
  <p>My build.gradle</p>
  <pre><code>plugins {
      id 'org.springframework.boot' version '2.5.4'
      id 'io.spring.dependency-management' version '1.0.11.RELEASE'
      id 'java'
  }
  
  group = 'poc'
  version = '0.0.1-SNAPSHOT'
  sourceCompatibility = '1.8'
  
  repositories {
      mavenCentral()
  }
  
  ext {
      set('springCloudVersion', &quot;2020.0.3&quot;)
      set('keycloakVersion', '4.8.3.Final')
  }
  
  dependencies {
  
      implementation 'org.springframework.cloud:spring-cloud-starter-gateway'
      implementation 'org.springframework.boot:spring-boot-starter-oauth2-client'
      implementation 'org.springframework.boot:spring-boot-starter-security'
      testImplementation 'org.springframework.boot:spring-boot-starter-test'
      testImplementation 'org.springframework.security:spring-security-test'
  
      // Keycloak
      implementation 'org.keycloak:keycloak-spring-boot-starter'
  }
  
  dependencyManagement {
      imports {
          mavenBom &quot;org.springframework.cloud:spring-cloud-dependencies:${springCloudVersion}&quot;
          mavenBom &quot;org.keycloak.bom:keycloak-adapter-bom:${keycloakVersion}&quot;
      }
  }
  
  test {
      useJUnitPlatform()
  }
  </code></pre>
  <p>My SecurityConfig.java</p>
  <pre><code>@Configuration
  @EnableWebFluxSecurity
  public class SecurityConfig {
  
      @Bean
      public SecurityWebFilterChain springSecurityFilterChain(ServerHttpSecurity http) {
          http.authorizeExchange(exchanges -&gt; exchanges.anyExchange().authenticated())
                  .oauth2Login(withDefaults());
          http.csrf().disable();
          return http.build();
      }
  }
  </code></pre>
  <p>The issue comes when the application is booting up. What I'm trying to do here is do the authentication and role bases authorization in the API gateway itself.</p>