-
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.
add: cors config and rest template config
- Loading branch information
Showing
2 changed files
with
64 additions
and
0 deletions.
There are no files selected for viewing
30 changes: 30 additions & 0 deletions
30
src/main/java/kr/co/helloplum/config/RestTemplateConfig.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 |
---|---|---|
@@ -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(); | ||
} | ||
} |
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 |
---|---|---|
@@ -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<String> 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 리퀘스트를 캐싱 | ||
} | ||
} |