/** * 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. */ 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 ); }
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 ); }
@Bean public AuthenticationProvider authenticationProvider() { return new TestingAuthenticationProvider(); }
/** * 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 */ 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 ); }