spring-bootspring-securityoktaspring-cloud-gateway

Spring gateway dependency causing clientRegistrationRepository cannot be null


The issue is coming because of spring gateway starter mvc, i am using webflux because gateway needs webflux and i am using okta for oauth2.0 implementation.

I created the same project demo with same files and configuration and it was running fine but when i create it again it is giving same clientRegistrationRepository cannot be null error. And i have also find why it is coming.

OAuth2LoginConfig.java

@Configuration
@EnableWebFluxSecurity
public class OAuth2LoginConfig {
    @Bean
    public SecurityWebFilterChain securityWebFilterChain(ServerHttpSecurity http) {
        http
            .authorizeExchange(authorize -> authorize
                .anyExchange().authenticated()
            )
            .oauth2Login(Customizer.withDefaults());

        return http.build();
    }
}

and here is my application.yml

server:
  port: 9098

spring:
  main:
    allow-bean-definition-overriding: true
 
  application:
    name: dummy-okta
    
eureka:
  instance:
    prefer-ip-address: true
  client:
    fetch-registry: true
    register-with-eureka: true
    service-url:
      defaultZone: http://localhost:8761/eureka

okta:
  oauth2:
    issuer: https://dev-27305929.okta.com/oauth2/default
    audience: api://default
    client-id: 0oahgmcygbKzqfnwk5d7
    client-secret: CJTkPspyDVwiNLTvtPWySF-R8P30yi2x8aWuy-272SXIwYlUcLzUZ93LSJjuj-Ev
    scopes: openid, profile, email, offline_access

and here is pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>3.2.6</version>
        <relativePath /> <!-- lookup parent from repository -->
    </parent>
    <groupId>okta-dummy</groupId>
    <artifactId>com.api.gateway</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>okta-dummy</name>
    <description>okta dummy test project</description>
    <properties>
        <java.version>17</java.version>
        <spring-cloud.version>2023.0.1</spring-cloud.version>
    </properties>
    <dependencies>
    
    <!-- okta security and spring security dependancy -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-security</artifactId>
        </dependency>
        <dependency>
            <groupId>com.okta.spring</groupId>
            <artifactId>okta-spring-boot-starter</artifactId>
            <version>3.0.6</version>
        </dependency>
        
        <!-- cloud gateway and webflux dependancy -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-webflux</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-gateway-mvc</artifactId>
        </dependency>
        
        
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
        </dependency>

        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>io.projectreactor</groupId>
            <artifactId>reactor-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>
    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>${spring-cloud.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <configuration>
                    <excludes>
                        <exclude>
                            <groupId>org.projectlombok</groupId>
                            <artifactId>lombok</artifactId>
                        </exclude>
                    </excludes>
                </configuration>
            </plugin>
        </plugins>
    </build>

</project>

same config same copy of this project is working but same project when i create or deploy previously is giving error what could be reason? classpath or dependency injection issue?

i tried to remove some dependencies one by one and found out it is because of gateway starter dependency


Solution

  • i am using webflux because gateway needs webflux

    That's wrong for spring-cloud-starter-gateway-mvc which is the synchronised version of Spring Cloud Gateway (the suffix in its name indicates it is based on WebMVC and not WebFlux).

    Either use spring-cloud-starter-gateway or change your security configuration to servlets one (SecurityFilterChain, not SecurityWebFilterChain).