From de9c6d4fd55364192ff936390c7118ddeb195dfd Mon Sep 17 00:00:00 2001 From: nohy6630 Date: Sat, 18 May 2024 16:04:31 +0900 Subject: [PATCH] =?UTF-8?q?Fix:=20=EB=A9=94=EC=9D=B8=ED=8E=98=EC=9D=B4?= =?UTF-8?q?=EC=A7=80=20=EB=B9=84=EB=A1=9C=EA=B7=B8=EC=9D=B8=EC=8B=9C=20?= =?UTF-8?q?=EC=9E=91=EB=8F=99=ED=95=98=EB=8F=84=EB=A1=9D=20=EC=88=98?= =?UTF-8?q?=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../domain/auth/jwt/SecurityContextProvider.java | 11 +++++++++-- .../backend/domain/user/service/UserService.java | 2 +- 2 files changed, 10 insertions(+), 3 deletions(-) diff --git a/src/main/java/com/meetup/teame/backend/domain/auth/jwt/SecurityContextProvider.java b/src/main/java/com/meetup/teame/backend/domain/auth/jwt/SecurityContextProvider.java index 513eef4..1f6b3f5 100644 --- a/src/main/java/com/meetup/teame/backend/domain/auth/jwt/SecurityContextProvider.java +++ b/src/main/java/com/meetup/teame/backend/domain/auth/jwt/SecurityContextProvider.java @@ -3,6 +3,7 @@ import com.meetup.teame.backend.global.exception.CustomException; import com.meetup.teame.backend.global.exception.ExceptionContent; import org.springframework.security.core.Authentication; +import org.springframework.security.core.GrantedAuthority; import org.springframework.security.core.context.SecurityContextHolder; import java.util.Objects; @@ -17,7 +18,13 @@ public static Long getAuthenticatedUserId() { return (Long) principal; } - public static Boolean isAuthenticated() { - return Objects.nonNull(SecurityContextHolder.getContext().getAuthentication()); + public static boolean isAnonymousUser() { + Authentication authentication = SecurityContextHolder.getContext().getAuthentication(); + if (authentication == null) { + return false; + } + return authentication.getAuthorities().stream() + .map(GrantedAuthority::getAuthority) + .anyMatch(role -> role.equals("ROLE_ANONYMOUS")); } } diff --git a/src/main/java/com/meetup/teame/backend/domain/user/service/UserService.java b/src/main/java/com/meetup/teame/backend/domain/user/service/UserService.java index 19072b1..012b07c 100644 --- a/src/main/java/com/meetup/teame/backend/domain/user/service/UserService.java +++ b/src/main/java/com/meetup/teame/backend/domain/user/service/UserService.java @@ -48,7 +48,7 @@ public class UserService { public ReadMainRes readMainPage() { Long userId = 5L; - if (SecurityContextProvider.isAuthenticated()) + if(!SecurityContextProvider.isAnonymousUser()) userId = SecurityContextProvider.getAuthenticatedUserId(); User user = userRepository.findById(userId) .orElseThrow(() -> new CustomException(ExceptionContent.NOT_FOUND_USER));