Files
velocity-pay-springboot/src/main/java/zw/qantra/tm/utils/SecurityUtils.java
2026-06-17 01:11:48 +02:00

21 lines
881 B
Java

package zw.qantra.tm.utils;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.UserDetails;
public class SecurityUtils {
public static String getCurrentUsername() {
Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
if (authentication != null && authentication.isAuthenticated()) {
Object principal = authentication.getPrincipal();
if (principal instanceof UserDetails) {
return ((UserDetails) principal).getUsername();
} else {
return principal.toString(); // For cases where principal might be a String
}
}
return null; // Or throw an exception if the user is expected to be logged in
}
}