feat: Allowed the frontend as origin for CORS
authorKai Moritz <kai@juplo.de>
Fri, 6 Jan 2023 00:45:58 +0000 (01:45 +0100)
committerKai Moritz <kai@juplo.de>
Sun, 15 Jan 2023 18:35:59 +0000 (19:35 +0100)
src/main/java/de/juplo/kafka/chat/backend/ChatBackendApplication.java
src/main/java/de/juplo/kafka/chat/backend/ChatBackendProperties.java

index 0700496..dbd12b0 100644 (file)
@@ -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()
        {
index 598e8ce..122ac61 100644 (file)
@@ -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";
 }