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

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

Introduction

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

Prototype

Object getDetails();

Source Link

Document

Stores additional details about the authentication request.

Usage

From source file:at.ac.univie.isc.asio.security.AuthTools.java

/**
 * Extract the {@link Identity} from the authentication's details if present.
 *
 * @param context spring security context
 * @return the client's identity if one is present
 *///from www. j  a  v a 2 s.c o m
@Nonnull
public static Identity findIdentity(@Nonnull final SecurityContext context) {
    requireNonNull(context, "spring security context");
    final Authentication authentication = context.getAuthentication();
    if (authentication != null && authentication.getDetails() instanceof DelegatedCredentialsDetails) {
        final DelegatedCredentialsDetails details = (DelegatedCredentialsDetails) authentication.getDetails();
        return details.getCredentials();
    }
    return Identity.undefined();
}

From source file:de.chludwig.websec.saml2sp.springconfig.CurrentUserHandlerMethodArgumentResolver.java

private static SAMLCredential getSamlCredential(Authentication auth) {
    Object detailsObject = auth.getDetails();
    if (detailsObject instanceof SamlUserDetails) {
        SamlUserDetails samlUserDetails = (SamlUserDetails) detailsObject;
        return samlUserDetails.getSamlCredential();
    }// w  ww. j  a va  2 s .co  m
    return null;
}

From source file:com.wisemapping.security.Utils.java

@NotNull
public static User getUser(boolean forceCheck) {
    User result = null;//w  ww  . j  a v  a 2s  . c om
    final Authentication auth = SecurityContextHolder.getContext().getAuthentication();
    if (auth != null && auth.getDetails() != null) {
        final Object principal = auth.getPrincipal();
        if (principal != null && principal instanceof UserDetails) {
            result = ((UserDetails) principal).getUser();
        }
    }

    if (result == null && forceCheck) {
        throw new IllegalStateException("User could not be retrieved");
    }
    return result;
}

From source file:edu.wisc.web.security.portlet.primaryattr.PrimaryAttributeUtils.java

/**
 * @return The current user's primary attribute
 *///from   ww  w  .  j a v  a2 s  .  com
public static String getPrimaryId() {
    final SecurityContext context = SecurityContextHolder.getContext();
    final Authentication authentication = context.getAuthentication();

    final PrimaryAttributePortletAuthenticationDetails authenticationDetails = (PrimaryAttributePortletAuthenticationDetails) authentication
            .getDetails();
    return authenticationDetails.getPrimaryAttribute();
}

From source file:edu.wisc.portlet.hrs.web.EmplIdUtils.java

/**
 * @return The current user's EmplID// w ww . j  a v a2  s .  com
 */
public static String getEmplId() {
    final SecurityContext context = SecurityContextHolder.getContext();
    final Authentication authentication = context.getAuthentication();

    final PrimaryAttributePortletAuthenticationDetails authenticationDetails = (PrimaryAttributePortletAuthenticationDetails) authentication
            .getDetails();
    return authenticationDetails.getPrimaryAttribute();
}

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

/**
 * ??IP, ?.//from  ww  w.  j av a  2s  .  co  m
 */
public static String getCurrentUserIp() {
    Authentication authentication = getAuthentication();
    if (authentication != null) {
        Object details = authentication.getDetails();
        if (details instanceof WebAuthenticationDetails) {
            WebAuthenticationDetails webDetails = (WebAuthenticationDetails) details;
            return webDetails.getRemoteAddress();
        }
    }

    return "";
}

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

/**
 * ??IP, ?.//  w  w w .  j  ava  2s  .  co m
 */
public static String getCurrentUserIp() {
    Authentication authentication = getAuthentication();

    if (authentication == null) {
        return "";
    }

    Object details = authentication.getDetails();
    if (!(details instanceof WebAuthenticationDetails)) {
        return "";
    }

    WebAuthenticationDetails webDetails = (WebAuthenticationDetails) details;
    return webDetails.getRemoteAddress();
}

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

/**
 * <p>// w w w  . jav  a 2  s.  c om
 * 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:id.co.sigma.zk.spring.security.SecurityUtil.java

public static WebAuthenticationDetails getAuthDetails() {
    final Authentication auth = SecurityContextHolder.getContext().getAuthentication();
    if (auth != null) {
        try {//ww w.  j  a  v a  2 s.c  o m
            Object p = auth.getDetails();
            if (p instanceof WebAuthenticationDetails)
                return (WebAuthenticationDetails) p;
        } catch (RuntimeException e) {
            e.printStackTrace();
            throw e;
        }
    }
    return null;
}

From source file:org.zalando.stups.oauth2.spring.client.AccessTokenUtilsTest.java

@Test
public void testUserDetailsIsNotAMap() throws Exception {
    final Authentication userAuthentication = mock(Authentication.class);
    when(userAuthentication.getDetails()).thenReturn("123456789");

    SecurityContextHolder.getContext()//from  www .j a va  2 s . c  o  m
            .setAuthentication(new OAuth2Authentication(mock(OAuth2Request.class), userAuthentication));
    assertThat(getAccessTokenFromSecurityContext().isPresent()).isFalse();
}