-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: 기존 독립적이었던 Cors 설정을 Security에서 설정하도록 수정
- Loading branch information
Showing
2 changed files
with
20 additions
and
12 deletions.
There are no files selected for viewing
28 changes: 17 additions & 11 deletions
28
src/main/java/MusicPlatform/global/config/cors/CorsConfig.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,26 +1,32 @@ | ||
package MusicPlatform.global.config.cors; | ||
|
||
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.servlet.config.annotation.CorsRegistry; | ||
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer; | ||
import org.springframework.web.cors.CorsConfiguration; | ||
import org.springframework.web.cors.CorsConfigurationSource; | ||
import org.springframework.web.cors.UrlBasedCorsConfigurationSource; | ||
|
||
@Configuration | ||
public class CorsConfig implements WebMvcConfigurer { | ||
public class CorsConfig { | ||
|
||
@Value(value = "${cors.allow.origins}") | ||
private String[] allowedOrigins; | ||
|
||
@Value(value = "${cors.allow.methods}") | ||
private String[] allowedMethods; | ||
|
||
@Override | ||
public void addCorsMappings(CorsRegistry registry) { | ||
registry.addMapping("/**") | ||
.allowedOrigins(allowedOrigins) | ||
.allowedMethods(allowedMethods) | ||
.allowedHeaders("Origin", "Content-Type", "Accept", "Authorization") | ||
.allowCredentials(true) | ||
.maxAge(3600); | ||
@Bean | ||
public CorsConfigurationSource corsConfigurationSource() { | ||
UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource(); | ||
CorsConfiguration config = new CorsConfiguration(); | ||
config.setAllowedOrigins(List.of(allowedOrigins)); | ||
config.setAllowedMethods(List.of(allowedMethods)); | ||
config.setAllowedHeaders(List.of("Origin", "Content-Type", "Accept", "Authorization")); | ||
config.setAllowCredentials(true); | ||
config.setMaxAge(3600L); // preflight 요청에 대한 응답 캐싱 (1시간) | ||
source.registerCorsConfiguration("/**", config); | ||
return source; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters