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:hsa.awp.admingui.util.AccessUtil.java

public static Collection<GrantedAuthority> getAuthorities() {
    return SecurityContextHolder.getContext().getAuthentication().getAuthorities();
}

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

public static boolean isThePrincipal(String userId) {
    SecurityContext securityContext = SecurityContextHolder.getContext();
    if (securityContext != null && !StringUtils.isBlank(userId)) {
        Authentication auth = securityContext.getAuthentication();
        if (auth != null) {
            Object principal = auth.getPrincipal();
            if (principal instanceof UserDetails && userId.equals(((UserDetails) principal).getUsername())) {
                return true;
            }//from   ww w .ja v  a  2s .  c  om
        }
    }
    return false;
}

From source file:com.mustafaderyol.inventory.beans.LoginControlClass.java

public void authorizedUserControl() {

    Authentication authentication = SecurityContextHolder.getContext().getAuthentication();

    if (!authentication.getPrincipal().toString().equals("anonymousUser")) {

        NavigationHandler nh = FacesContext.getCurrentInstance().getApplication().getNavigationHandler();
        nh.handleNavigation(FacesContext.getCurrentInstance(), null, "/dashboard.xhtml?faces-redirect=true");

    }/*  ww w  . j  ava 2 s  .  c o  m*/
}

From source file:business.SmallExcerptListTests.java

@Test(groups = "request", dependsOnMethods = "approveRequest")
public void uploadExcerptList() throws IOException {
    UserAuthenticationToken palga = getPalga();
    SecurityContext securityContext = SecurityContextHolder.getContext();
    securityContext.setAuthentication(palga);

    RequestRepresentation representation = requestController.getRequestById(palga, processInstanceId);
    log.info("Status: " + representation.getStatus());

    log.info("Activity: " + representation.getActivityId());
    if (representation.getActivityId() == null) {
        for (Task task : taskService.createTaskQuery().list()) {
            log.info("Task " + task.getId() + ", process instance: " + task.getProcessInstanceId() + ", name: "
                    + task.getName() + ", key: " + task.getTaskDefinitionKey());
        }//from   w  ww  .jav a  2s  .co  m
    }

    log.info("uploadExcerptList: processInstanceId = " + processInstanceId);

    representation = requestController.claim(palga, processInstanceId, representation);

    ClassLoader classLoader = getClass().getClassLoader();
    URL resource = classLoader.getResource("test/Example excerptlist.csv");
    InputStream input = resource.openStream();
    MultipartFile file = new MockMultipartFile(resource.getFile(), input);

    Integer flowTotalChunks = 1;
    Integer flowChunkNumber = 1;
    String flowIdentifier = "flow";

    int entryCount = requestController.uploadExcerptList(palga, processInstanceId, resource.getFile(),
            flowTotalChunks, flowChunkNumber, flowIdentifier, file);

    assertEquals(3, entryCount);

    SecurityContextHolder.clearContext();
}

From source file:business.LargerExcerptListTests.java

@Test(groups = "request", dependsOnMethods = "approveRequest")
public void uploadExcerptList() throws IOException {
    UserAuthenticationToken palga = getPalga();
    SecurityContext securityContext = SecurityContextHolder.getContext();
    securityContext.setAuthentication(palga);

    RequestRepresentation representation = requestController.getRequestById(palga, processInstanceId);
    log.info("Status: " + representation.getStatus());

    log.info("Activity: " + representation.getActivityId());
    if (representation.getActivityId() == null) {
        for (Task task : taskService.createTaskQuery().list()) {
            log.info("Task " + task.getId() + ", process instance: " + task.getProcessInstanceId() + ", name: "
                    + task.getName() + ", key: " + task.getTaskDefinitionKey());
        }//w ww.  jav a2  s .  c  om
    }

    log.info("uploadExcerptList: processInstanceId = " + processInstanceId);

    representation = requestController.claim(palga, processInstanceId, representation);

    ClassLoader classLoader = getClass().getClassLoader();
    URL resource = classLoader.getResource("test/Example excerptlist 20150521 v2.csv");
    InputStream input = resource.openStream();
    MultipartFile file = new MockMultipartFile(resource.getFile(), input);

    Integer flowTotalChunks = 1;
    Integer flowChunkNumber = 1;
    String flowIdentifier = "flow";

    int entryCount = requestController.uploadExcerptList(palga, processInstanceId, resource.getFile(),
            flowTotalChunks, flowChunkNumber, flowIdentifier, file);

    assertEquals(6, entryCount);

    SecurityContextHolder.clearContext();
}

From source file:mvc.CheckUserController.java

private String getUserName() {
    Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
    if (!(authentication instanceof AnonymousAuthenticationToken) && authentication != null) {
        String currentUserName = authentication.getName();
        return currentUserName;
    }/*  ww w.j  a v a2s .  co m*/
    return "";
}

From source file:org.socialsignin.exfmproxy.mvc.auth.ExFmUserPasswordService.java

private String getAuthenticatedUserName() {
    Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
    return authentication == null || authentication.getName().equals("anonymousUser") ? null
            : authentication.getName();//from   w  ww  .  j  a  v  a  2 s.  c o m
}

From source file:utils.AbstractTest.java

public void authenticate(String username) {
    UserDetails userDetails;/*from w  w  w.  j av a2s.c o m*/
    TestingAuthenticationToken authenticationToken;
    SecurityContext context;

    if (username == null)
        authenticationToken = null;
    else {
        userDetails = loginService.loadUserByUsername(username);
        authenticationToken = new TestingAuthenticationToken(userDetails, null);
    }

    context = SecurityContextHolder.getContext();
    context.setAuthentication(authenticationToken);
}

From source file:com.javiermoreno.springboot.modelo.GestionPersonasServiceImpl.java

@Transactional
@Override//ww w  .  ja  v  a 2s .  c  o  m
public Persona registrarNuevaPersona(Persona persona) {
    em.persist(persona);
    Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
    if (authentication != null) {
        Object principal = authentication.getPrincipal();
        String username;
        if (principal instanceof UserDetails) {
            username = ((UserDetails) principal).getUsername();
        } else {
            username = principal.toString();
        }
        System.out.format("Nueva persona registrada por {0}.", username);
    }
    return persona;
}

From source file:org.cloudfoundry.identity.uaa.security.DefaultSecurityContextAccessorTests.java

@Test
public void clientIsNotUser() throws Exception {
    SecurityContextHolder.getContext().setAuthentication(
            new UsernamePasswordAuthenticationToken("client", "secret", UaaAuthority.ADMIN_AUTHORITIES));

    assertFalse(new DefaultSecurityContextAccessor().isUser());
}