move cors to the Config

This commit is contained in:
Lee 2024-04-13 14:07:54 +01:00
parent 52d89a6d9f
commit 586539d810
2 changed files with 19 additions and 14 deletions

@ -40,18 +40,4 @@ public class Main {
SpringApplication.run(Main.class, args); // Start the application SpringApplication.run(Main.class, args); // Start the application
} }
@Bean
public WebMvcConfigurer configureCors() {
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
}
};
}
} }

@ -2,11 +2,15 @@ package cc.fascinated.config;
import jakarta.annotation.PostConstruct; import jakarta.annotation.PostConstruct;
import lombok.Getter; import lombok.Getter;
import lombok.NonNull;
import lombok.extern.log4j.Log4j2; import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value; import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.Environment; import org.springframework.core.env.Environment;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
@Getter @Log4j2 @Getter @Log4j2
@Configuration @Configuration
@ -27,8 +31,23 @@ public class Config {
@PostConstruct @PostConstruct
public void onInitialize() { public void onInitialize() {
INSTANCE = this; INSTANCE = this;
String environmentProperty = environment.getProperty("ENVIRONMENT", "development"); String environmentProperty = environment.getProperty("ENVIRONMENT", "development");
production = environmentProperty.equalsIgnoreCase("production"); // Set the production mode production = environmentProperty.equalsIgnoreCase("production"); // Set the production mode
log.info("Server is running in {} mode", production ? "production" : "development"); log.info("Server is running in {} mode", production ? "production" : "development");
} }
@Bean
public WebMvcConfigurer configureCors() {
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
}
};
}
} }