-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
27 additions
and
0 deletions.
There are no files selected for viewing
27 changes: 27 additions & 0 deletions
27
src/main/java/univ/yesummit/global/auth/util/SecurityUtil.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"); | ||
} | ||
} |