All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.eu.vooo.commons.security.util.UserUtils Maven / Gradle / Ivy

The newest version!
package org.eu.vooo.commons.security.util;

import org.eu.vooo.commons.security.dto.impl.XLoginUserResDTO;
import org.springframework.security.authentication.AnonymousAuthenticationToken;
import org.springframework.security.core.context.SecurityContextHolder;

import java.util.Objects;

/**
 * @author [email protected]
 */
public class UserUtils {

    public static String loginUsername() {
        return loginUser().getUsername();
    }

    public static String loginUserTenantId() {
        return loginUser().getTenantId();
    }

    public static XLoginUserResDTO loginUser() {
        if (Objects.nonNull(SecurityContextHolder.getContext())) {
            if (Objects.nonNull(SecurityContextHolder.getContext().getAuthentication())) {
                if (SecurityContextHolder.getContext().getAuthentication() instanceof AnonymousAuthenticationToken) {
                    return new XLoginUserResDTO();
                }
                if (Objects.nonNull(SecurityContextHolder.getContext().getAuthentication().getPrincipal())) {
                    return (XLoginUserResDTO) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
                }
            }
        }
        return new XLoginUserResDTO();
    }

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy