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

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

Introduction

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

Prototype

public String getName();

Source Link

Document

Returns the name of this principal.

Usage

From source file:org.joyrest.oauth2.endpoint.TokenEndpoint.java

private static String getClientId(Principal principal) {
    Authentication client = (Authentication) principal;

    String clientId = client.getName();
    // Might be a client and user combined authentication
    if (client instanceof OAuth2Authentication) {
        clientId = ((OAuth2Authentication) client).getOAuth2Request().getClientId();
    }/*  www.ja  v  a2 s.  co m*/

    return clientId;
}

From source file:com.sshdemo.common.web.util.ContextUtil.java

public static String getUserName() {
    Authentication currentUser = SecurityContextHolder.getContext().getAuthentication();
    if (currentUser == null) {
        return "";
    }/*from  w  w  w.  ja va  2 s  .  c  om*/
    return currentUser.getName();
}

From source file:com.mothsoft.alexis.security.CurrentUserUtil.java

public static boolean isAuthenticated() {
    final Authentication auth = SecurityContextHolder.getContext().getAuthentication();
    return auth != null && auth.isAuthenticated() && !"anonymousUser".equals(auth.getName());
}

From source file:com.foilen.smalltools.tools.TombstoneTools.java

/**
 * Add a warning log entry telling which user in Spring Security is using it.
 *
 * @param id// ww  w  .  j  a  va 2 s  .c  om
 *            the id to output in the log (should be unique to know which one was used)
 * @param time
 *            the time when you added that entry (could be a date or the version number)
 */
public static void logWithUser(String id, String time) {

    SecurityContext securityContext = null;
    try {
        securityContext = SecurityContextHolder.getContext();
    } catch (Exception e) {
    }

    String user = null;
    Class<? extends Authentication> authClass = null;
    if (securityContext != null) {
        Authentication authentication = securityContext.getAuthentication();
        if (authentication != null) {
            authClass = authentication.getClass();
            user = authentication.getName();
        }
    }

    logger.warn("{} - AuthClass: {} - User: {}", id, authClass, user);
}

From source file:com.lll.util.SpringSecurityUtils.java

/**
 * ????, ?.//w w w.jav a 2s  . co  m
 */
public static String getCurrentUserName() {
    Authentication authentication = getAuthentication();
    if (authentication != null && authentication.getPrincipal() != null) {
        return authentication.getName();
    }
    return "";
}

From source file:fr.xebia.audit.Auditor.java

/**
 * <p>//from w w w .j  av a 2s.  co  m
 * Emmits the audit message : <code>
 * "$date{yyyy-MM-dd'T'HH:mm:ss.SSSZZ} ${message} by ${spring-security-user}|anonymous [coming from ${remote-address}]"</code>.
 * <p>
 * <p>
 * If the Spring Security authentication is <code>null</code>, 'anonymous'
 * is emmitted.
 * </p>
 * <p>
 * If the Spring Security authentication details are
 * {@link WebAuthenticationDetails}, the incoming
 * {@link WebAuthenticationDetails#getRemoteAddress()} is emmitted.
 * </p>
 * 
 * @param message
 *            message to audit
 * @see SecurityContextHolder#getContext()
 */
public static void audit(String message) {
    if (message == null) {
        message = "";
    }
    StringBuilder msg = new StringBuilder(40 + message.length());

    SimpleDateFormat simpleDateFormat = (SimpleDateFormat) dateFormatPrototype.clone();
    msg.append(simpleDateFormat.format(new Date()));

    msg.append(" ").append(message).append(" by ");

    Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
    if (authentication == null) {
        msg.append("anonymous");
    } else {
        msg.append(authentication.getName());
        if (authentication.getDetails() instanceof WebAuthenticationDetails) {
            WebAuthenticationDetails details = (WebAuthenticationDetails) authentication.getDetails();
            msg.append(" coming from " + details.getRemoteAddress());
        }
    }
    auditLogger.info(msg.toString());
}

From source file:com.sjtu.onlinelibrary.util.SpringSecurityUtils.java

/**
 * ????, ?./*from  ww w .  j a va  2  s .  c o m*/
 */
public static String getCurrentUserName() {
    Authentication authentication = getAuthentication();

    if (authentication == null || authentication.getPrincipal() == null) {
        return "";
    }
    return authentication.getName();
}

From source file:com.rosy.bill.security.SpringSecurityUtils.java

/**
 * ????, ?./* www.  ja  v  a  2 s  .  c  o  m*/
 */
public static String getCurrentUserName() {
    Authentication authentication = getAuthentication();

    if (authentication == null || authentication.getPrincipal() == null) {
        return "";
    }

    return authentication.getName();
}

From source file:com.rosy.bill.security.SpringSecurityUtils.java

/**
 * ????, ?./* ww  w  .j  av a 2  s .  c  o m*/
 */
public static String getCurrentAccountName() {
    Authentication authentication = getAuthentication();

    if (authentication == null || authentication.getPrincipal() == null) {
        return "";
    }

    return authentication.getName();
}

From source file:com.sonymobile.backlogtool.Util.java

/**
 * Gets the username of the active user.
 * @return username or null if the user is not logged in
 *//*  ww w .  j  a  v  a 2s.  c om*/
public static String getUserName() {
    Authentication auth = SecurityContextHolder.getContext().getAuthentication();
    if (auth == null) {
        return null;
    }
    return auth.getName();
}