How to add CorsConfiguration in Eureka Discovery service in reactive springboot?

Viewed 15

I have a Eureka server deployed in Springboot. I have enabled spring security with this maven dependency and yaml property for username/password:

<dependency>
 <groupId>org.springframework.boot</groupId>
 <artifactId>spring-boot-starter-security</artifactId> <!-- Provide basic auth for Eureka client registration -->
</dependency>

The following block shows my springboot version and webflux usage:

<parent>
 <groupId>org.springframework.boot</groupId>
 <artifactId>spring-boot-starter-parent</artifactId>
 <version>2.7.3</version>
 <relativePath/> <!-- lookup parent from repository -->
</parent>

<groupId>me.sonam</groupId>
<artifactId>discovery-service</artifactId>
<version>1.0-SNAPSHOT</version>
<name>eureka-service</name>
<description>Eureka Discovery Service</description>
<properties>
 <java.version>16</java.version>
 <spring-cloud.version>2021.0.4</spring-cloud.version>
</properties>

<dependencies>
 <dependency>
  <groupId>org.springframework.cloud</groupId>
  <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
 </dependency>

  <dependency>
   <groupId>org.springframework.boot</groupId>
   <artifactId>spring-boot-starter-webflux</artifactId>
  </dependency>

This is how I set my basic/auth security with username/password:

spring:
  security:
    user:
      name: ${EUREKA_USER}
      password: ${EUREKA_PASSWORD}

I have set CorsConfiguration as follows:

@EnableEurekaServer
@SpringBootApplication
public class EurekaServiceApplication {
    private static final Logger LOG = LoggerFactory.getLogger(EurekaServiceApplication.class);

    public static void main(String[] args) {
        SpringApplication.run(EurekaServiceApplication.class, args);
    }


    @Bean
    CorsConfigurationSource corsConfigurationSource() {
        CorsConfiguration corsConfig = new CorsConfiguration();
        corsConfig.setAllowedOrigins(List.of("*"));
        corsConfig.setMaxAge(3600L);
        corsConfig.addAllowedMethod("*");
        corsConfig.addAllowedHeader("*");

        UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
        source.registerCorsConfiguration("/**", corsConfig);
        return source;
    }
}

When I build my project with mvn clean install I get the following error:

 Bean named 'corsConfigurationSource' is expected to be of type 'org.springframework.web.cors.CorsConfigurationSource' but was actually of type 'org.springframework.web.cors.reactive.UrlBasedCorsConfigurationSource'
1 Answers

Replace your

import  org.springframework.web.cors.CorsConfigurationSource

with

import  org.springframework.web.cors.reactive.CorsConfigurationSource

Spring Framework has 2 classes named CorsConfigurationSource, one inside the reactive package and one in the outer package. Also the same for UrlBasedCorsConfigurationSource. But you have imported CorsConfigurationSource from outer(non reactive) package and UrlBasedCorsConfigurationSource from the reactive package. Therefore the issue you have.

Related