Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: CORS 설정 추가 #382

Merged
merged 1 commit into from
Apr 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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);
}
Comment on lines +64 to +73
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good~

}
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
Loading