From 728fbcdad63e7f5e5fe3c810f4f70a4e67c62800 Mon Sep 17 00:00:00 2001 From: Kai Moritz Date: Fri, 6 Jan 2023 01:45:58 +0100 Subject: [PATCH] feat: Allowed the frontend as origin for CORS --- .../chat/backend/ChatBackendApplication.java | 16 ++++++++++++++-- .../chat/backend/ChatBackendProperties.java | 1 + 2 files changed, 15 insertions(+), 2 deletions(-) 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"; } -- 2.20.1