Example usage for org.springframework.security.core.context SecurityContextHolder getContext

List of usage examples for org.springframework.security.core.context SecurityContextHolder getContext

Introduction

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

Prototype

public static SecurityContext getContext() 

Source Link

Document

Obtain the current SecurityContext.

Usage

From source file:com.project.companyprofile.admin.misc.SignInUtils.java

/**
 * Programmatically signs in the user with the given the user ID.
 *//*from  ww w . j a  va 2  s. c  o m*/
public static void signin(LoginVo loginVo, List<GrantedAuthority> roles) {
    logger.info("Signin util fired!");

    SecurityContextHolder.getContext()
            .setAuthentication(new UsernamePasswordAuthenticationToken(loginVo, null, roles));
}

From source file:ru.mystamps.web.support.spring.security.SecurityContextUtils.java

/**
 * @author Sergey Chechenev//from   w  w  w.  j a  va 2s .  c o  m
 */
public static boolean hasAuthority(GrantedAuthority authority) {
    return Optional.ofNullable(SecurityContextHolder.getContext().getAuthentication())
            .map(Authentication::getAuthorities).orElse(Collections.emptyList()).contains(authority);
}

From source file:com.springsource.greenhouse.account.AccountUtils.java

/**
 * Programmatically sign-in the member holding the provided Account.
 * Sets the Account in the SecurityContext, which will associate this Account with the user Session.
 *//*  w ww . j a  va2  s .  c o m*/
public static void signin(Account account) {
    SecurityContextHolder.getContext().setAuthentication(authenticationTokenFor(account));
}

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

/**
 * @return The current user's EmplID/* w  ww.j ava  2  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.indusborn.util.AccountUtils.java

/**
 * Programatically sign-in the member holding the provided Account.
 * Sets the Account in the SecurityContext, which will associate this Account with the user Session.
 *//*from   w  w w .  ja v a2  s.  co  m*/
public static void signin(UserVO userVO) {
    SecurityContextHolder.getContext().setAuthentication(authenticationTokenFor(userVO));
}

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

@NotNull
public static User getUser(boolean forceCheck) {
    User result = null;/*from   ww  w. j a v  a  2 s.co  m*/
    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:com.folion.social.SignInUtils.java

public static void signIn(String userIdentifier, SocialAccountService socialAccountService) {

    Account account = socialAccountService.getAccount(userIdentifier);
    List<GrantedAuthority> grantedAuthorities = createAuthorities(account.getAccountRole());
    SecurityContextHolder.getContext().setAuthentication(
            new UsernamePasswordAuthenticationToken(userIdentifier, null, grantedAuthorities));
}

From source file:com.ge.predix.acs.testutils.MockSecurityContext.java

public static void mockSecurityContext(final Zone zone) {
    ZoneOAuth2Authentication acsZoneOAuth = Mockito.mock(ZoneOAuth2Authentication.class);
    when(acsZoneOAuth.getZoneId()).thenReturn(zone.getSubdomain());
    SecurityContextHolder.getContext().setAuthentication(acsZoneOAuth);
}

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

public static Optional<String> getAccessTokenFromSecurityContext() {
    SecurityContext securityContext = SecurityContextHolder.getContext();

    Authentication authentication = securityContext.getAuthentication();
    if (authentication instanceof OAuth2Authentication) {
        Object userDetails = ((OAuth2Authentication) authentication).getUserAuthentication().getDetails();
        if (userDetails != null) {
            try {
                final Map details = (Map) userDetails;
                return Optional.fromNullable((String) details.get(ACCESS_TOKEN));
            } catch (ClassCastException e) {

                return Optional.absent();
            }//from  www.j a  va 2  s.c  o  m
        } else {

            return Optional.absent();
        }
    }

    return Optional.absent();
}

From source file:com.wiiyaya.framework.provider.utils.ConsumerUtils.java

private static Authentication getAuthentication() {
    SecurityContext context = SecurityContextHolder.getContext();
    if (context == null) {
        return null;
    }/*ww  w . ja va2s  .  c  o  m*/
    Authentication authentication = context.getAuthentication();
    if (authentication == null || !authentication.isAuthenticated()
            || authentication instanceof AnonymousAuthenticationToken) {
        return null;
    }
    return authentication;
}