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));