Skip to content

Commit

Permalink
Refactor: CORS 프론트 개발환경 추가 (#241)
Browse files Browse the repository at this point in the history
refactor: CORS 프론트 개발주소 추가
  • Loading branch information
GitJIHO authored Nov 15, 2024
1 parent eb18588 commit 76bafa5
Showing 1 changed file with 21 additions and 4 deletions.
25 changes: 21 additions & 4 deletions src/main/java/com/example/sinitto/common/config/WebConfig.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.example.sinitto.common.config;

import com.example.sinitto.common.interceptor.JwtInterceptor;
import com.example.sinitto.common.properties.KakaoProperties;
import org.apache.hc.client5.http.config.RequestConfig;
import org.apache.hc.client5.http.impl.classic.CloseableHttpClient;
import org.apache.hc.client5.http.impl.classic.HttpClientBuilder;
Expand All @@ -9,6 +10,7 @@
import org.springframework.boot.web.client.RestTemplateBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.HttpMethod;
import org.springframework.http.client.HttpComponentsClientHttpRequestFactory;
import org.springframework.web.client.RestTemplate;
import org.springframework.web.cors.CorsConfiguration;
Expand All @@ -25,10 +27,14 @@ public class WebConfig implements WebMvcConfigurer {
private static final int TIME_OUT_DURATION = 5;
private static final int MAX_OPEN_CONNECTIONS = 100;
private static final int CONNECTIONS_PER_IP_PORT_PAIR = 5;
private static final String DEV_SERVER_URL = "https://localhost:5173";

private final JwtInterceptor jwtInterceptor;
private final KakaoProperties kakaoProperties;

public WebConfig(JwtInterceptor jwtInterceptor) {
public WebConfig(JwtInterceptor jwtInterceptor, KakaoProperties kakaoProperties) {
this.jwtInterceptor = jwtInterceptor;
this.kakaoProperties = kakaoProperties;
}

@Bean
Expand Down Expand Up @@ -61,10 +67,21 @@ public CorsFilter corsFilter() {
UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
CorsConfiguration config = new CorsConfiguration();
config.setAllowCredentials(true);
config.addAllowedOriginPattern("https://www.sinitto.life");
config.addAllowedHeader("*");
config.addAllowedMethod("*");

config.addAllowedOriginPattern(kakaoProperties.frontUri());
config.addAllowedOriginPattern(DEV_SERVER_URL);

config.addAllowedMethod(HttpMethod.GET);
config.addAllowedMethod(HttpMethod.POST);
config.addAllowedMethod(HttpMethod.PUT);
config.addAllowedMethod(HttpMethod.DELETE);
config.addAllowedMethod(HttpMethod.OPTIONS);

config.addAllowedHeader("Authorization");
config.addAllowedHeader("Content-Type");

config.addExposedHeader("Authorization");

config.setMaxAge(3600L);
source.registerCorsConfiguration("/**", config);
return new CorsFilter(source);
Expand Down

0 comments on commit 76bafa5

Please sign in to comment.