From 064ce1b15231c6f0c7f9f57004d9754f38ce5a4c Mon Sep 17 00:00:00 2001 From: minjikim Date: Tue, 31 Oct 2023 14:39:08 +0900 Subject: [PATCH] add: cors config and rest template config --- .../helloplum/config/RestTemplateConfig.java | 30 ++++++++++++++++ .../kr/co/helloplum/config/WebMvcConfig.java | 34 +++++++++++++++++++ 2 files changed, 64 insertions(+) create mode 100644 src/main/java/kr/co/helloplum/config/RestTemplateConfig.java create mode 100644 src/main/java/kr/co/helloplum/config/WebMvcConfig.java diff --git a/src/main/java/kr/co/helloplum/config/RestTemplateConfig.java b/src/main/java/kr/co/helloplum/config/RestTemplateConfig.java new file mode 100644 index 0000000..5c1b6f1 --- /dev/null +++ b/src/main/java/kr/co/helloplum/config/RestTemplateConfig.java @@ -0,0 +1,30 @@ +package kr.co.helloplum.config; + +import java.nio.charset.StandardCharsets; +import java.time.Duration; +import org.springframework.boot.web.client.RestTemplateBuilder; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import org.springframework.http.client.BufferingClientHttpRequestFactory; +import org.springframework.http.client.SimpleClientHttpRequestFactory; +import org.springframework.http.converter.StringHttpMessageConverter; +import org.springframework.web.client.RestTemplate; + +@Configuration +public class RestTemplateConfig { + + @Bean + public RestTemplateBuilder restTemplateBuilder() { + return new RestTemplateBuilder(); + } + + @Bean + public RestTemplate restTemplate() { + return restTemplateBuilder() + .requestFactory(() -> new BufferingClientHttpRequestFactory(new SimpleClientHttpRequestFactory())) + .setConnectTimeout(Duration.ofMillis(5000)) // connection-timeout + .setReadTimeout(Duration.ofMillis(5000)) // read-timeout + .additionalMessageConverters(new StringHttpMessageConverter(StandardCharsets.UTF_8)) + .build(); + } +} diff --git a/src/main/java/kr/co/helloplum/config/WebMvcConfig.java b/src/main/java/kr/co/helloplum/config/WebMvcConfig.java new file mode 100644 index 0000000..4b1f20a --- /dev/null +++ b/src/main/java/kr/co/helloplum/config/WebMvcConfig.java @@ -0,0 +1,34 @@ +package kr.co.helloplum.config; + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; +import org.springframework.context.annotation.Configuration; +import org.springframework.http.HttpMethod; +import org.springframework.web.servlet.config.annotation.CorsRegistry; +import org.springframework.web.servlet.config.annotation.WebMvcConfigurer; + +@Configuration +public class WebMvcConfig implements WebMvcConfigurer { + private final String localhost = "http://localhost:"; + private final int allowedPort = 3000; + private List allowedOrigins = new ArrayList<>(); + + // CORS + @Override + public void addCorsMappings(CorsRegistry registry) { + // 허용할 origin 목록 + allowedOrigins.add(localhost + allowedPort); + + registry.addMapping("/**") + .allowedOrigins(allowedOrigins.toArray(new String[allowedOrigins.size()])) + .allowedMethods( + HttpMethod.GET.name(), + HttpMethod.HEAD.name(), + HttpMethod.POST.name(), + HttpMethod.PUT.name(), + HttpMethod.PATCH.name(), + HttpMethod.DELETE.name()) + .maxAge(3000); // pre-flight 리퀘스트를 캐싱 + } +} \ No newline at end of file