
Spring으로 백을 만들고 react로 프론트를 만들기 위해 연결 테스트를 하던 중 cors문제가 발생했다.
부트캠프에서 프로젝트로 했던 조합이었기에 우리 대장님이 만들어준 코드를 보며 복습하고 모르는 점을 정리하기로 했다.
CORS(Cross-Origin Resource Sharing)
CORS란 도메인이 다른 서버끼리 리소스를 주고 받을 때 보안을 위해 설정된 정책이다.
React 프론트(3000)와 Spring(8090) 이기 때문에 포트가 달라 서로 다른 CORS 위반 에러가 발생한다.
다른 Origin을 갖고있는 react와 spring이 통신해주기 위해 설정이 필요함을 알았다.
@Configuration
public class WebMvcConfig implements WebMvcConfigurer {
@Override
public void addCorsMappings(CorsRegistry registry) {
registry.addMapping("/api/**")
.allowedOrigins("http://localhost:3000/")
.allowedMethods("*")
.allowedHeaders("*")
.allowCredentials(true)
.maxAge(3600);
}
}
addMapping("/api/**") : CORS를 적용할 URL패턴을 정의한다. (/api/~ 로 들어오는 URL에 적용한다).allowedOrigins("http://localhost:3000/") : 자원 공유를 허락할 Origin설정(현재는 프론트 서버만 허락).allowedMethods("*") : 모든 HTTP메서드를 허용한다..allowedHeaders("*") : 모든 HTTP헤더를 허용한다..allowCredentials(true) : 쿠키와 인증 정보를 포함한 크로스 도메인 요청을 허용한다..maxAge(3600); : CORS설정이 캐시되는 시간을 3600초(1시간)으로 설정import axios from "axios";
const apiInstance = axios.create({
baseURL: "http://localhost:8090",
headers: {
"Content-Type": "application/json",
},
// timeout: 10000,
withCredentials: true,
});
export const HttpGet = async (url, params = null) => {
try {
const response = await apiInstance.get(url, { params });
// console.log("GET request sent to:", response.config.url);
return response.data;
} catch (error) {
console.error("Error in HttpGet:", error);
throw error;
}
};
import { HttpGet } from "./service/httpService";
import React from "react";
import { useState, useEffect } from "react";
function App() {
const [hello, setHello] = useState('')
useEffect(() => {
HttpGet("/api/hello").then((data)=>{
if(data) {
setHello(data);
}
});
}, []);
return (
<div>
데이터 가져오기 = {hello}
</div>
);
}
export default App;
@RestController
public class MemberController {
@GetMapping("/api/hello")
public String test() {
return "Hello, world!";
}
}
가져와 졌다. 성공....
