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.jevontech.wabl.services.implementation.SecurityServiceImpl.java

@Override
public Boolean hasProtectedAccess() {
    return (SecurityContextHolder.getContext().getAuthentication().getAuthorities()
            .contains(new SimpleGrantedAuthority("ADMIN")));
}

From source file:com.gcrm.util.security.UserUtil.java

/**
 * Gets current login user name//from w w  w.  j a v  a2s .c o  m
 * 
 * @return curretn login user name
 */
public static String getUserName() {
    try {
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        UserDetails userDetails = (UserDetails) authentication.getPrincipal();
        return userDetails.getUsername();
    } catch (Exception e) {
        return null;
    }
}

From source file:org.bremersee.common.security.core.context.RunAsUtil.java

/**
 * Executes the callback with the specified authority (name and roles).
 *
 * @param name     the name of the executing authority
 * @param roles    the roles of the executing authority
 * @param callback the callback which should be executed
 * @param <T>      the response type
 * @return the response of the callback/*from   w  w w.  java  2s  .co  m*/
 */
@SuppressWarnings("SameParameterValue")
public static <T> T runAs(final String name, final String[] roles, final RunAsCallback<T> callback) {

    Validate.notBlank(name, "Name mast not be null or blank");
    final Authentication auth = SecurityContextHolder.getContext().getAuthentication();
    try {
        SecurityContextHolder.getContext().setAuthentication(new RunAsAuthentication(name, roles));
        return callback.execute();
    } finally {
        SecurityContextHolder.getContext().setAuthentication(auth);
    }
}

From source file:dtu.ds.warnme.utils.SecurityUtils.java

public static String getCurrentUserId() {
    if (SecurityContextHolder.getContext() == null
            || SecurityContextHolder.getContext().getAuthentication() == null
            || SecurityContextHolder.getContext().getAuthentication().getPrincipal() == null) {
        return null;
    }//from   ww w  .  j a  va 2s.c  o m
    if (!(SecurityContextHolder.getContext().getAuthentication().getPrincipal() instanceof CustomUser)) {
        return null;
    }
    return ((CustomUser) SecurityContextHolder.getContext().getAuthentication().getPrincipal()).getId();
}

From source file:com.folion.social.SignInUtils.java

public static void signIn(Account user) {

    List<GrantedAuthority> grantedAuthorities = createAuthorities(user.getAccountRole());
    SecurityContextHolder.getContext().setAuthentication(
            new UsernamePasswordAuthenticationToken(user.getEmail(), null, grantedAuthorities));
}

From source file:whitelabel.cloud.webapp.utils.UserUtil.java

public static CloudUser getUser() {
    if (SecurityContextHolder.getContext() != null
            && SecurityContextHolder.getContext().getAuthentication() != null) {
        try {/* w  w  w  .ja  v  a  2s. co m*/
            UserDetailsImpl principal = (UserDetailsImpl) SecurityContextHolder.getContext().getAuthentication()
                    .getPrincipal();
            return (CloudUser) principal.getUser();
        } catch (Exception dontCare) {

        }

    }
    return null;
}

From source file:com.qpark.eip.core.spring.security.EipUserDetailsService.java

/**
 * @param userDetailService/*from   w w  w.  ja v a 2  s  .c o  m*/
 * @param userName
 * @return
 */
public static boolean setSecurityContextHolderAuthentication(final EipUserProvider userDetailService,
        final String userName) {
    boolean doLogin = SecurityContextHolder.getContext().getAuthentication() == null;
    if (doLogin) {
        User user = userDetailService.getUser(userName);
        SecurityContextHolder.getContext()
                .setAuthentication(new UsernamePasswordAuthenticationToken(user, user.getPassword()));
    }
    return doLogin;
}

From source file:cz.muni.fi.pb138.cvmanager.controller.BaseController.java

/**
 * Finds out and returns name of currently logged in user
 *
 * @return name of currently logged in user
 *///from w ww .ja v  a 2 s. c o  m
protected String getPrincipalUsername() {
    Authentication auth = SecurityContextHolder.getContext().getAuthentication();
    UserDetails userDetails = (UserDetails) auth.getPrincipal();
    return userDetails.getUsername();
}

From source file:fr.mael.microrss.BaseTest.java

protected void login(Integer id) {
    User user = userService.get(id);/*from  www .  j ava  2 s.  com*/
    SecurityContextHolder.getContext()
            .setAuthentication(new UsernamePasswordAuthenticationToken(user, "admin"));
}

From source file:com.inkubator.securitycore.util.UserInfoUtil.java

public static List<String> getRoles() {
    Authentication auth = SecurityContextHolder.getContext().getAuthentication();
    List<String> listAuth;
    if (auth != null) {
        listAuth = toStringList(auth.getAuthorities());
    } else {//from  w w  w .j  a  va2s. c o m
        listAuth = Collections.emptyList();
    }
    return listAuth;

}