protected AuthenticationInfo buildAuthenticationInfo(Object principal, Object credentials) { Collection<Object> principals = new ArrayList<Object>(3); principals.add(new UserIdPrincipal(USER_ID)); principals.add(new UsernamePrincipal(USERNAME)); principals.add(USER_ID + USERNAME); return new SimpleAuthenticationInfo(principals, PASSWORD, getName()); }
@Test public void testDefaultConfig() { String localhost = "localhost"; Subject subject = SecurityUtils.getSubject(); subject.login(new UsernamePasswordToken(USERNAME, PASSWORD, localhost)); assertTrue(subject.isAuthenticated()); assertTrue(subject.hasRole(ROLE)); UsernamePrincipal usernamePrincipal = subject.getPrincipals().oneByType(UsernamePrincipal.class); assertTrue(usernamePrincipal.getUsername().equals(USERNAME)); UserIdPrincipal userIdPrincipal = subject.getPrincipals().oneByType(UserIdPrincipal.class); assertTrue(userIdPrincipal.getUserId() == USER_ID); assertTrue(realm.hasRole(subject.getPrincipals(), ROLE)); subject.logout(); }
@Test public void testDefaultConfig() { AuthenticationInfo info = realm.getAuthenticationInfo(new UsernamePasswordToken(USERNAME, PASSWORD, localhost)); assertNotNull(info); assertTrue(realm.hasRole(info.getPrincipals(), ROLE)); Object principal = info.getPrincipals().getPrimaryPrincipal(); assertTrue(principal instanceof UserIdPrincipal); UsernamePrincipal usernamePrincipal = info.getPrincipals().oneByType(UsernamePrincipal.class); assertTrue(usernamePrincipal.getUsername().equals(USERNAME)); UserIdPrincipal userIdPrincipal = info.getPrincipals().oneByType(UserIdPrincipal.class); assertTrue(userIdPrincipal.getUserId() == USER_ID); String stringPrincipal = info.getPrincipals().oneByType(String.class); assertTrue(stringPrincipal.equals(USER_ID + USERNAME)); }
@Override protected AuthenticationInfo buildAuthenticationInfo(Object principal, Object credentials) { String username = (String) principal; UsernamePrincipal customPrincipal = new UsernamePrincipal(username); return new SimpleAccount(customPrincipal, credentials, getName()); } };
@Test public void testCreateAccountOverride() { AuthorizingRealm realm = new AllowAllRealm() { @Override protected AuthenticationInfo buildAuthenticationInfo(Object principal, Object credentials) { String username = (String) principal; UsernamePrincipal customPrincipal = new UsernamePrincipal(username); return new SimpleAccount(customPrincipal, credentials, getName()); } }; AuthenticationInfo info = realm.getAuthenticationInfo(new UsernamePasswordToken(USERNAME, PASSWORD, localhost)); assertNotNull(info); assertTrue(realm.hasRole(info.getPrincipals(), ROLE)); Object principal = info.getPrincipals().getPrimaryPrincipal(); assertTrue(principal instanceof UsernamePrincipal); assertEquals(USERNAME, ((UsernamePrincipal) principal).getUsername()); }
protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken token) throws AuthenticationException { SimpleAccount account = (SimpleAccount) super.doGetAuthenticationInfo(token); if (account != null) { SimplePrincipalCollection principals = new SimplePrincipalCollection(); principals.add(new UserIdPrincipal(USER_ID), getName()); principals.add(new UsernamePrincipal(USERNAME), getName()); account.setPrincipals(principals); } return account; }
Principal principal = new UsernamePrincipal("blah"); PrincipalCollection pCollection = new SimplePrincipalCollection(principal, "nullAuthzRealm"); List<Permission> permList = new ArrayList<Permission>();
@Test public void testRealmWithRolePermissionResolver() { Principal principal = new UsernamePrincipal("rolePermResolver"); PrincipalCollection pCollection = new SimplePrincipalCollection(principal, "testRealmWithRolePermissionResolver"); AuthorizingRealm realm = new AllowAllRealm(); realm.setRolePermissionResolver( new RolePermissionResolver() { public Collection<Permission> resolvePermissionsInRole( String roleString ) { Collection<Permission> permissions = new HashSet<Permission>(); if( roleString.equals( ROLE )) { permissions.add( new WildcardPermission( ROLE + ":perm1" ) ); permissions.add( new WildcardPermission( ROLE + ":perm2" ) ); permissions.add( new WildcardPermission( "other:*:foo" ) ); } return permissions; } }); assertTrue( realm.hasRole( pCollection, ROLE ) ); assertTrue( realm.isPermitted( pCollection, ROLE + ":perm1" ) ); assertTrue( realm.isPermitted( pCollection, ROLE + ":perm2" ) ); assertFalse( realm.isPermitted( pCollection, ROLE + ":perm3" ) ); assertTrue( realm.isPermitted( pCollection, "other:bar:foo" ) ); }