Example usage for org.springframework.security.core.context SecurityContext setAuthentication

List of usage examples for org.springframework.security.core.context SecurityContext setAuthentication

Introduction

In this page you can find the example usage for org.springframework.security.core.context SecurityContext setAuthentication.

Prototype

void setAuthentication(Authentication authentication);

Source Link

Document

Changes the currently authenticated principal, or removes the authentication information.

Usage

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

public static SecurityContext studentSecurityContext() {
    SecurityContext securityContext = SecurityContextHolder.createEmptyContext();
    securityContext.setAuthentication(new UsernamePasswordAuthenticationToken("opiskelija", "password"));
    return securityContext;
}

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

public static SecurityContext teacherSecurityContext() {
    SecurityContext securityContext = SecurityContextHolder.createEmptyContext();
    securityContext.setAuthentication(new UsernamePasswordAuthenticationToken("opettaja", "password"));
    return securityContext;
}

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

public static SecurityContext hybridUserSecurityContext() {
    SecurityContext securityContext = SecurityContextHolder.createEmptyContext();
    securityContext.setAuthentication(new UsernamePasswordAuthenticationToken("hybriduser", "password"));
    return securityContext;
}

From source file:io.pivotal.cla.security.Login.java

public static Authentication loginAs(User user) {
    SecurityContext context = SecurityContextHolder.createEmptyContext();
    UserAuthentication authentication = new UserAuthentication(user);
    context.setAuthentication(authentication);
    SecurityContextHolder.setContext(context);
    return authentication;
}

From source file:org.socialhistoryservices.pid.service.StubPidResourceServiceTest.java

@BeforeClass
public static void setup() throws Exception {

    if (System.getProperty("pid.properties") == null) // -Dhandle.properties=pid.properties
        System.setProperty("pid.properties", "server/pid.properties");

    class Grant implements GrantedAuthority {

        private String authority;

        public Grant(String authority) {
            this.authority = authority;
        }/*from   w  w  w  .  j a  v  a  2s  .c om*/

        @Override
        public String getAuthority() {
            return authority;
        }
    }
    final Collection<GrantedAuthority> authorities = new ArrayList(4);
    authorities.add(new Grant("ROLE_PID-WEBSERVICE-USER"));
    authorities.add(new Grant("ROLE_PID-WEBSERVICE-USER_00000.1"));
    authorities.add(new Grant("ROLE_PID-WEBSERVICE-USER_" + na));
    authorities.add(new Grant("ROLE_PID-WEBSERVICE-USER_00000.2"));
    ClientToken clientToken = new ClientToken("test-client", UUID.randomUUID().toString(), null);
    UsernamePasswordAuthenticationToken usernamePasswordAuthentication = new UsernamePasswordAuthenticationToken(
            null, null, authorities);
    OAuth2Authentication auth2Authentication = new OAuth2Authentication(clientToken,
            usernamePasswordAuthentication);
    final SecurityContext context = SecurityContextHolder.getContext();
    context.setAuthentication(auth2Authentication);
}

From source file:org.cloudbyexample.dc.service.util.SecurityContextUtil.java

public static void createSystemUser() {
    SecurityContext context = SecurityContextHolder.getContext();

    // Set the granted authorities so that the user is not authenticated again
    List<GrantedAuthority> grantedAuthorities = new ArrayList<GrantedAuthority>();
    grantedAuthorities.add(new SimpleGrantedAuthority("ROLE_SYSTEM"));

    UsernamePasswordAuthenticationToken token = new UsernamePasswordAuthenticationToken("system", "",
            grantedAuthorities);//from www .j a  v  a 2 s  .  c  o  m

    context.setAuthentication(token);
}

From source file:com.brienwheeler.web.spring.security.SecurityUtils.java

public static void setLoggedInUser(User user, Collection<? extends GrantedAuthority> authorities) {
    ValidationUtils.assertNotNull(user, "user cannot be null");
    ValidationUtils.assertTrue(user.getId() != 0, "user cannot be unpersisted");
    ValidationUtils.assertNotNull(authorities, "authorities cannot be null");

    SecurityContext securityContext = SecurityContextHolder.getContext();
    if (securityContext == null)
        throw new IllegalStateException("can't set logged in user if securityContext is null");

    if ((securityContext.getAuthentication() != null)
            && (!(securityContext.getAuthentication() instanceof AnonymousAuthenticationToken))
            && (securityContext.getAuthentication().getPrincipal() != null)) {
        Object principal = securityContext.getAuthentication().getPrincipal();
        if (!(principal instanceof UserDetails) || (((UserDetails) principal).getUserId() != user.getId())) {
            throw new IllegalStateException("cannot overwrite currently logged in user");
        }/*from   w ww .  j av  a 2s . c  o  m*/
        // fall through to set new Authentication object in case authorities have changed
    }

    securityContext.setAuthentication(new UsernamePasswordAuthenticationToken(
            new UserDetails(user, authorities), user.getHashedPassword(), authorities));
}

From source file:io.pivotal.cla.security.WithClaAuthorUserFactory.java

@Override
public SecurityContext createSecurityContext(WithClaAuthorUser user) {
    User adminUser = create();/*  www . j  a  v a2  s  . c o  m*/
    UserAuthentication auth = new UserAuthentication(adminUser);
    SecurityContext context = SecurityContextHolder.createEmptyContext();
    context.setAuthentication(auth);
    return context;
}

From source file:com.utest.domain.service.BaseDomainServiceIntegrationTest.java

protected void loginUser(final User user) throws Exception {
    final List<GrantedAuthority> grantedAuthorities = new ArrayList<GrantedAuthority>();
    final AuthenticatedUserInfo authUser = new AuthenticatedUserInfo(user.getId(), user.getFullName());
    final Authentication auth = new UsernamePasswordAuthenticationToken(authUser, null, grantedAuthorities);
    final SecurityContext ctx = new SecurityContextImpl();
    ctx.setAuthentication(auth);
    SecurityContextHolder.setContext(ctx);
}

From source file:io.pivotal.cla.security.WithAdminUserFactory.java

@Override
public SecurityContext createSecurityContext(WithAdminUser user) {
    User adminUser = create();/* w w  w.  j av  a 2 s . c  om*/
    UserAuthentication auth = new UserAuthentication(adminUser);
    SecurityContext context = SecurityContextHolder.createEmptyContext();
    context.setAuthentication(auth);
    return context;
}