Skip to content

Commit

Permalink
[BE] feat : RestTemplate CORS 설정 추가
Browse files Browse the repository at this point in the history
  • Loading branch information
320Hwany committed May 3, 2024
1 parent ec9b0fc commit b0ed193
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package moim_today.global.config;

import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpRequest;
import org.springframework.http.client.ClientHttpRequestExecution;
import org.springframework.http.client.ClientHttpRequestInterceptor;
import org.springframework.http.client.ClientHttpResponse;
import org.springframework.stereotype.Component;

import java.io.IOException;

@Component
public class CorsInterceptor implements ClientHttpRequestInterceptor {

@Override
public ClientHttpResponse intercept(HttpRequest request, byte[] body, ClientHttpRequestExecution execution) throws IOException {
HttpHeaders headers = request.getHeaders();
headers.add("Access-Control-Allow-Origin", "*");
headers.add("Access-Control-Allow-Methods", "*");
headers.add("Access-Control-Allow-Headers", "Content-Type, Authorization");

return execution.execute(request, body);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,16 @@
@Configuration
public class RestTemplateConfig {

private final CorsInterceptor corsInterceptor;

public RestTemplateConfig(final CorsInterceptor corsInterceptor) {
this.corsInterceptor = corsInterceptor;
}

@Bean
public RestTemplate restTemplate() {
return new RestTemplate();
RestTemplate restTemplate = new RestTemplate();
restTemplate.getInterceptors().add(corsInterceptor);
return restTemplate;
}
}

0 comments on commit b0ed193

Please sign in to comment.