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:br.ufac.sion.audit.CustomRevisionEntityListener.java

@Override
public void newRevision(Object revisionEntity) {
    try {//from   ww  w.  j  ava 2 s . c  om
        CustomRevisionEntity customRevisionEntity = (CustomRevisionEntity) revisionEntity;
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        System.out.println("authentication == null : " + (authentication == null));
        if (authentication == null) {
            customRevisionEntity.setUsername("Sion - tarefa automatizada!");
        } else {
            customRevisionEntity.setUsername(authentication.getName());
        }
        customRevisionEntity.setIp(InetAddress.getLocalHost().getHostAddress());
    } catch (Exception ex) {
        log.error("Erro de sistema (sion-web): " + ex.getMessage(), ex);
    }
}

From source file:org.unitedinternet.cosmo.security.CosmoSecurityManager.java

public String getUsername() {
    SecurityContext context = SecurityContextHolder.getContext();
    Authentication authentication = context.getAuthentication();
    if (authentication == null) {
        throw new RuntimeException("no Authentication found in SecurityContext");
    }//from w w  w .  jav a2 s. c om

    return authentication.getName();
}

From source file:am.ik.categolj2.domain.AuditAwareBean.java

@Override
public String getCurrentAuditor() {
    Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
    if (authentication == null) {
        return null;
    } else {/*w  w w  .j  ava  2 s  .c o m*/
        return authentication.getName();
    }
}

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

/**
 * Get the currently authenticated Account principal.
 *///from   w ww  . ja  va2  s . com
public static Account getCurrentAccount() {
    Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
    if (authentication == null) {
        return null;
    }
    Object principal = authentication.getPrincipal();
    return principal instanceof Account ? (Account) principal : null;
}

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

public static String getUserName() {
    Authentication currentUser = SecurityContextHolder.getContext().getAuthentication();
    if (currentUser == null) {
        return "";
    }/* w ww. ja  va2  s .  c  o  m*/
    return currentUser.getName();
}

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

public static UserDetails getPrincipal() {
    SecurityContext securityContext = SecurityContextHolder.getContext();
    if (securityContext != null) {
        Authentication auth = securityContext.getAuthentication();
        if (auth != null) {
            Object principal = auth.getPrincipal();
            if (principal instanceof UserDetails) {
                return (UserDetails) principal;
            }//  w w w.  j  a  v a2s.c o m
        }
    }
    return new User();
}

From source file:com.hp.autonomy.hod.sso.SpringSecurityTokenProxyService.java

@Override
public TokenProxy<E, TokenType.Simple> getTokenProxy() {
    final Authentication authentication = SecurityContextHolder.getContext().getAuthentication();

    if (!(authentication instanceof HodAuthentication)) {
        return null;
    }//w  w w .j av a  2 s  . com

    // Usage of this class requires that the application is using HodAuthentications
    //noinspection unchecked
    return ((HodAuthentication<E>) authentication).getTokenProxy();
}

From source file:org.springbyexample.contact.orm.entity.AuditorAwareImpl.java

@Override
public String getCurrentAuditor() {
    return SecurityContextHolder.getContext().getAuthentication().getPrincipal().toString();
}

From source file:com.github.fauu.natrank.security.SecurityContextAccessorImpl.java

@Override
public boolean isCurrentAuthenticationAnonymous() {
    Authentication authentication = SecurityContextHolder.getContext().getAuthentication();

    return authenticationTrustResolver.isAnonymous(authentication);
}

From source file:customer.springboot.controller.UserController.java

@RequestMapping(value = "/loggedin", method = RequestMethod.GET)
public User getUserLoggedIn() throws Exception {
    Authentication auth = SecurityContextHolder.getContext().getAuthentication();
    if (auth == null) {
        throw new Exception("Unauthenticated Request");
    }/*  ww  w .j  a va2  s  .  c  o m*/

    Object principal = auth.getPrincipal();
    if (principal == null) {
        throw new Exception("Invalid authentication object");
    }

    if (!org.springframework.security.core.userdetails.User.class.isAssignableFrom(principal.getClass())) {
        throw new Exception("Invalid authentication object" + principal.getClass().getName());
    }
    org.springframework.security.core.userdetails.User u = (org.springframework.security.core.userdetails.User) principal;
    return userDao.findByUsername(u.getUsername());
}