Example usage for org.springframework.security.core Authentication getPrincipal

List of usage examples for org.springframework.security.core Authentication getPrincipal

Introduction

In this page you can find the example usage for org.springframework.security.core Authentication getPrincipal.

Prototype

Object getPrincipal();

Source Link

Document

The identity of the principal being authenticated.

Usage

From source file:org.helianto.security.internal.UserDetailsAdapter.java

/**
 * Convenience to retrieve user details from context.
 *///from   w ww  .  java  2  s  . c o  m
public static UserDetailsAdapter getUserDetailsFromContext() {
    Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
    if (authentication != null) {
        return (UserDetailsAdapter) authentication.getPrincipal();
    }
    return null;
}

From source file:com.minlia.cloud.framework.common.security.SpringSecurityUtil.java

/**
 * Gets the current user details.//from   w w w .jav a  2s .c o m
 *
 * @return the current user details or null if can't be retrieved.
 */
public static UserDetails getCurrentUserDetails() {
    final Authentication authentication = getCurrentAuthentication();
    if (authentication == null) {
        return null;
    }

    final Object principal = authentication.getPrincipal();
    if (principal == null) {
        return null;
    }
    if (principal instanceof UserDetails) {
        return (UserDetails) principal;
    }

    return null;
}

From source file:com.minlia.cloud.framework.common.security.SpringSecurityUtil.java

public static SpringSecurityPrincipal getCurrentPrincipal() {
    final Authentication currentAuthentication = getCurrentAuthentication();
    if (currentAuthentication == null) {
        return null;
    }//ww w  .  jav  a2 s  .  c o  m
    final Object principal = currentAuthentication.getPrincipal();
    if (principal == null) {
        return null;
    }

    return (SpringSecurityPrincipal) principal;
}

From source file:org.zkoss.reference.developer.spring.security.SecurityUtil.java

/**
 * Return the current Authentication object.
 *//*ww  w. ja  va 2s.c  om*/
public static User getUser() {
    final Authentication auth = SecurityContextHolder.getContext().getAuthentication();
    if (auth != null) {
        try {
            Object p = auth.getPrincipal();
            if (p instanceof User)
                return (User) p;
        } catch (RuntimeException e) {
            e.printStackTrace();
            throw e;
        }
    }
    return null;
}

From source file:com.task.springsec.SecurityUtil.java

/**
 * Return the current Authentication object.
 *//* w  ww .  j  a  va  2 s.  com*/
public static MyUser getUser() {
    final Authentication auth = SecurityContextHolder.getContext().getAuthentication();
    if (auth != null) {
        try {
            Object p = auth.getPrincipal();
            if (p instanceof MyUser)
                return (MyUser) p;
        } catch (RuntimeException e) {
            e.printStackTrace();
            throw e;
        }
    }
    return null;
}

From source file:nc.noumea.mairie.appock.core.security.SecurityUtil.java

/**
 * @return the current Authentication object.
 *//*w w  w.  ja v  a 2 s  .com*/
public static String getUser() {
    final Authentication auth = SecurityContextHolder.getContext().getAuthentication();
    if (auth != null) {
        try {
            Object p = auth.getPrincipal();
            if (p instanceof LdapUserDetails)
                return ((LdapUserDetails) p).getUsername();
        } catch (RuntimeException e) {
            e.printStackTrace();
            throw e;
        }
    }
    return null;
}

From source file:id.co.sigma.zk.spring.security.SecurityUtil.java

/**
 * Return the current Authentication object.
 */// w  w  w  .  j  a  va 2 s  . com
public static UserData getUser() {
    final Authentication auth = SecurityContextHolder.getContext().getAuthentication();
    if (auth != null) {
        try {
            Object p = auth.getPrincipal();
            if (p instanceof UserData)
                return (UserData) p;
        } catch (RuntimeException e) {
            e.printStackTrace();
            throw e;
        }
    }
    return null;
}

From source file:com.adnature.framework.web.util.Struts2Utils.java

public static String getLoginUserId() {
    String userId = "";
    Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
    if (authentication != null) {
        if (authentication.getPrincipal() != null && authentication.getPrincipal() instanceof UserContext) {
            UserContext user = (UserContext) authentication.getPrincipal();
            userId = user.getUsername();
        }//from   w  ww  .ja va2 s .  co m
    }
    return userId;
}

From source file:nc.noumea.mairie.annuairev2.saisie.core.security.SecurityUtil.java

/**
 * Return the current Authentication object.
 * @return /* www  .  j  a v a2 s .c o m*/
 */
public static String getUser() {
    final Authentication auth = SecurityContextHolder.getContext().getAuthentication();
    if (auth != null) {
        try {
            Object p = auth.getPrincipal();
            if (p instanceof LdapUserDetails)
                return ((LdapUserDetails) p).getUsername();
            else {
                if ("test".equalsIgnoreCase(ApplicationContextUtils.getActiveProfile())) {
                    // env. test
                    return auth.getName();
                }
            }
        } catch (RuntimeException e) {
            throw e;
        }
    }
    return null;
}

From source file:net.cristcost.study.services.ServiceTestUtil.java

private static void dumpSecurityInformation(PrintWriter writer, AuthenticationManager authenticationManager) {
    writer.println("### General Security Information ###");

    writer.println("Security Strategy is " + SecurityContextHolder.getContextHolderStrategy().toString());

    writer.println("Current Thread is " + Thread.currentThread().getName() + " ("
            + Thread.currentThread().getId() + ")");

    writer.println();//from   www  .  ja  v  a  2  s .  c o m

    if (authenticationManager != null) {
        writer.println("I've been injected with the AuthenticationManager");
    } else {
        writer.println("I've not been injected with the AuthenticationManager");
    }
    writer.println();

    Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
    if (authentication != null) {
        writer.println("There is an Authentication of type: " + authentication.getClass().getName());
        writer.println("Principal is of type: " + authentication.getPrincipal().getClass().getName()
                + " and is value is: " + authentication.getPrincipal().toString());
        for (GrantedAuthority ga : authentication.getAuthorities()) {
            writer.println(" - you have " + ga.getAuthority() + " authority");
        }

    } else {
        writer.println("There is no Authentication!");
    }
    writer.println();
}