Example usage for org.springframework.security.core.context SecurityContext getAuthentication

List of usage examples for org.springframework.security.core.context SecurityContext getAuthentication

Introduction

In this page you can find the example usage for org.springframework.security.core.context SecurityContext getAuthentication.

Prototype

Authentication getAuthentication();

Source Link

Document

Obtains the currently authenticated principal, or an authentication request token.

Usage

From source file:org.emonocot.portal.http.AuthenticatingHttpClientFactory.java

/**
 * @param uri Set the uri// w ww .ja va 2  s .  c om
 * @param httpMethod set the httpMethod
 * @return a client http request object
 * @throws IOException if there is a problem
 */
public final ClientHttpRequest createRequest(final URI uri, final HttpMethod httpMethod) throws IOException {
    ClientHttpRequest clientHttpRequest = super.createRequest(uri, httpMethod);
    SecurityContext securityContext = SecurityContextHolder.getContext();
    if (securityContext != null && securityContext.getAuthentication() != null) {
        Authentication authentication = securityContext.getAuthentication();
        if (authentication != null && authentication.getPrincipal() != null
                && authentication.getPrincipal().getClass().equals(User.class)) {
            User user = (User) authentication.getPrincipal();
            String unencoded = user.getUsername() + ":" + user.getPassword();
            String encoded = new String(Base64.encodeBase64(unencoded.getBytes()));
            clientHttpRequest.getHeaders().add("Authorization", "Basic " + encoded);
        }
    }
    return clientHttpRequest;
}

From source file:fi.helsinki.opintoni.security.SecurityUtils.java

public String getCurrentLogin() {
    SecurityContext securityContext = SecurityContextHolder.getContext();
    Authentication authentication = securityContext.getAuthentication();
    String userName = null;/*from  ww w  . j  a va2 s .  co m*/
    if (authentication != null) {
        if (authentication.getPrincipal() instanceof UserDetails) {
            UserDetails springSecurityUser = (UserDetails) authentication.getPrincipal();
            userName = springSecurityUser.getUsername();
        } else if (authentication.getPrincipal() instanceof String) {
            userName = (String) authentication.getPrincipal();
        }
    }
    return userName;
}

From source file:org.openinfinity.sso.identityprovisioning.api.IdentityProvisioningAspect.java

@Before(value = "anyIdentityProvisioningMethod() && @annotation(identityProvisioning)")
public void provision(IdentityProvisioning userProvisioning) {
    long startTime = System.currentTimeMillis();
    LOGGER.debug("Starting provisioning of the Identity.");
    SecurityContext securityContext = SecurityContextHolder.getContext();
    Authentication authentication = securityContext.getAuthentication();
    if (authentication instanceof Identity) {
        Identity identity = (Identity) authentication;
        if (LOGGER.isDebugEnabled())
            LOGGER.debug("Injecting Identity to provisioning bridge.");
        identityProvisioningBridge.provision(identity);

    }//  w  ww  .  ja  va 2 s.  co m
    if (LOGGER.isDebugEnabled())
        LOGGER.debug("Identity provisioning finalized in " + (System.currentTimeMillis() - startTime) + " ms.");
}

From source file:com.linuxbox.enkive.message.search.AsynchronousSearchThread.java

public AsynchronousSearchThread(Map<String, String> fields, String searchResultId,
        MessageSearchService messageSearchService, SearchResultBuilder searchResultBuilder) {
    SecurityContext ctx = SecurityContextHolder.getContext();
    searchingUserAuth = ctx.getAuthentication();
    this.searchResultId = searchResultId;
    this.fields = fields;
    this.messageSearchService = messageSearchService;
    this.searchResultBuilder = searchResultBuilder;
}

From source file:com.gsr.myschool.server.security.SecurityContextProviderImpl.java

@Override
@Transactional(readOnly = true)//  w  w w. j  a  v a 2  s . c  o m
public User getCurrentUser() {
    SecurityContext securityContext = SecurityContextHolder.getContext();
    if (securityContext != null) {
        String email = securityContext.getAuthentication().getName();
        return userRepos.findByEmail(email);
    }

    return null;
}

From source file:com.gsr.myschool.server.security.SecurityContextProviderImpl.java

@Override
@Transactional(readOnly = true)//ww  w . j  a  va  2 s  . c o m
public AdminUser getCurrentAdmin() {
    SecurityContext securityContext = SecurityContextHolder.getContext();
    if (securityContext != null) {
        String email = securityContext.getAuthentication().getName();
        return adminUserRepos.findByEmail(email);
    }

    return null;
}

From source file:fr.mycellar.interfaces.web.security.SecurityContextTokenRepository.java

public Token newToken(SecurityContext context) {
    Authentication auth = context.getAuthentication();
    if ((auth != null) && auth.isAuthenticated() && !(auth instanceof AnonymousAuthenticationToken)) {
        return keyBasedPersistenceTokenService.allocateToken(context.getAuthentication().getName());
    }/*from  w  ww .  j a v  a2 s  . co m*/
    return null;
}

From source file:org.jasig.springframework.security.servlet.test.ServletSecurityTestController.java

protected String handle(ModelMap model) {
    final SecurityContext context = SecurityContextHolder.getContext();
    final Authentication authentication = context.getAuthentication();
    if (authentication != null) {
        final Object principal = authentication.getPrincipal();

        final String username;
        if (principal instanceof UserDetails) {
            username = ((UserDetails) principal).getUsername();
            model.put("userDetails", principal);
        } else {//from   w ww . ja va  2s . co m
            username = principal.toString();
        }

        model.put("username", username);
    }

    return "servletDisplayUserInfo";
}

From source file:org.jasig.springframework.security.portlet.test.PortletSecurityTestController.java

protected String handle(ModelMap model) {
    final SecurityContext context = SecurityContextHolder.getContext();
    final Authentication authentication = context.getAuthentication();
    if (authentication != null) {
        final Object principal = authentication.getPrincipal();

        final String username;
        if (principal instanceof UserDetails) {
            username = ((UserDetails) principal).getUsername();
            model.put("userDetails", principal);
        } else {/*from  w w w  .  ja  v a2  s  . c  o  m*/
            username = principal.toString();
        }

        model.put("username", username);
    }

    return "portletDisplayUserInfo";
}

From source file:org.socialhistoryservices.pid.security.NAAuthentication.java

/**
 * Retrieves the user./*  w w w  . ja v  a 2 s  .c om*/
 *
 * @return
 */
public List<String> authenticate() {

    final SecurityContext context = SecurityContextHolder.getContext();
    final OAuth2Authentication authentication = (OAuth2Authentication) context.getAuthentication();
    return NamingAuthority.getNaRole(authentication.getUserAuthentication());
}