use of org.apache.shiro.subject.PrincipalCollection in project shiro by apache.
the class AllSuccessfulStrategyTest method beforeAttemptRealmDoesntSupportToken.
@Test(expected = UnsupportedTokenException.class)
public void beforeAttemptRealmDoesntSupportToken() {
Realm notSupportingRealm = new AuthorizingRealm() {
public boolean supports(AuthenticationToken token) {
return false;
}
protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken token) throws AuthenticationException {
return null;
}
protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principal) {
return null;
}
};
strategy.beforeAttempt(notSupportingRealm, null, null);
}
use of org.apache.shiro.subject.PrincipalCollection in project shiro by apache.
the class SimpleAuthenticationInfoTest method testMergeWithImmutablePrincipalCollection.
@SuppressWarnings("serial")
@Test
public void testMergeWithImmutablePrincipalCollection() {
SimpleAuthenticationInfo aggregate = new SimpleAuthenticationInfo();
// Make a quick test fixture that does *not* implement MutablePrincipalCollection
PrincipalCollection principalCollection = new PrincipalCollection() {
@SuppressWarnings("unchecked")
public List asList() {
return null;
}
@SuppressWarnings("unchecked")
public Set asSet() {
return null;
}
public <T> Collection<T> byType(Class<T> type) {
return null;
}
@SuppressWarnings("unchecked")
public Collection fromRealm(String realmName) {
Collection<Object> principals = new HashSet<Object>();
principals.add("testprincipal");
return principals;
}
public Object getPrimaryPrincipal() {
return null;
}
public Set<String> getRealmNames() {
Set<String> realms = new HashSet<String>();
realms.add("testrealm");
return realms;
}
public boolean isEmpty() {
return false;
}
public <T> T oneByType(Class<T> type) {
return null;
}
@SuppressWarnings("unchecked")
public Iterator iterator() {
return null;
}
};
aggregate.setPrincipals(principalCollection);
SimpleAuthenticationInfo local = new SimpleAuthenticationInfo("username", "password", "testRealm");
aggregate.merge(local);
assertEquals(2, aggregate.getPrincipals().asList().size());
}
use of org.apache.shiro.subject.PrincipalCollection in project shiro by apache.
the class AbstractRememberMeManagerTest method testGetRememberedPrincipalsWithEmptySerializedBytes.
/**
* Tests the {@link AbstractRememberMeManager#getRememberedPrincipals(SubjectContext)} method
* implementation when the internal
* {@link AbstractRememberMeManager#getRememberedSerializedIdentity(SubjectContext)} method
* returns null or empty bytes.
*/
@Test
public void testGetRememberedPrincipalsWithEmptySerializedBytes() {
AbstractRememberMeManager rmm = new DummyRememberMeManager();
// Since the dummy's getRememberedSerializedIdentity implementation returns an empty byte
// array, we should be ok:
PrincipalCollection principals = rmm.getRememberedPrincipals(new DefaultSubjectContext());
assertNull(principals);
// try with a null return value too:
rmm = new DummyRememberMeManager() {
@Override
protected byte[] getRememberedSerializedIdentity(SubjectContext subjectContext) {
return null;
}
};
principals = rmm.getRememberedPrincipals(new DefaultSubjectContext());
assertNull(principals);
}
use of org.apache.shiro.subject.PrincipalCollection in project shiro by apache.
the class HelloController method home.
@SuppressWarnings("Duplicates")
@RequestMapping("/")
public String home(HttpServletRequest request, Model model) {
String name = "World";
Subject subject = SecurityUtils.getSubject();
PrincipalCollection principalCollection = subject.getPrincipals();
if (principalCollection != null && !principalCollection.isEmpty()) {
Collection<Map> principalMaps = subject.getPrincipals().byType(Map.class);
if (CollectionUtils.isEmpty(principalMaps)) {
name = subject.getPrincipal().toString();
} else {
name = (String) principalMaps.iterator().next().get("username");
}
}
model.addAttribute("name", name);
return "hello";
}
use of org.apache.shiro.subject.PrincipalCollection in project shiro by apache.
the class AbstractAuthorizationAnnotationTest method bindUser.
protected void bindUser() {
PrincipalCollection principals = new SimplePrincipalCollection("test", realm.getName());
bind(new Subject.Builder(securityManager).principals(principals).buildSubject());
}
Aggregations