From: Kai Moritz Date: Fri, 6 Jan 2023 00:45:58 +0000 (+0100) Subject: feat: Allowed the frontend as origin for CORS X-Git-Tag: TEST~52 X-Git-Url: http://juplo.de/gitweb/?a=commitdiff_plain;h=0e138bf2a9e7e6bc659ee62a182d8462c1ba7d0d;p=demos%2Fkafka%2Fchat feat: Allowed the frontend as origin for CORS --- diff --git a/src/main/java/de/juplo/kafka/chat/backend/ChatBackendApplication.java b/src/main/java/de/juplo/kafka/chat/backend/ChatBackendApplication.java index 0700496e..dbd12b00 100644 --- a/src/main/java/de/juplo/kafka/chat/backend/ChatBackendApplication.java +++ b/src/main/java/de/juplo/kafka/chat/backend/ChatBackendApplication.java @@ -6,18 +6,30 @@ import jakarta.annotation.PreDestroy; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; -import org.springframework.context.annotation.Bean; +import org.springframework.web.reactive.config.CorsRegistry; +import org.springframework.web.reactive.config.WebFluxConfigurer; import reactor.core.publisher.Flux; @SpringBootApplication -public class ChatBackendApplication +public class ChatBackendApplication implements WebFluxConfigurer { + @Autowired + ChatBackendProperties properties; @Autowired ChatHome chatHome; @Autowired StorageStrategy storageStrategy; + + @Override + public void addCorsMappings(CorsRegistry registry) + { + registry + .addMapping("/**") + .allowedOrigins(properties.getAllowedOrigins()); + } + @PreDestroy public void onExit() { diff --git a/src/main/java/de/juplo/kafka/chat/backend/ChatBackendProperties.java b/src/main/java/de/juplo/kafka/chat/backend/ChatBackendProperties.java index 598e8ce0..122ac619 100644 --- a/src/main/java/de/juplo/kafka/chat/backend/ChatBackendProperties.java +++ b/src/main/java/de/juplo/kafka/chat/backend/ChatBackendProperties.java @@ -13,4 +13,5 @@ import java.nio.file.Paths; public class ChatBackendProperties { private String datadir = Paths.get(System.getProperty("java.io.tmpdir"),"chat", "backend").toString(); + private String allowedOrigins = "http://localhost:4200"; }