Skip to content

Commit

Permalink
feat: CORS 설정 추가 (#382)
Browse files Browse the repository at this point in the history
(cherry picked from commit ab63fd0)
  • Loading branch information
Choi-JJunho committed May 9, 2024
1 parent 874454a commit 6b23953
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 0 deletions.
2 changes: 2 additions & 0 deletions src/main/java/in/koreatech/koin/KoinApplication.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.context.properties.ConfigurationPropertiesScan;
import org.springframework.scheduling.annotation.EnableScheduling;

@EnableScheduling
@SpringBootApplication
@ConfigurationPropertiesScan
public class KoinApplication {

public static void main(String[] args) {
Expand Down
12 changes: 12 additions & 0 deletions src/main/java/in/koreatech/koin/global/config/CorsProperties.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package in.koreatech.koin.global.config;

import java.util.List;

import org.springframework.boot.context.properties.ConfigurationProperties;

@ConfigurationProperties(prefix = "cors")
public record CorsProperties(
List<String> allowedOrigins
) {

}
12 changes: 12 additions & 0 deletions src/main/java/in/koreatech/koin/global/config/WebConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import org.springframework.context.annotation.Configuration;
import org.springframework.format.FormatterRegistry;
import org.springframework.web.method.support.HandlerMethodArgumentResolver;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

Expand All @@ -31,6 +32,7 @@ public class WebConfig implements WebMvcConfigurer {
private final IpAddressInterceptor ipAddressInterceptor;
private final ServerURLArgumentResolver serverURLArgumentResolver;
private final ServerURLInterceptor serverURLInterceptor;
private final CorsProperties corsProperties;

@Override
public void addInterceptors(InterceptorRegistry registry) {
Expand Down Expand Up @@ -59,4 +61,14 @@ public void addFormatters(FormatterRegistry registry) {
registry.addConverter(new BusStationEnumConverter());
registry.addConverter(new ImageUploadDomainEnumConverter());
}

@Override
public void addCorsMappings(CorsRegistry registry) {
registry.addMapping("/**")
.allowedOrigins(corsProperties.allowedOrigins().toArray(new String[0]))
.allowedMethods("GET", "POST", "PUT", "DELETE", "OPTIONS", "PATCH")
.allowedHeaders("*")
.allowCredentials(true)
.maxAge(3600);
}
}
4 changes: 4 additions & 0 deletions src/main/resources/application-test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -62,3 +62,7 @@ fcm:
url: koinAppUrl://

OPEN_API_KEY:

cors:
allowedOrigins:
- http://localhost:3000

0 comments on commit 6b23953

Please sign in to comment.