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

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

Introduction

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

Prototype

public TestingAuthenticationToken(Object principal, Object credentials, List<GrantedAuthority> authorities) 

Source Link

Usage

From source file:org.springframework.security.web.authentication.www.DigestAuthenticationFilterTests.java

@Test
public void authenticationCreatesEmptyContext() throws Exception {
    SecurityContext existingContext = SecurityContextHolder.createEmptyContext();
    TestingAuthenticationToken existingAuthentication = new TestingAuthenticationToken("existingauthenitcated",
            "pass", "ROLE_USER");
    existingContext.setAuthentication(existingAuthentication);

    SecurityContextHolder.setContext(existingContext);

    String responseDigest = DigestAuthUtils.generateDigest(false, USERNAME, REALM, PASSWORD, "GET", REQUEST_URI,
            QOP, NONCE, NC, CNONCE);/*ww w . j a  v a  2 s . c  om*/

    request.addHeader("Authorization",
            createAuthorizationHeader(USERNAME, REALM, NONCE, REQUEST_URI, responseDigest, QOP, NC, CNONCE));

    filter.setCreateAuthenticatedToken(true);
    executeFilterInContainerSimulator(filter, request, true);

    assertThat(existingAuthentication).isSameAs(existingContext.getAuthentication());
}

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.
 *///from w w w .j a va2  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",
            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);/*  w w  w . j  a va 2s. co  m*/

    putTokenInContext(token);
}

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
 *//* w  w w .j  a v a 2s  .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 w  w w . j av  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);//from  ww w . ja v a  2 s .  c  o m

    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//from   w  w  w  .j a  va2  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  va 2  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);
}