21 lines
726 B
Java
21 lines
726 B
Java
package com.reservation.demo.common.security;
|
|
|
|
import com.reservation.demo.common.exception.BusinessException;
|
|
import com.reservation.demo.common.exception.ErrorCode;
|
|
import org.springframework.security.core.Authentication;
|
|
import org.springframework.security.core.context.SecurityContextHolder;
|
|
|
|
public final class SecurityUtils {
|
|
|
|
private SecurityUtils() {
|
|
}
|
|
|
|
public static Long getCurrentUserId() {
|
|
Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
|
|
if (authentication == null || authentication.getPrincipal() == null) {
|
|
throw new BusinessException(ErrorCode.UNAUTHORIZED);
|
|
}
|
|
|
|
return (Long) authentication.getPrincipal();
|
|
}
|
|
}
|