You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
|
|
package com.example.demo.Util;
import com.example.demo.domain.entity.Admin; import org.springframework.security.core.Authentication; import org.springframework.security.core.context.SecurityContextHolder;
// com.example.demo.utils.SecurityUtils.java
public class SecurityUtils {
public static String getCurrentUsername() { Authentication authentication = SecurityContextHolder.getContext().getAuthentication(); if (authentication == null || !authentication.isAuthenticated()) { return "anonymous"; } return authentication.getName(); }
public static Integer getCurrentUserId() { Authentication authentication = SecurityContextHolder.getContext().getAuthentication(); if (authentication == null || !authentication.isAuthenticated()) { return null; } if (authentication.getPrincipal() instanceof Admin userDetails) { return userDetails.getId(); } return null; } }
|