From e270b33df8630d6e15b65090d63ca59a9ab4dd36 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ren=C3=A9=20Meier?= Date: Thu, 19 Dec 2024 13:18:46 +0100 Subject: [PATCH] Add a possibility to configure CORS. --- README.md | 4 +++ .../api/WebConfig.java | 28 +++++++++++++++++++ 2 files changed, 32 insertions(+) create mode 100644 src/main/java/de/ipb_halle/massbank3_export_service/api/WebConfig.java diff --git a/README.md b/README.md index 09b44d4..2c13293 100644 --- a/README.md +++ b/README.md @@ -19,6 +19,10 @@ release. This directory needs to be configured in the environment variable git clone https://github.com/MassBank/MassBank-data.git export MB_DATA_DIRECTORY="./MassBank-data" ``` +To configure CORS you can set the environment variable `CORS_ALLOWED_ORIGINS`. +```bash +export CORS_ALLOWED_ORIGINS=http://localhost:3000 +``` ## Build and Run Build the service with Maven: diff --git a/src/main/java/de/ipb_halle/massbank3_export_service/api/WebConfig.java b/src/main/java/de/ipb_halle/massbank3_export_service/api/WebConfig.java new file mode 100644 index 0000000..8bdc7ae --- /dev/null +++ b/src/main/java/de/ipb_halle/massbank3_export_service/api/WebConfig.java @@ -0,0 +1,28 @@ +package de.ipb_halle.massbank3_export_service.api; + +import org.springframework.beans.factory.annotation.Value; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import org.springframework.lang.NonNull; +import org.springframework.web.servlet.config.annotation.CorsRegistry; +import org.springframework.web.servlet.config.annotation.WebMvcConfigurer; + +@Configuration +public class WebConfig { + + @Value("${cors.allowedOrigins:*}") + private String allowedOrigins; + + @Bean + public WebMvcConfigurer corsConfigurer() { + return new WebMvcConfigurer() { + @Override + public void addCorsMappings(@NonNull CorsRegistry registry) { + registry.addMapping("/**") + .allowedOrigins(allowedOrigins.split(",")) + .allowedMethods("GET", "POST", "PUT", "DELETE", "OPTIONS") + .allowedHeaders("*"); + } + }; + } +} \ No newline at end of file