use of org.apache.shiro.subject.SimplePrincipalCollection in project ART-TIME by Artezio.
the class ActiveDirectoryRealmTest method testDoGetAuthorizationInfo_ifExistManagedProjects.
@Test
public void testDoGetAuthorizationInfo_ifExistManagedProjects() throws Exception {
activeDirectoryRealm = PowerMock.createPartialMock(ActiveDirectoryRealm.class, "queryForAuthorizationInfo");
setField(activeDirectoryRealm, "projectRepository", projectRepository);
List<Project> projects = Arrays.asList(new 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));
assertTrue(actual.getRoles().contains(UserRoles.PM_ROLE));
}
use of org.apache.shiro.subject.SimplePrincipalCollection in project wechat by dllwh.
the class CustomSessionManager method getSessionEntity.
/**
* ----------------------------------------------- [私有方法]
*/
private OnlineUser getSessionEntity(Session session) {
/**
* 获取登录信息
*/
Object obj = session.getAttribute(DefaultSubjectContext.PRINCIPALS_SESSION_KEY);
if (null == obj) {
return null;
}
if (obj instanceof SimplePrincipalCollection) {
SimplePrincipalCollection spc = (SimplePrincipalCollection) obj;
obj = spc.getPrimaryPrincipal();
if (null != obj && obj instanceof SysUser) {
OnlineUser onlineUser = new OnlineUser((SysUser) obj);
// 最后一次和系统交互的时间
onlineUser.setLastAccess(session.getLastAccessTime());
// 主机的ip地址
onlineUser.setHost(session.getHost());
// session ID
onlineUser.setSessionId(session.getId().toString());
// 会话到期
onlineUser.setTimeout(session.getTimeout());
// 会话创建
onlineUser.setStartTime(session.getStartTimestamp());
SessionStatus sessionStatus = (SessionStatus) session.getAttribute(CacheConstans.SESSION_STATUS);
boolean status = Boolean.TRUE;
if (null != sessionStatus) {
status = sessionStatus.getOnlineStatus();
}
onlineUser.setSessionStatus(status);
return onlineUser;
}
}
return null;
}
use of org.apache.shiro.subject.SimplePrincipalCollection in project shiro by apache.
the class HashedCredentialsMatcherTest method testBackwardsCompatibleSaltedAuthenticationInfo.
/**
* Test backwards compatibility of salted credentials before
* <a href="https://issues.apache.org/jira/browse/SHIRO-186">SHIRO-186</a> edits.
*/
@Test
public void testBackwardsCompatibleSaltedAuthenticationInfo() {
HashedCredentialsMatcher matcher = new HashedCredentialsMatcher(Sha1Hash.ALGORITHM_NAME);
// enable this for Shiro 1.0 backwards compatibility:
matcher.setHashSalted(true);
// simulate an account with SHA-1 hashed password, using the username as the salt
// (BAD IDEA, but backwards-compatible):
final String username = "username";
final String password = "password";
final Object hashedPassword = new Sha1Hash(password, username).getBytes();
AuthenticationInfo account = new AuthenticationInfo() {
public PrincipalCollection getPrincipals() {
return new SimplePrincipalCollection(username, "realmName");
}
public Object getCredentials() {
return hashedPassword;
}
};
// simulate a username/password (plaintext) token created in response to a login attempt:
AuthenticationToken token = new UsernamePasswordToken("username", "password");
// verify the hashed token matches what is in the account:
assertTrue(matcher.doCredentialsMatch(token, account));
}
use of org.apache.shiro.subject.SimplePrincipalCollection in project shiro by apache.
the class TextConfigurationRealmTest method testHasRole.
/*
* Tests that roles can't be read while the realm is being loaded.
*/
@Test
public void testHasRole() throws InterruptedException {
setUpForReadConfigurationTest();
executeTest(new Runnable() {
public void run() {
PrincipalCollection principalCollection = new SimplePrincipalCollection("user1", "realm1");
assertTrue("principal doesn't have role when it should", realm.hasRole(principalCollection, "role2"));
assertTrue("principal doesn't have all roles when it should", realm.hasAllRoles(principalCollection, Arrays.asList(new String[] { "role1", "role2" })));
}
});
}
use of org.apache.shiro.subject.SimplePrincipalCollection in project shiro by apache.
the class TextConfigurationRealmTest method testCheckPermission.
/*
* Tests that a principal's permissions can't be checked while the realm is being loaded.
*/
@Test
public void testCheckPermission() throws InterruptedException {
setUpForReadConfigurationTest();
executeTest(new Runnable() {
public void run() {
PrincipalCollection principalCollection = new SimplePrincipalCollection("user1", "realm1");
try {
realm.checkPermission(principalCollection, "role1_permission1");
realm.checkPermissions(principalCollection, new String[] { "role1_permission1", "role2_permission2" });
} catch (AuthorizationException ae) {
fail("principal doesn't have permission when it should");
}
}
});
}
Aggregations