Search in sources :

Example 36 with SimplePrincipalCollection

use of org.apache.shiro.subject.SimplePrincipalCollection in project perry by ca-cwds.

the class JwtRealmTest method testDoGetAuthorizationInfo.

@Test
public void testDoGetAuthorizationInfo() throws Exception {
    PerryAccount perryAccount = perryAccount();
    String jwtToken = generateToken(perryAccount.getUser());
    List<Object> principals = Arrays.asList(perryAccount.getUser(), perryAccount, jwtToken);
    PrincipalCollection principalCollection = new SimplePrincipalCollection(principals, "testRealm");
    AuthorizationInfo authorizationInfo = jwtRealm.doGetAuthorizationInfo(principalCollection);
    Assert.assertEquals(perryAccount.getRoles(), authorizationInfo.getRoles());
}
Also used : PrincipalCollection(org.apache.shiro.subject.PrincipalCollection) SimplePrincipalCollection(org.apache.shiro.subject.SimplePrincipalCollection) SimplePrincipalCollection(org.apache.shiro.subject.SimplePrincipalCollection) AuthorizationInfo(org.apache.shiro.authz.AuthorizationInfo) Test(org.junit.Test) AbstractApiSecurityTest(gov.ca.cwds.security.AbstractApiSecurityTest)

Example 37 with SimplePrincipalCollection

use of org.apache.shiro.subject.SimplePrincipalCollection in project tesla by linking12.

the class SessionService method list.

public List<UserOnline> list() {
    List<UserOnline> list = new ArrayList<>();
    Collection<Session> sessions = sessionDAO.getActiveSessions();
    for (Session session : sessions) {
        UserOnline userOnline = new UserOnline();
        if (session.getAttribute(DefaultSubjectContext.PRINCIPALS_SESSION_KEY) == null) {
            continue;
        } else {
            SimplePrincipalCollection principalCollection = (SimplePrincipalCollection) session.getAttribute(DefaultSubjectContext.PRINCIPALS_SESSION_KEY);
            String userName = principalCollection.getRealmNames().iterator().next();
            userOnline.setUsername(userName);
        }
        userOnline.setId((String) session.getId());
        userOnline.setHost(session.getHost());
        userOnline.setStartTimestamp(session.getStartTimestamp());
        userOnline.setLastAccessTime(session.getLastAccessTime());
        userOnline.setTimeout(session.getTimeout());
        list.add(userOnline);
    }
    return list;
}
Also used : UserOnline(io.github.tesla.authz.domain.UserOnline) ArrayList(java.util.ArrayList) SimplePrincipalCollection(org.apache.shiro.subject.SimplePrincipalCollection) Session(org.apache.shiro.session.Session)

Example 38 with SimplePrincipalCollection

use of org.apache.shiro.subject.SimplePrincipalCollection in project moon by gentoo111.

the class ShiroRealm method getAuthorizationCacheKey.

/**
 * 重写缓存key,否则集群下session共享时,会重复执行doGetAuthorizationInfo权限配置
 */
@Override
protected Object getAuthorizationCacheKey(PrincipalCollection principals) {
    SimplePrincipalCollection principalCollection = (SimplePrincipalCollection) principals;
    Object object = principalCollection.getPrimaryPrincipal();
    if (object instanceof User) {
        User user = (User) object;
        return "authorization:cache:key:users:" + user.getId();
    }
    return super.getAuthorizationCacheKey(principals);
}
Also used : User(com.moon.admin.domain.User) SimplePrincipalCollection(org.apache.shiro.subject.SimplePrincipalCollection)

Example 39 with SimplePrincipalCollection

use of org.apache.shiro.subject.SimplePrincipalCollection in project ART-TIME by Artezio.

the class ActiveDirectoryRealmTest method testDoGetAuthorizationInfo_ifPrincipalsCollectionNotEmpty.

@Test
public void testDoGetAuthorizationInfo_ifPrincipalsCollectionNotEmpty() throws Exception {
    activeDirectoryRealm = PowerMock.createPartialMock(ActiveDirectoryRealm.class, "queryForAuthorizationInfo");
    setField(activeDirectoryRealm, "projectRepository", projectRepository);
    List<Project> projects = new ArrayList<Project>();
    PrincipalCollection principals = new SimplePrincipalCollection("principal", "ldapRealm");
    AuthorizationInfo info = new SimpleAccount(principals, "credential", new HashSet<String>());
    PowerMock.expectPrivate(activeDirectoryRealm, "queryForAuthorizationInfo", anyObject(PrincipalCollection.class), anyObject(LdapContextFactory.class)).andReturn(info);
    expect(projectRepository.getProjectsByManager("principal")).andReturn(projects);
    PowerMock.replayAll(projectRepository, activeDirectoryRealm);
    AuthorizationInfo actual = activeDirectoryRealm.doGetAuthorizationInfo(principals);
    PowerMock.verifyAll();
    assertTrue(actual.getRoles().contains(UserRoles.EMPLOYEE_ROLE));
}
Also used : SimpleAccount(org.apache.shiro.authc.SimpleAccount) Project(com.artezio.arttime.datamodel.Project) ArrayList(java.util.ArrayList) PrincipalCollection(org.apache.shiro.subject.PrincipalCollection) SimplePrincipalCollection(org.apache.shiro.subject.SimplePrincipalCollection) SimplePrincipalCollection(org.apache.shiro.subject.SimplePrincipalCollection) AuthorizationInfo(org.apache.shiro.authz.AuthorizationInfo) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Example 40 with SimplePrincipalCollection

use of org.apache.shiro.subject.SimplePrincipalCollection in project ART-TIME by Artezio.

the class ActiveDirectoryRealmTest method testDoGetAuthorizationInfo_ifPrincipalsCollectionNotContainsLdapRealm.

@Test
public void testDoGetAuthorizationInfo_ifPrincipalsCollectionNotContainsLdapRealm() throws NamingException, NoSuchFieldException {
    activeDirectoryRealm = createMockBuilder(ActiveDirectoryRealm.class).addMockedMethod("queryForAuthorizationInfo").createMock();
    PrincipalCollection principals = new SimplePrincipalCollection("principals", "jdbcRealm");
    replay(activeDirectoryRealm);
    AuthorizationInfo actual = activeDirectoryRealm.doGetAuthorizationInfo(principals);
    verify(activeDirectoryRealm);
    assertNull(actual);
}
Also used : PrincipalCollection(org.apache.shiro.subject.PrincipalCollection) SimplePrincipalCollection(org.apache.shiro.subject.SimplePrincipalCollection) SimplePrincipalCollection(org.apache.shiro.subject.SimplePrincipalCollection) AuthorizationInfo(org.apache.shiro.authz.AuthorizationInfo) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Aggregations

SimplePrincipalCollection (org.apache.shiro.subject.SimplePrincipalCollection)55 PrincipalCollection (org.apache.shiro.subject.PrincipalCollection)26 Test (org.junit.Test)25 AuthorizationInfo (org.apache.shiro.authz.AuthorizationInfo)11 ArrayList (java.util.ArrayList)7 AuthenticationInfo (org.apache.shiro.authc.AuthenticationInfo)7 DefaultSecurityManager (org.apache.shiro.mgt.DefaultSecurityManager)7 Realm (org.apache.shiro.realm.Realm)7 Principal (java.security.Principal)6 SimpleAuthenticationInfo (org.apache.shiro.authc.SimpleAuthenticationInfo)6 UsernamePasswordToken (org.apache.shiro.authc.UsernamePasswordToken)6 Subject (org.apache.shiro.subject.Subject)6 Subject (ddf.security.Subject)5 SecurityToken (org.apache.cxf.ws.security.tokenstore.SecurityToken)5 SimpleAccount (org.apache.shiro.authc.SimpleAccount)5 AuthorizingRealm (org.apache.shiro.realm.AuthorizingRealm)5 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)5 SecurityAssertion (ddf.security.assertion.SecurityAssertion)4 HashSet (java.util.HashSet)4 AuthenticationToken (org.apache.shiro.authc.AuthenticationToken)4