Cookie, Local Storage, Session Storage중 하나에 저장하여
"로그인을 유지"하는 것이 아니였다.
프론트에서 담을 공간을 정하는 방식.
이러한 방식으로 진행된다.
Frontend에서 Cookie를 사용하기 위해 react-cookie 라이브러리를 사용하였다.
설치 npm install react-cookie
import {Cookies} from "react-cookie";
const cookies = new Cookies();
export const setCookie = (name, value, options) => {
return cookies.set(name, value, {...options});
}
export const getCookie = (name) => {
return cookies.get(name);
}
export const removeCookie = (name) => {
return cookies.remove(name);
}
import {useEffect, useState} from "react";
import {getCookie} from "./CookieUtil";
function UserList() {
const [users, setUsers] = useState([]);
useEffect(() => {
fetch('/users', {
method: "GET",
headers: {
"Authorization": "Bearer " + getCookie("JWT")
}
})
.then(response => response.json())
.then(data => setUsers(data))
.catch(err => console.error("Error = ", err));
}, []);
return (
<div>
<table>
<thead>
<tr>
<th>id</th>
<th>student_id</th>
<th>password</th>
<th>email</th>
<th>name</th>
<th>sex</th>
<th>phone</th>
</tr>
</thead>
<tbody>
{users.map(user => (
<tr key={user.id}>
<td>{user.student_id}</td>
<td>{user.password}</td>
<td>{user.email}</td>
<td>{user.name}</td>
<td>{user.sex}</td>
<td>{user.phone}</td>
</tr>
))}
</tbody>
</table>
</div>
);
}
export default UserList;
@PostMapping("/loginForm")
public ResponseEntity<LoginResponseDTO> loginPost(@RequestBody @Valid LoginRequestDTO loginRequestDTO) {
String token = userService.login(loginRequestDTO.getStudent_id(), loginRequestDTO.getPassword());
if (token != null){
log.info("로그인 POST 1 [성공]");
return new ResponseEntity<>(new LoginResponseDTO(token), HttpStatus.OK);
}
else {
log.info("로그인 POST 2 [실패]");
// 401에러
return ResponseEntity.status(HttpStatus.UNAUTHORIZED).body(new LoginResponseDTO("애프리케이션 ID 또는 암호에 오류가 있습니다."));
}
}
교차 출처 리소스 공유를 뜻하는 CORS는, '서로 다른 출처에서 리소스를 공유하는 것'을 뜻한다.
fetch('http://localhost:8080/users’) -> cors오류 발생하는데
fetch(‘/users’) -> cors오류가 발생하지않음
왜? why?
동일 출처 정책 (Same-Origin Policy):
웹 브라우저는 보안상의 이유로 기본적으로 다른 출처 (Origin)의 리소스에 대한 접근을 제한합니다. 출처란 프로토콜 (HTTP, HTTPS), 호스트 (도메인), 포트로 구성됩니다. 따라서 두 개의 URL이 동일한 출처를 갖는다면 CORS 오류가 발생하지 않을 것입니다.
fetch('/users')는 현재 웹 페이지의 동일한 출처에 있는 리소스를 가져오려고 시도하므로 CORS 오류가 발생하지 않을 것입니다.
다른 출처에서 리소스 요청:
만약 fetch 함수를 사용하여 다른 출처의 리소스 (예: 다른 도메인, 다른 포트)에 접근하려고 한다면, 웹 브라우저는 이를 보안상의 이유로 차단합니다. 이것이 바로 CORS 정책입니다. 따라서 fetch('http://localhost:8080/users')와 같이 다른 도메인 또는 포트로 요청을 보내면, 브라우저는 보안 정책을 위반했다고 판단하고 CORS 오류를 발생시킵니다.
from chatGPT...
@Override
public void addCorsMappings(CorsRegistry registry) {
registry.addMapping("/**")
.allowedOrigins("http://localhost:3000") // React 앱의 도메인 주소
.allowedMethods("GET", "POST", "PUT", "DELETE")
.allowedHeaders("*")
.allowCredentials(true);
}
'
'
'
'
'
처음 Spring과 React를 연결할 때 찾아보다 보니 알게된 것들!
'
'
'
개강 이슈로 속도가 느려졌지만 앞으로 JWT를 refresh Token과 access Token으로 나누어 보는 작업을 해보려고 한다.👍