Skip to content

Commit

Permalink
refactor: CorsConfig 설정 변경
Browse files Browse the repository at this point in the history
- 기존 WebMvcConfigurer 방식에서 CorsConfigurationSource 로 변경합니다.
- http://192.168.0.* 에서 오는 요청에 대해 모두 허용합니다.
  • Loading branch information
MinhoJJang committed May 3, 2024
1 parent 98d02d1 commit 7ba3796
Showing 1 changed file with 17 additions and 15 deletions.
32 changes: 17 additions & 15 deletions src/main/java/goorm/reinput/global/security/CorsConfig.java
Original file line number Diff line number Diff line change
@@ -1,24 +1,26 @@
package goorm.reinput.global.security;

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;
import org.springframework.web.util.pattern.PathPatternParser;
import java.util.Arrays;

@Configuration
public class CorsConfig implements WebMvcConfigurer {
public class CorsConfig {

// TODO: cors 설정. dev, prod 주소 추가 예정
// @Value("${cors.origin.development}")
// private String developmentOrigin;
//
// @Value("${cors.origin.production}")
// private String productionOrigin;
@Bean
public CorsConfigurationSource corsConfigurationSource() {
CorsConfiguration config = new CorsConfiguration();
config.setAllowedOriginPatterns(Arrays.asList("http://192.168.0.*"));
config.setAllowedMethods(Arrays.asList("GET", "POST", "PUT", "PATCH", "DELETE", "OPTIONS", "HEAD"));
config.setAllowedHeaders(Arrays.asList("Authorization", "Cache-Control", "Content-Type"));
config.setAllowCredentials(true);

@Override
public void addCorsMappings(CorsRegistry registry) {
registry.addMapping("/**")
.allowedMethods("OPTIONS", "HEAD", "GET", "POST", "PUT", "PATCH", "DELETE")
.allowCredentials(true)
.allowedOrigins("http://localhost:3000","https://2024-beotkkotthon-team-24-fe.vercel.app", "https://reinput.info", "https://www.reinput.info");
UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource(new PathPatternParser());
source.registerCorsConfiguration("/**", config);
return source;
}
}

0 comments on commit 7ba3796

Please sign in to comment.