Skip to content

Commit

Permalink
[#2]✨Feat: 로그인 인증 인가 코드 추가
Browse files Browse the repository at this point in the history
  • Loading branch information
sumin220 committed Nov 19, 2024
1 parent d8e3d4c commit 2679842
Showing 1 changed file with 27 additions and 0 deletions.
27 changes: 27 additions & 0 deletions src/main/java/univ/yesummit/global/auth/util/SecurityUtil.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package univ.yesummit.global.auth.util;

import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.oauth2.core.user.OAuth2User;

public class SecurityUtil {

public static String getLoginUsername() {
Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
if (authentication == null || !authentication.isAuthenticated()) {
throw new IllegalStateException("User is not authenticated");
}

Object principal = authentication.getPrincipal();
if (principal instanceof UserDetails) {
return ((UserDetails) principal).getUsername();
} else if (principal instanceof OAuth2User) {
return ((OAuth2User) principal).getAttribute("name"); // 필요에 따라 변경 가능
} else if (principal instanceof String) {
return (String) principal;
}

throw new IllegalStateException("Unknown principal type");
}
}

0 comments on commit 2679842

Please sign in to comment.