Skip to content

Commit

Permalink
Fix: 메인페이지 비로그인시 작동하도록 수정
Browse files Browse the repository at this point in the history
  • Loading branch information
nohy6630 committed May 18, 2024
1 parent ba8da12 commit de9c6d4
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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"));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand Down

0 comments on commit de9c6d4

Please sign in to comment.