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.excilys.ebi.bank.web.security.SecurityUtils.java

public static boolean isAuthenticated() {
    SecurityContext ctx = SecurityContextHolder.getContext();
    return ctx != null //
            && ctx.getAuthentication() != null //
            && ctx.getAuthentication().isAuthenticated();
}

From source file:es.us.isa.ideas.app.social.CustomUserIdSource.java

public String getUserId() {
    Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
    if (authentication == null) {
        throw new IllegalStateException("Unable to get a ConnectionRepository: no user signed in");
    }/*w w  w. j a  v  a 2  s . c om*/
    return authentication.getName();
}

From source file:com.clz.share.sec.util.SignInUtils.java

/**
 * Programmatically signs in the user with the given the user ID.
 *//* www.  java2 s.c o m*/
public static void signin(String userId) {
    SecurityContextHolder.getContext()
            .setAuthentication(new UsernamePasswordAuthenticationToken(userId, null, null));
}

From source file:eu.gyza.eap.service.HelloService.java

public HelloService() {
    SecurityContextHolder.getContext()
            .setAuthentication(new UsernamePasswordAuthenticationToken("deslos@yahoo.com", null, null));
}

From source file:edu.zipcloud.cloudstreetmarket.core.util.AuthenticationUtil.java

public static boolean userHasRole(Role role) {
    SecurityContext securityContext = SecurityContextHolder.getContext();
    if (securityContext != null) {
        Authentication auth = securityContext.getAuthentication();
        if (auth != null) {
            if (!auth.isAuthenticated()) {
                return false;
            }//from w w w .  j av  a  2  s.c o m
        }
    }

    return UserDetailsUtil.hasRole(getPrincipal(), role);
}

From source file:com.rockagen.gnext.service.spring.security.aspect.SS3Tools.java

/**
 * Obtain current user principal//from   w  w w .  j  a  v  a2 s  .c  o m
 * @return BasicSecurityUser
 */
public static BasicSecurityUser getUserInfo() {
    BasicSecurityUser userDetails = null;
    try {
        SecurityContext sc = SecurityContextHolder.getContext();
        Authentication au = sc.getAuthentication();
        userDetails = (BasicSecurityUser) au.getPrincipal();
    } catch (NullPointerException e) {
        userDetails = new BasicSecurityUser("guest", "guest", true, true, true, true,
                AuthorityUtils.NO_AUTHORITIES, "root", "", "127.0.0.1");

    }
    return userDetails;

}

From source file:org.syncope.core.util.EntitlementUtil.java

public static Set<String> getOwnedEntitlementNames() {
    final Set<String> result = new HashSet<String>();

    final SecurityContext ctx = SecurityContextHolder.getContext();

    if (ctx != null && ctx.getAuthentication() != null && ctx.getAuthentication().getAuthorities() != null) {

        for (GrantedAuthority authority : SecurityContextHolder.getContext().getAuthentication()
                .getAuthorities()) {/*from   w  ww .  j  av a  2 s. c om*/

            result.add(authority.getAuthority());
        }
    }

    return result;
}

From source file:com.lateralthoughts.commons.security.SecurityUtils.java

/**
 * Programmatically signs in the user with the given the user ID.
 *///from  w  ww.ja  va 2  s  .c o  m
static void signin(AccountWrapper userDetailsPrincipal) {
    SecurityContextHolder.getContext().setAuthentication(new UsernamePasswordAuthenticationToken(
            userDetailsPrincipal, null, userDetailsPrincipal.getAuthorities()));
}

From source file:com.erudika.para.security.SecurityUtils.java

/**
 * Extracts a User object from the security context
 * @return an authenticated user or null if a user is not authenticated
 */// w ww .  j a v a  2 s . com
public static User getAuthenticatedUser() {
    User u = null;
    if (SecurityContextHolder.getContext().getAuthentication() != null) {
        Authentication auth = SecurityContextHolder.getContext().getAuthentication();
        if (auth.isAuthenticated() && auth.getPrincipal() instanceof User) {
            u = (User) auth.getPrincipal();
        }
    }
    return u;
}

From source file:cz.sohlich.workstack.repository.auditing.TaskSecurityAuditor.java

@Override
public String getCurrentAuditor() {
    return ((UserAuthentication) SecurityContextHolder.getContext().getAuthentication()).getDetails().getId();
}