From 3106b3e94ad6e6c3e5ad64562adbc65bd2030394 Mon Sep 17 00:00:00 2001 From: Paulo Gustavo Veiga Date: Sat, 17 Feb 2024 18:16:46 -0800 Subject: [PATCH] Add cors configuration support. --- .../wisemapping/config/rest/WebConfig.java | 24 +++++++++++++++++++ .../wisemapping/rest/AccountController.java | 1 - .../wisemapping/rest/JwtAuthController.java | 1 - .../wisemapping/rest/MindmapController.java | 1 - wise-api/src/main/resources/application.yml | 1 + 5 files changed, 25 insertions(+), 3 deletions(-) create mode 100644 wise-api/src/main/java/com/wisemapping/config/rest/WebConfig.java diff --git a/wise-api/src/main/java/com/wisemapping/config/rest/WebConfig.java b/wise-api/src/main/java/com/wisemapping/config/rest/WebConfig.java new file mode 100644 index 00000000..923ec980 --- /dev/null +++ b/wise-api/src/main/java/com/wisemapping/config/rest/WebConfig.java @@ -0,0 +1,24 @@ +package com.wisemapping.config.rest; + +import org.jetbrains.annotations.NotNull; +import org.springframework.beans.factory.annotation.Value; +import org.springframework.context.annotation.Configuration; +import org.springframework.web.servlet.config.annotation.CorsRegistry; +import org.springframework.web.servlet.config.annotation.EnableWebMvc; +import org.springframework.web.servlet.config.annotation.WebMvcConfigurer; + +@Configuration +@EnableWebMvc +public class WebConfig implements WebMvcConfigurer { + @Value("${app.security.corsAllowedOrigins:}") + private String corsAllowedOrigins; + + @Override + public void addCorsMappings(@NotNull CorsRegistry registry) { + if (!corsAllowedOrigins.isEmpty()) { + registry.addMapping("/api/**") + .allowedOrigins(corsAllowedOrigins). + maxAge(3600); + } + } +} \ No newline at end of file diff --git a/wise-api/src/main/java/com/wisemapping/rest/AccountController.java b/wise-api/src/main/java/com/wisemapping/rest/AccountController.java index c15467fe..fa54ba4b 100644 --- a/wise-api/src/main/java/com/wisemapping/rest/AccountController.java +++ b/wise-api/src/main/java/com/wisemapping/rest/AccountController.java @@ -40,7 +40,6 @@ import java.util.List; @RestController @RequestMapping("/api/restful/account") @PreAuthorize("isAuthenticated() and hasRole('ROLE_USER')") -@CrossOrigin public class AccountController extends BaseController { @Qualifier("userService") @Autowired diff --git a/wise-api/src/main/java/com/wisemapping/rest/JwtAuthController.java b/wise-api/src/main/java/com/wisemapping/rest/JwtAuthController.java index 01202552..17d4fa5c 100644 --- a/wise-api/src/main/java/com/wisemapping/rest/JwtAuthController.java +++ b/wise-api/src/main/java/com/wisemapping/rest/JwtAuthController.java @@ -32,7 +32,6 @@ import org.springframework.security.authentication.UsernamePasswordAuthenticatio import org.springframework.web.bind.annotation.*; @RestController -@CrossOrigin @RequestMapping("/api/restful") public class JwtAuthController { diff --git a/wise-api/src/main/java/com/wisemapping/rest/MindmapController.java b/wise-api/src/main/java/com/wisemapping/rest/MindmapController.java index 348e287d..70d65d5c 100644 --- a/wise-api/src/main/java/com/wisemapping/rest/MindmapController.java +++ b/wise-api/src/main/java/com/wisemapping/rest/MindmapController.java @@ -48,7 +48,6 @@ import java.util.stream.Collectors; @RestController @RequestMapping("/api/restful/maps") -@CrossOrigin public class MindmapController extends BaseController { private final Logger logger = LogManager.getLogger(); diff --git a/wise-api/src/main/resources/application.yml b/wise-api/src/main/resources/application.yml index b1909729..d15a6f16 100644 --- a/wise-api/src/main/resources/application.yml +++ b/wise-api/src/main/resources/application.yml @@ -71,6 +71,7 @@ app: # Redirect to this url, this url must be configured in the google app {baseurl}/c/registration-google #security.oauth2.google.callbackUrl= security: + corsAllowedOrigins: https://dev.wisemapping.com oauth2: google: confirmUrl: https://oauth2.googleapis.com/token