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:ru.org.linux.auth.AuthUtil.java

public static boolean isSessionAuthorized() {
    Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
    return authentication != null && (authentication.isAuthenticated() && !hasAuthority("ROLE_SYSTEM_ANONYMOUS")
            && hasAuthority("ROLE_ANONYMOUS"));
}

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

public static UserDetails getUserDetails() {
    UserDetails userDetails = (UserDetails) SecurityContextHolder.getContext().getAuthentication()
            .getPrincipal();/*from   w  ww  . ja v a2s . c  o  m*/
    return userDetails;
}

From source file:com.iflytek.edu.cloud.frame.utils.RestContextHolder.java

/**
 * password ????//from   w  w  w . j  av a  2 s .co m
 * 
 * @return
 */
public static String getUsername() {
    Authentication authentication = SecurityContextHolder.getContext().getAuthentication();

    if (authentication != null) {
        OAuth2Authentication oAuth2Authentication = (OAuth2Authentication) authentication;
        if (oAuth2Authentication.getUserAuthentication() != null)
            return oAuth2Authentication.getUserAuthentication().getName();
    }

    return null;
}

From source file:nl.surfnet.coin.selfservice.util.SpringSecurity.java

/**
 * Get the  currently logged in user from the security context.
 *
 * @return String//  ww  w  .  ja v a2  s.c  o  m
 * @throws SecurityException in case no principal is found.
 */
public static CoinUser getCurrentUser() {
    Authentication auth = SecurityContextHolder.getContext().getAuthentication();
    if (auth == null) {
        return new CoinUser();
    }
    Object principal = auth.getPrincipal();
    if (principal != null && principal instanceof CoinUser) {
        return (CoinUser) principal;
    }
    return new CoinUser();
}

From source file:com.francetelecom.clara.cloud.TestHelper.java

public static void loginAsAdmin() {
    SecurityContextHolder.getContext().setAuthentication(new BobAuthentication());
}

From source file:pt.webdetails.cpk.testUtils.PentahoSystemForTesting.java

public static <T> T runAsSystem(final Callable<T> callable) throws Exception {
    final String name = "system session"; //$NON-NLS-1$
    IPentahoSession origSession = PentahoSessionHolder.getSession();
    Authentication origAuth = SecurityContextHolder.getContext().getAuthentication();
    try {//from   www  .  java 2s  .  c  o  m
        // create pentaho session
        StandaloneSession session = new StandaloneSession(name);
        session.setAuthenticated(name);
        // create authentication

        GrantedAuthority[] roles;

        ISystemSettings settings = PentahoSystem.getSystemSettings();
        String roleName = (settings != null) ? settings.getSystemSetting("acl-voter/admin-role", "Admin")
                : "Admin";

        roles = new GrantedAuthority[1];
        roles[0] = new SimpleGrantedAuthority(roleName);

        Authentication auth = new UsernamePasswordAuthenticationToken(name, "", Arrays.asList(roles)); //$NON-NLS-1$

        // set holders
        PentahoSessionHolder.setSession(session);
        SecurityContextHolder.getContext().setAuthentication(auth);
        return callable.call();
    } finally {
        IPentahoSession sessionToDestroy = PentahoSessionHolder.getSession();
        if (sessionToDestroy != null) {
            try {
                sessionToDestroy.destroy();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        PentahoSessionHolder.setSession(origSession);
        SecurityContextHolder.getContext().setAuthentication(origAuth);
    }
}

From source file:org.obozek.minermonitor.config.SecurityContextHelperImpl.java

@Override
public SecurityContext getSecurityContext() {
    SecurityContext sc = SecurityContextHolder.getContext();
    return sc;
}

From source file:psiprobe.tools.SecurityUtils.java

/**
 * User has role.//from   w ww.  j av  a 2s  .  c  om
 *
 * @param privilegedRole the privileged role
 * @return true, if successful
 */
private static boolean userHasRole(String privilegedRole) {
    Collection<? extends GrantedAuthority> authorities = SecurityContextHolder.getContext().getAuthentication()
            .getAuthorities();

    boolean result = false;
    for (GrantedAuthority authority : authorities) {
        if (privilegedRole.equals(authority.getAuthority())) {
            result = true;
            break;
        }
    }
    return result;
}

From source file:rzd.vivc.documentexamination.service.AuthenticationInfoSpringSecurity.java

@Override
public String getLogin() {
    Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
    Object principal = authentication.getPrincipal();
    String username;/*from   w  ww  .  j  a  v  a2s.c o  m*/
    if (principal instanceof UserDetails) {
        username = ((UserDetails) principal).getUsername();
    } else {
        username = principal.toString();
    }
    return username;
}

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

private static Principal signinPrivate(String userId) {
    Authentication authentication = new UsernamePasswordAuthenticationToken("user", "pass",
            AuthorityUtils.createAuthorityList("ROLE_USER"));
    SecurityContextHolder.getContext().setAuthentication(authentication);
    return (Principal) authentication.getPrincipal();
}