Skip to content

Commit 7d4192a

Browse files
authored
Merge pull request #27 from YAPP-Github/feat/PRODUCT-106
[Feat] CORS 설정
2 parents 9babd59 + d443b96 commit 7d4192a

File tree

3 files changed

+91
-0
lines changed

3 files changed

+91
-0
lines changed
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
package timeeat.config;
2+
3+
import org.springframework.beans.factory.annotation.Value;
4+
import org.springframework.context.annotation.Configuration;
5+
import org.springframework.http.HttpMethod;
6+
import org.springframework.web.servlet.config.annotation.CorsRegistry;
7+
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
8+
9+
@Configuration
10+
public class CorsConfig implements WebMvcConfigurer {
11+
12+
private final String[] corsOrigin;
13+
14+
public CorsConfig(@Value("${cors.origin}") String[] corsOrigin) {
15+
validate(corsOrigin);
16+
this.corsOrigin = corsOrigin;
17+
}
18+
19+
private void validate(String[] corsOrigin) {
20+
if (corsOrigin == null || corsOrigin.length == 0) {
21+
// TODO Initialize error 논의
22+
throw new RuntimeException("Initialization Error: CORS origin cannot be empty.");
23+
}
24+
for (String origin : corsOrigin) {
25+
if (origin == null || origin.isBlank()) {
26+
throw new RuntimeException("Initialization Error: CORS origin string cannot be blank.");
27+
}
28+
}
29+
}
30+
31+
@Override
32+
public void addCorsMappings(CorsRegistry registry) {
33+
registry.addMapping("/**")
34+
.allowedOriginPatterns(corsOrigin)
35+
.allowedMethods(
36+
HttpMethod.GET.name(),
37+
HttpMethod.POST.name(),
38+
HttpMethod.PUT.name(),
39+
HttpMethod.PATCH.name(),
40+
HttpMethod.DELETE.name()
41+
)
42+
.allowCredentials(true)
43+
.allowedHeaders("*");
44+
}
45+
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package timeeat.controller;
2+
3+
import static org.hamcrest.Matchers.containsString;
4+
5+
import org.junit.jupiter.api.Nested;
6+
import org.junit.jupiter.api.Test;
7+
import org.junit.jupiter.params.ParameterizedTest;
8+
import org.junit.jupiter.params.provider.ValueSource;
9+
import org.springframework.beans.factory.annotation.Value;
10+
11+
public class CorsTest extends BaseControllerTest {
12+
13+
@Value("${cors.origin}")
14+
private String corsOrigin;
15+
16+
@Nested
17+
class PreflightTest {
18+
19+
@ParameterizedTest
20+
@ValueSource(strings = {"GET", "POST", "PUT", "PATCH", "DELETE"})
21+
void CORS_preflight에서_허용된_origin의_요청을_정상적으로_처리할_수_있다(String method) {
22+
given()
23+
.header("Origin", corsOrigin)
24+
.header("Access-Control-Request-Method", method)
25+
.when().options("/")
26+
.then().statusCode(200)
27+
.headers("Access-Control-Allow-Origin", corsOrigin)
28+
.header("Access-Control-Allow-Methods", containsString(method));
29+
}
30+
31+
@Test
32+
void CORS_preflight에서_허용되지_않은_origin의_요청을_막을_수_있다() {
33+
String notAllowedOrigin = "https://not-allowed-origin.com";
34+
String allowedMethod = "GET";
35+
36+
given()
37+
.header("Origin", notAllowedOrigin)
38+
.header("Access-Control-Request-Method", allowedMethod)
39+
.when().options("/")
40+
.then().statusCode(403);
41+
}
42+
}
43+
}

src/test/resources/application.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,3 +23,6 @@ spring:
2323
defer-datasource-initialization: true
2424
flyway:
2525
enabled: false
26+
27+
cors:
28+
origin: "https://example.eat-da.com"

0 commit comments

Comments
 (0)