자신의 글만 읽는법
-로그인된 사용자가 자신의 글을 확인하려면, 그 사용자의 ID나 사용자명 등으로 글을 필터링 할 수 있다.
-서버 측에서 사용자 인증 정보를 확인하고, 해당 사용자가 작성한 글만을 반환하도록 하는 로직을 구현함으로써 가능하다.
-Spring Security - -- Authentication 객체를 통해 현재 로그인된 사용자의 정보를 얻을 수 있다.
-uthentication 객체는 SecurityContextHolder를 통해 접근할 수 있으며, 이 객체를 통해 로그인된 사용자의 세부 정보를 얻을 수 있다. import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.Authentication;
.
Authentication auth = SecurityContextHolder.getContext().getAuthentication();
String username = auth.getName(); // 현재 로그인된 사용자의 이름을 얻습니다.
.
// 이제 username을 이용하여 해당 사용자가 작성한 글만을 찾아서 반환하는 로직을 구현합니다.
List<Post> posts = postRepository.findByUsername(username);