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

Fix(#64): cors 적용 에러 #65

Merged
merged 3 commits into from
Feb 6, 2025
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
1 change: 1 addition & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ services:
KAKAO_LOGIN_CLIENT_ID: ${KAKAO_LOGIN_CLIENT_ID}
KAKAO_LOGIN_CLIENT_SECRET: ${KAKAO_LOGIN_CLIENT_SECRET}
SPRING_PROFILES_ACTIVE: ${SPRING_PROFILES_ACTIVE}
TZ: Asia/Seoul
volumes:
- /var/log/jaknaeso:/var/log/jaknaeso
depends_on:
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package org.nexters.jaknaesoserver.config;

import java.util.List;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.cors.CorsConfiguration;
import org.springframework.web.cors.CorsConfigurationSource;
import org.springframework.web.cors.UrlBasedCorsConfigurationSource;

@Configuration
public class CorsConfig {

private static final String LOCAL_URL = "http://localhost:3000";
private static final String JAKNAESO_WEB_VERCEL_APP = "https://jaknaeso-web.vercel.app";

@Value("${cors.origins.api-doc}")
private String API_DOC_HOST;

@Bean
public CorsConfigurationSource corsConfigurationSource() {

final CorsConfiguration corsConfiguration = new CorsConfiguration();
corsConfiguration.setAllowedOrigins(List.of(API_DOC_HOST, LOCAL_URL, JAKNAESO_WEB_VERCEL_APP));
corsConfiguration.setAllowedMethods(List.of("GET", "POST", "PUT", "PATCH", "DELETE"));
corsConfiguration.addAllowedHeader("*");

UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
source.registerCorsConfiguration("/**", corsConfiguration);
return source;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import org.springframework.security.config.http.SessionCreationPolicy;
import org.springframework.security.web.SecurityFilterChain;
import org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter;
import org.springframework.web.cors.CorsConfigurationSource;

@RequiredArgsConstructor
@EnableWebSecurity
Expand All @@ -20,6 +21,7 @@ public class SecurityConfig {

private final SecurityExceptionHandler securityExceptionHandler;
private final JwtAuthFilter jwtAuthFilter;
private final CorsConfigurationSource corsConfigurationSource;

@Bean
public SecurityFilterChain filterChain(HttpSecurity httpSecurity) throws Exception {
Expand All @@ -32,6 +34,7 @@ public SecurityFilterChain filterChain(HttpSecurity httpSecurity) throws Excepti
.permitAll()
.anyRequest()
.authenticated())
.cors(corsConfigurer -> corsConfigurer.configurationSource(corsConfigurationSource))
Copy link
Member

Choose a reason for hiding this comment

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

이건 그냥 궁금해서 물어보는건데 제가 공식문서를 제대로 읽은건지 모르겠는데..기존 세팅에서

Suggested change
.cors(corsConfigurer -> corsConfigurer.configurationSource(corsConfigurationSource))
.cors(withDefaults())

쓰면 저희 Mvc 설정 따라가는걸까요 ??

https://docs.spring.io/spring-security/reference/servlet/integrations/cors.html
해당문서참고했습니다 ! PR머지 후 확인해봐도 될것같습니다

Copy link
Member Author

Choose a reason for hiding this comment

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

넵 감사합니다! 확인해볼게요!

Copy link
Member Author

@pythonstrup pythonstrup Feb 6, 2025

Choose a reason for hiding this comment

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

민혁님 말씀이 맞는 것 같아요. withDefaults()를 MVC 설정을 따라가는 것 같습니다. 이 메소드를 사용했으면 더 빨리 해결할 수 있었겠네요. 다만 CorsConfigurationSource를 사용하면 조금 더 세밀한 설정이 가능하다는 장점이 있어서 CorsConfigurationSource를 사용하는 게 좋다고 생각해요.
ex) API 엔드포인트별로 서로 다른 CORS 제어가 가능

.formLogin(AbstractHttpConfigurer::disable)
.logout(AbstractHttpConfigurer::disable)
.httpBasic(AbstractHttpConfigurer::disable)
Expand Down

This file was deleted.