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:csjobs.AuthenticationSecurity.SecurityUtils.java

public static String getUser() {
    return isAuthenticated()
            ? ((UserDetails) SecurityContextHolder.getContext().getAuthentication().getPrincipal())
                    .getUsername()//from  w  w  w .  j  a va 2s . com
            : null;
}

From source file:org.slc.sli.dashboard.util.SecurityUtil.java

/**
 * find if a user is IT Administrator or Leader
 *
 * @return/*www . j  av  a 2  s  .  co  m*/
 */
public static boolean isNotEducator() {
    SecurityContext context = SecurityContextHolder.getContext();
    if (context != null) {
        Authentication authentication = context.getAuthentication();
        if (authentication != null) {
            Collection<GrantedAuthority> authorities = authentication.getAuthorities();
            for (GrantedAuthority authority : authorities) {
                if (authority.getAuthority().equals(Constants.ROLE_IT_ADMINISTRATOR)) {
                    return true;
                } else if (authority.getAuthority().equals(Constants.ROLE_LEADER)) {
                    return true;
                }
            }
        }
    }
    return false;
}

From source file:com.dnn.controller.LoginController.java

public String pegarUsuario() {
    String usuarioLogado = null;//from ww w  . j a  va 2  s  .c o  m
    Object usuario = SecurityContextHolder.getContext().getAuthentication().getPrincipal();
    if (usuario instanceof UserDetails) {
        usuarioLogado = ((UserDetails) usuario).getUsername();
    } else {
        usuarioLogado = usuario.toString();
    }
    return usuarioLogado;
}

From source file:com.foilen.smalltools.tools.TombstoneTools.java

/**
 * Add a warning log entry telling which user in Spring Security is using it.
 *
 * @param id/*  w  w  w . ja  v  a 2 s  . co m*/
 *            the id to output in the log (should be unique to know which one was used)
 * @param time
 *            the time when you added that entry (could be a date or the version number)
 */
public static void logWithUser(String id, String time) {

    SecurityContext securityContext = null;
    try {
        securityContext = SecurityContextHolder.getContext();
    } catch (Exception e) {
    }

    String user = null;
    Class<? extends Authentication> authClass = null;
    if (securityContext != null) {
        Authentication authentication = securityContext.getAuthentication();
        if (authentication != null) {
            authClass = authentication.getClass();
            user = authentication.getName();
        }
    }

    logger.warn("{} - AuthClass: {} - User: {}", id, authClass, user);
}

From source file:com.fursel.persistence.service.impl.OrderServiceImpl.java

@Override
public Page<Order> getOrders(Pageable pageable, String keywords) {
    TenantUserDetails userDetails = (TenantUserDetails) SecurityContextHolder.getContext().getAuthentication()
            .getPrincipal();/*from  w ww .  j a  v  a 2 s  . com*/
    return orderRepository.findOrders(keywords, userDetails.getTenantId(), pageable);
}

From source file:mx.unam.pixel.controller.LoginController.java

@PostConstruct
public void init() {
    Object user = SecurityContextHolder.getContext().getAuthentication().getPrincipal();
    Authentication auth = SecurityContextHolder.getContext().getAuthentication();
    if (!(auth instanceof AnonymousAuthenticationToken) && (user instanceof UserDetails)) {
        this.username = ((UserDetails) user).getUsername();
        if (auth.getAuthorities().size() > 0) {
            this.rol = auth.getAuthorities().iterator().next().getAuthority();
        }//from w w  w  . j  a v  a2 s  . co m
        this.loged = true;
    } else if (user instanceof String) {
        this.username = user.toString();
        this.loged = false;
    }

}

From source file:cz.sohlich.workstack.security.StatelessAuthenticationFilter.java

@Override
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
        throws IOException, ServletException {
    SecurityContextHolder.getContext()
            .setAuthentication(tokenAuthenticationService.getAuthentication((HttpServletRequest) request));
    chain.doFilter(request, response);//from  w ww. j a v a 2  s.  co m
}

From source file:com.amediamanager.controller.AllControllers.java

@ModelAttribute("user")
public User populateUser() {
    SecurityContext context = SecurityContextHolder.getContext();
    if (context == null) {
        return null;
    }/*  ww  w.ja v a 2  s  . c  o  m*/
    Authentication auth = context.getAuthentication();
    if (auth == null) {
        return null;
    }
    Object user = auth.getDetails();

    return (user != null && user instanceof User) ? (User) user : null;
}

From source file:com.caa.koko.SimplePageController.java

@RequestMapping(value = "/")
public String home(Model model) {
    Object principal = SecurityContextHolder.getContext().getAuthentication().getPrincipal();
    String uid = "somepony";
    String name = "Hickory Dickory Dock Diques";

    if (principal instanceof User) {
        User u = (User) principal;/*from   w  w  w  . java 2  s  .  c  o m*/
        uid = u.getUsername();
        name = u.getName();
    }

    model.addAttribute("uid", uid);
    model.addAttribute("name", name);
    return "home";
}

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

public static void logout() {
    SecurityContextHolder.getContext().setAuthentication(null);
}