Example usage for org.springframework.security.authentication TestingAuthenticationToken setAuthenticated

List of usage examples for org.springframework.security.authentication TestingAuthenticationToken setAuthenticated

Introduction

In this page you can find the example usage for org.springframework.security.authentication TestingAuthenticationToken setAuthenticated.

Prototype

public void setAuthenticated(boolean authenticated) 

Source Link

Usage

From source file:nl.surfnet.coin.selfservice.filter.SpringSecurityUtil.java

public static void setAuthentication(CoinUser coinUser) {

    final TestingAuthenticationToken token = new TestingAuthenticationToken(coinUser, "");
    token.setAuthenticated(true);

    SecurityContextHolder.getContext().setAuthentication(token);

}

From source file:org.encuestame.test.business.config.AbstractSpringSecurityContext.java

/**
 * set Authentication./*from w  ww .jav  a2  s. c om*/
 * @param username
 * @param password
 */
public void setAuthentication(final String username, final String password) {
    final List<GrantedAuthority> authorities = new ArrayList<GrantedAuthority>();
    //Add permissions.
    authorities.add(new SimpleGrantedAuthority(EnMePermission.ENCUESTAME_USER.name()));
    authorities.add(new SimpleGrantedAuthority(EnMePermission.ENCUESTAME_ADMIN.name()));
    authorities.add(new SimpleGrantedAuthority(EnMePermission.ENCUESTAME_WRITE.name()));
    authorities.add(new SimpleGrantedAuthority(EnMePermission.ENCUESTAME_OWNER.name()));
    TestingAuthenticationToken token = new TestingAuthenticationToken(username, password, authorities);
    token.setAuthenticated(true);
    SecurityContextHolder.getContext().setAuthentication(token);
    //System.out.println("creando SecurityContextHolder "+SecurityContextHolder.getContext().getAuthentication());
    setUsernameLogged(this.springSecurityLoggedUserAccount.getUsername());
}

From source file:org.encuestame.test.business.config.AbstractSpringSecurityContext.java

/**
 *
 * @param userAccount/*from   ww w.j  ava 2 s . com*/
 */
public void login(UserAccount userAccount) {
    final List<GrantedAuthority> authorities = new ArrayList<GrantedAuthority>();
    //Add permissions.
    authorities.add(new SimpleGrantedAuthority(EnMePermission.ENCUESTAME_USER.name()));
    authorities.add(new SimpleGrantedAuthority(EnMePermission.ENCUESTAME_ADMIN.name()));
    authorities.add(new SimpleGrantedAuthority(EnMePermission.ENCUESTAME_EDITOR.name()));
    authorities.add(new SimpleGrantedAuthority(EnMePermission.ENCUESTAME_PUBLISHER.name()));
    authorities.add(new SimpleGrantedAuthority(EnMePermission.ENCUESTAME_WRITE.name()));
    authorities.add(new SimpleGrantedAuthority(EnMePermission.ENCUESTAME_OWNER.name()));
    TestingAuthenticationToken token = new TestingAuthenticationToken(userAccount.getUsername(), "12345",
            authorities);
    token.setAuthenticated(true);
    SecurityContextHolder.getContext().setAuthentication(token);
    //setUsernameLogged(this.springSecurityLoggedUserAccount.getUsername());
}

From source file:ubc.pavlab.aspiredb.server.BaseSpringContextTest.java

/**
 * Grant authority to a test user, with admin privileges, and put the token in the context. This means your tests
 * will be authorized to do anything an administrator would be able to do.
 *///  w w w  . j a  v  a  2 s  . co m
protected void grantAdminAuthority(ApplicationContext ctx) {
    ProviderManager providerManager = (ProviderManager) ctx.getBean("authenticationManager");
    providerManager.getProviders().add(new TestingAuthenticationProvider());

    // Grant all roles to test user.
    TestingAuthenticationToken token = new TestingAuthenticationToken("administrator", "administrator",
            Arrays.asList(new GrantedAuthority[] {
                    new SimpleGrantedAuthority(AuthorityConstants.ADMIN_GROUP_AUTHORITY) }));

    token.setAuthenticated(true);

    putTokenInContext(token);
}

From source file:ubc.pavlab.aspiredb.server.BaseSpringContextTest.java

protected void grantAnonAuthority(ApplicationContext ctx) {
    ProviderManager providerManager = (ProviderManager) ctx.getBean("authenticationManager");
    providerManager.getProviders().add(new TestingAuthenticationProvider());

    // Grant all roles to test user.
    TestingAuthenticationToken token = new TestingAuthenticationToken("anon", "anon",
            Arrays.asList(new GrantedAuthority[] {
                    new SimpleGrantedAuthority(AuthorityConstants.IS_AUTHENTICATED_ANONYMOUSLY) }));

    token.setAuthenticated(true);

    putTokenInContext(token);/*  w  w  w.j a  v  a 2 s. com*/
}

From source file:ubc.pavlab.aspiredb.server.BaseSpringContextTest.java

/**
 * Grant authority to a test user, with regular user privileges, and put the token in the context. This means your
 * tests will be authorized to do anything that user could do
 *///from www. ja va2  s  .c  o  m
protected void switchToUser(ApplicationContext ctx, String username) {

    UserDetails user = userManager.loadUserByUsername(username);

    List<GrantedAuthority> authrs = new ArrayList<GrantedAuthority>(user.getAuthorities());

    ProviderManager providerManager = (ProviderManager) ctx.getBean("authenticationManager");
    providerManager.getProviders().add(new TestingAuthenticationProvider());

    TestingAuthenticationToken token = new TestingAuthenticationToken(username, "testing", authrs);
    token.setAuthenticated(true);

    putTokenInContext(token);
}

From source file:ubic.gemma.core.util.test.BaseSpringContextTest.java

/**
 * Grant authority to a test user, with admin privileges, and put the token in the context. This means your tests
 * will be authorized to do anything an administrator would be able to do.
 *
 * @param ctx context/*from  ww  w . j a v  a  2s  .c  om*/
 */
protected void grantAdminAuthority(ApplicationContext ctx) {
    ProviderManager providerManager = (ProviderManager) ctx.getBean("authenticationManager");
    providerManager.getProviders().add(new TestingAuthenticationProvider());

    // Grant all roles to test user.
    TestingAuthenticationToken token = new TestingAuthenticationToken("administrator", "administrator",
            Arrays.asList(new GrantedAuthority[] {
                    new SimpleGrantedAuthority(AuthorityConstants.ADMIN_GROUP_AUTHORITY) }));

    token.setAuthenticated(true);

    AuthenticationTestingUtil.putTokenInContext(token);
}

From source file:ubic.gemma.core.util.test.BaseSpringContextTest.java

protected void logOut(ApplicationContext ctx) {
    ProviderManager providerManager = (ProviderManager) ctx.getBean("authenticationManager");
    providerManager.getProviders().add(new TestingAuthenticationProvider());

    TestingAuthenticationToken token = new TestingAuthenticationToken(AuthorityConstants.ANONYMOUS_USER_NAME,
            null, Arrays.asList(new GrantedAuthority[] {
                    new SimpleGrantedAuthority(AuthorityConstants.ANONYMOUS_GROUP_AUTHORITY) }));

    token.setAuthenticated(false);

    AuthenticationTestingUtil.putTokenInContext(token);
}

From source file:ubic.gemma.core.util.test.BaseSpringContextTest.java

/**
 * Grant authority to a test user, with regular user privileges, and put the token in the context. This means your
 * tests will be authorized to do anything that user could do
 *
 * @param ctx      context/* w w  w  .j  a  v a  2 s . c o m*/
 * @param username user name
 */
protected void switchToUser(ApplicationContext ctx, String username) {

    UserDetails user = userManager.loadUserByUsername(username);

    List<GrantedAuthority> grantedAuthorities = new ArrayList<>(user.getAuthorities());

    ProviderManager providerManager = (ProviderManager) ctx.getBean("authenticationManager");
    providerManager.getProviders().add(new TestingAuthenticationProvider());

    TestingAuthenticationToken token = new TestingAuthenticationToken(username, "testing", grantedAuthorities);
    token.setAuthenticated(true);

    AuthenticationTestingUtil.putTokenInContext(token);
}

From source file:ubic.gemma.testing.BaseSpringContextTest.java

/**
 * Grant authority to a test user, with admin privileges, and put the token in the context. This means your tests
 * will be authorized to do anything an administrator would be able to do.
 *//*from w  w  w.  j a v  a2  s.  c o m*/
protected void grantAdminAuthority(ApplicationContext ctx) {
    ProviderManager providerManager = (ProviderManager) ctx.getBean("authenticationManager");
    providerManager.getProviders().add(new TestingAuthenticationProvider());

    // Grant all roles to test user.
    TestingAuthenticationToken token = new TestingAuthenticationToken("administrator", "administrator",
            new GrantedAuthority[] { new GrantedAuthorityImpl("GROUP_ADMIN") });

    token.setAuthenticated(true);

    putTokenInContext(token);
}