Skip to content

Commit

Permalink
feat : corsConfig추가
Browse files Browse the repository at this point in the history
  • Loading branch information
sycuuui committed Apr 18, 2024
1 parent 33eaee9 commit 331136d
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 0 deletions.
1 change: 1 addition & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ dependencies {
// Spring Boot Default
implementation 'org.springframework.boot:spring-boot-starter-data-jpa'
implementation 'org.springframework.boot:spring-boot-starter-web'
implementation 'org.springframework.boot:spring-boot-starter-security'
testImplementation 'org.springframework.boot:spring-boot-starter-test'

// Lombok
Expand Down
40 changes: 40 additions & 0 deletions src/main/java/com/capic/server/global/config/CorsConfig.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package com.capic.server.global.config;

import lombok.RequiredArgsConstructor;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.web.cors.CorsConfiguration;
import org.springframework.web.cors.CorsConfigurationSource;
import org.springframework.web.cors.UrlBasedCorsConfigurationSource;

import java.util.List;
@Configuration
@EnableWebSecurity
@RequiredArgsConstructor
public class CorsConfig {
/**
* CORS 허용하도록 커스터마이징 진행
* @return - 변경된 CORS 정책 정보 반환
*/
@Bean
CorsConfigurationSource corsConfigurationSource() {
CorsConfiguration config = new CorsConfiguration();

// 인증정보 주고받도록 허용
config.setAllowCredentials(true);
// 허용할 주소
config.setAllowedOriginPatterns(List.of("*"));
// 허용할 HTTP Method
config.setAllowedMethods(List.of("GET", "POST", "PUT", "DELETE", "PATCH", "OPTIONS"));
// 허용할 헤더 정보
config.setAllowedHeaders(List.of("*"));
config.setExposedHeaders(List.of("*"));

UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
source.registerCorsConfiguration("/**", config);

return source;
}

}

0 comments on commit 331136d

Please sign in to comment.