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

com.github.iintelligas.util.UserUtil Maven / Gradle / Ivy

The newest version!
package com.github.iintelligas.util;

import com.github.iintelligas.persist.dto.User;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.UserDetails;

public class UserUtil
{
    public static User getLoggedinUser()
    {
        try
        {
            return (User) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        }
        catch (Exception e)
        {
            return new User("null", "null", "null", "null", "null");
        }
    }

    public static String getCurrentUser()
    {
        if (SecurityContextHolder.getContext() != null && SecurityContextHolder.getContext().getAuthentication() != null
                && SecurityContextHolder.getContext().getAuthentication().getPrincipal() != null)
        {
            return ((UserDetails) SecurityContextHolder.getContext().getAuthentication().getPrincipal()).getUsername();
        }
        else
        {
            return "Unresolved";
        }

    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy