From f2e836056728171885ee6e1db528841b089cd577 Mon Sep 17 00:00:00 2001 From: Liam Date: Thu, 11 Apr 2024 00:33:10 +0100 Subject: [PATCH] allow cors from any domain --- src/main/java/cc.fascinated/Main.java | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/src/main/java/cc.fascinated/Main.java b/src/main/java/cc.fascinated/Main.java index 1b01d49..9058733 100644 --- a/src/main/java/cc.fascinated/Main.java +++ b/src/main/java/cc.fascinated/Main.java @@ -2,10 +2,15 @@ package cc.fascinated; import com.google.gson.Gson; import com.google.gson.GsonBuilder; +import lombok.NonNull; import lombok.SneakyThrows; import lombok.extern.log4j.Log4j2; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; +import org.springframework.context.annotation.Bean; +import org.springframework.web.cors.CorsConfiguration; +import org.springframework.web.servlet.config.annotation.CorsRegistry; +import org.springframework.web.servlet.config.annotation.WebMvcConfigurer; import java.io.File; import java.net.http.HttpClient; @@ -36,4 +41,18 @@ public class Main { SpringApplication.run(Main.class, args); // Start the application } + + @Bean + public WebMvcConfigurer corsConfigurer() { + return new WebMvcConfigurer() { + @Override + public void addCorsMappings(@NonNull CorsRegistry registry) { + // Allow all origins to access the API + registry.addMapping("/**") + .allowedOrigins("*") // Allow all origins + .allowedMethods("*") // Allow all methods + .allowedHeaders("*"); // Allow all headers + } + }; + } } \ No newline at end of file