diff --git a/src/main/kotlin/org/careerseekers/apientrypoint/config/CorsConfig.kt b/src/main/kotlin/org/careerseekers/apientrypoint/config/CorsConfig.kt new file mode 100644 index 0000000..ffa9ae1 --- /dev/null +++ b/src/main/kotlin/org/careerseekers/apientrypoint/config/CorsConfig.kt @@ -0,0 +1,40 @@ +package org.careerseekers.apientrypoint.config + +import org.springframework.beans.factory.annotation.Value +import org.springframework.context.annotation.Bean +import org.springframework.context.annotation.Configuration +import org.springframework.web.cors.CorsConfiguration +import org.springframework.web.cors.reactive.CorsWebFilter +import org.springframework.web.cors.reactive.UrlBasedCorsConfigurationSource + + +@Configuration +class CorsConfig { + + @Value("\${services.frontend.local}") + private lateinit var frontendLocalHost: String + + @Value("\${services.frontend.production}") + private lateinit var frontendProductionHost: String + + @Value("\${services.frontend.productionPattern}") + private lateinit var frontendProductionHostPattern: String + + @Bean + fun corsWebFilter(): CorsWebFilter { + val corsConfig = CorsConfiguration().apply { + addAllowedOrigin(frontendLocalHost) + addAllowedOrigin(frontendProductionHost) + addAllowedOriginPattern(frontendProductionHostPattern) + + allowedMethods = listOf("GET", "POST", "PUT", "DELETE", "OPTIONS") + allowedHeaders = listOf("Content-Type", "Authorization", "Accept", "Origin", "X-Requested-With") + allowCredentials = true + } + + val source = UrlBasedCorsConfigurationSource() + source.registerCorsConfiguration("/**", corsConfig) + + return CorsWebFilter(source) + } +} \ No newline at end of file diff --git a/src/main/resources/application.yaml b/src/main/resources/application.yaml index f668e0e..a67c817 100644 --- a/src/main/resources/application.yaml +++ b/src/main/resources/application.yaml @@ -8,6 +8,10 @@ services: events-service: ${EVENTS_SERVICE_URI} mail-service: ${MAIL_SERVICE_URI} file-service: ${FILE_SERVICE_URI} + frontend: + local: ${LOCAL_HOST} + production: ${PRODUCTION_HOST} + productionPattern: ${PRODUCTION_HOST_PATTERN} server: port: 8080 \ No newline at end of file