Skip to content

Commit e52fbd9

Browse files
committed
[FIX] : CORS에러 해결을 위한 허용 범위 수정
1 parent 9c2c0c4 commit e52fbd9

File tree

2 files changed

+20
-9
lines changed

2 files changed

+20
-9
lines changed

server/src/main/java/org/cecd/server/external/SecurityConfig.java

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
import org.springframework.web.cors.CorsConfiguration;
2525

2626
import java.io.IOException;
27+
import java.util.List;
2728

2829
@Configuration
2930
@EnableWebSecurity
@@ -41,10 +42,15 @@ public SecurityFilterChain securityFilterChain(HttpSecurity httpSecurity) throws
4142
httpSecurity
4243
.cors(cors -> cors.configurationSource(request -> {
4344
CorsConfiguration configuration = new CorsConfiguration();
44-
configuration.addAllowedOrigin("*");
45-
configuration.addAllowedMethod("*");
46-
configuration.addAllowedHeader("*");
47-
configuration.setAllowCredentials(true);
45+
// 명시적으로 허용할 Origin 설정
46+
configuration.setAllowedOrigins(List.of(
47+
"http://localhost:3000",
48+
"https://www.dgu1921.p-e.kr",
49+
"https://dgutestbed.netlify.app"
50+
));
51+
configuration.setAllowedMethods(List.of("GET", "POST", "PUT", "DELETE", "OPTIONS"));
52+
configuration.setAllowedHeaders(List.of("*"));
53+
configuration.setAllowCredentials(true); // Credentials 허용
4854
return configuration;
4955
}))
5056
.csrf(csrf -> csrf.disable()) // CSRF 비활성화
@@ -62,7 +68,6 @@ public SecurityFilterChain securityFilterChain(HttpSecurity httpSecurity) throws
6268
.authenticationEntryPoint(new AuthenticationEntryPoint() {
6369
@Override
6470
public void commence(HttpServletRequest request, HttpServletResponse response, AuthenticationException authException) throws IOException, ServletException {
65-
// API에서 인증 실패 시 에러를 그대로 출력
6671
if (!request.getRequestURI().contains("api")) {
6772
response.sendRedirect("/jwt-login/authentication-fail");
6873
} else {
@@ -85,3 +90,4 @@ public void handle(HttpServletRequest request, HttpServletResponse response, Acc
8590
return httpSecurity.build();
8691
}
8792
}
93+

server/src/main/java/org/cecd/server/external/WebConfig.java

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,14 @@ public class WebConfig implements WebMvcConfigurer {
1212

1313
@Override
1414
public void addCorsMappings(CorsRegistry registry) {
15-
registry.addMapping("/**") //어떤 URI로 들어오는 요청을 허용할 것인가?
16-
.allowedOrigins("*") // 모두 허용
15+
registry.addMapping("/**")
16+
.allowedOrigins(
17+
"http://localhost:3000",
18+
"https://www.dgu1921.p-e.kr",
19+
"https://dgutestbed.netlify.app"
20+
)
1721
.allowedMethods("GET", "POST", "PUT", "DELETE", "PATCH")
18-
.allowedHeaders("*");
22+
.allowedHeaders("*")
23+
.allowCredentials(true);
1924
}
20-
}
25+
}

0 commit comments

Comments
 (0)