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:web.org.perfmon4j.console.app.HeaderController.java

@Override
public void doAfterCompose(Component comp) throws Exception {
    super.doAfterCompose(comp);
    Perfmon4jUser user = (Perfmon4jUser) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
    String welcome = "Welcome " + user.getDisplayName();
    currentUserMenu.setLabel(welcome);//from w w w .  j  a va 2 s.  co m
}

From source file:com.mastercard.test.spring.security.MockWithMockUserTest.java

@Test(expected = NullPointerException.class)
public void getUserThrowsNullPointerExceptionWhenWithoutWithMockUser() {
    SecurityContextHolder.getContext().getAuthentication().getPrincipal();
}

From source file:at.plechinger.demo.scribesec.facebook.TestController.java

@RequestMapping("/protected")
@ResponseBody//www. j av a2  s  . c  om
String home() {
    return "Hello (protected) World! Principal is: "
            + SecurityContextHolder.getContext().getAuthentication().getPrincipal();
}

From source file:fi.helsinki.opintoni.security.SecurityUtils.java

public String getCurrentLogin() {
    SecurityContext securityContext = SecurityContextHolder.getContext();
    Authentication authentication = securityContext.getAuthentication();
    String userName = null;/*from w ww. j av  a 2  s .com*/
    if (authentication != null) {
        if (authentication.getPrincipal() instanceof UserDetails) {
            UserDetails springSecurityUser = (UserDetails) authentication.getPrincipal();
            userName = springSecurityUser.getUsername();
        } else if (authentication.getPrincipal() instanceof String) {
            userName = (String) authentication.getPrincipal();
        }
    }
    return userName;
}

From source file:cz.cvut.kbss.wpa.back.EditUserBean.java

public PlayerDTO getPlayer() {
    if (player == null) {
        //load from credential
        CurrentUserDetails cud = (CurrentUserDetails) SecurityContextHolder.getContext().getAuthentication()
                .getPrincipal();//  w w w. j av a2 s.  com
        player = (PlayerDTO) cud.getUserDto();
        player.setPassword("");
    }
    return player;
}

From source file:org.springbyexample.contact.test.AbstractTransactionalProfileTest.java

/**
 * Set the default user on the security context.
 *///from ww w. ja va2 s  .  c o  m
@Override
protected void doInit() {
    SecurityContextHolder.getContext().setAuthentication(
            new UsernamePasswordAuthenticationToken(DEFAULT_SECURITY_USER, DEFAULT_SECURITY_USER_PASSWORD));
}

From source file:com.tapas.evidence.repository.KindergartenRepositoryImpl.java

@Override
@SuppressWarnings("unchecked")
public List<Kindergarten> findAll() {
    final Long tenantId = ((EvidenceUserDetails) SecurityContextHolder.getContext().getAuthentication()
            .getPrincipal()).getTenantId();
    Query query = this.entityManager.createNamedQuery(Kindergarten.QUERY_NAME_FIND_ALL_BY_DELETED_FLAG);
    query.setParameter("deleted", Boolean.FALSE);
    query.setParameter("tenantId", tenantId);
    return query.getResultList();
}

From source file:fr.keemto.web.config.ConnectionRepositoryConfig.java

@Bean
@Scope(value = "request", proxyMode = ScopedProxyMode.INTERFACES)
public ConnectionRepository connectionRepository() {
    Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
    if (authentication == null) {
        throw new IllegalStateException("Unable to get a ConnectionRepository: no user signed in");
    }//www . ja v  a  2  s .  co m
    String username = authentication.getName();
    ConnectionRepository connectionRepository = usersConnectionRepository.createConnectionRepository(username);
    return new ObservableConnectionRepository(username, connectionRepository, accountInterceptor);
}

From source file:es.upm.fiware.rss.dao.impl.UserDaoImpl.java

@Override
public RSUser getCurrentUser() {
    String userName;//from   w  ww .  j ava  2  s  .  c  om
    Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
    // When OAuth2 is being used, we should cast the authentication to read the correct user name
    userName = ((ClientAuthenticationToken) authentication).getUserProfile().getId();

    logger.info("User: {}", userName);
    return this.getById(userName);
}

From source file:net.sf.mmm.persistence.impl.hibernate.EnversTest.java

@Test
public void testPersistence() throws Exception {

    TransactionExecutor transactionExecutor = SpringContainerPool.getInstance(SPRING_XML)
            .get(TransactionExecutor.class);
    // MutableUserSession session = (MutableUserSession) UserSessionAccess.getSession();
    // session.setUser(TestUser.DEFAULT_USER);
    SecurityContextHolder.getContext().setAuthentication(TestUser.DEFAULT_USER);

    DummyRevisionedFooEntity foo = transactionExecutor
            .doInTransaction(new Callable<DummyRevisionedFooEntity>() {

                public DummyRevisionedFooEntity call() throws Exception {

                    return createAndSave();
                }/*from   w ww . j  a  v  a2s.c  o  m*/
            });
    final Long fooId = foo.getId();

    // shutdown and restart to ensure we really read from DB
    SpringContainerPool.dispose(SPRING_XML);
    transactionExecutor = SpringContainerPool.getInstance(SPRING_XML).get(TransactionExecutor.class);

    transactionExecutor.doInTransaction(new Callable<Void>() {

        public Void call() throws Exception {

            readAndUpdate(fooId);
            return null;
        }
    });

    // shutdown and restart to ensure we really read from DB
    SpringContainerPool.dispose(SPRING_XML);
    transactionExecutor = SpringContainerPool.getInstance(SPRING_XML).get(TransactionExecutor.class);

    transactionExecutor.doInTransaction(new Callable<Void>() {

        public Void call() throws Exception {

            readAgainAndDelete(fooId);
            return null;
        }
    });

}