Example usage for org.apache.shiro.subject SimplePrincipalCollection add

List of usage examples for org.apache.shiro.subject SimplePrincipalCollection add

Introduction

In this page you can find the example usage for org.apache.shiro.subject SimplePrincipalCollection add.

Prototype

public void add(Object principal, String realmName) 

Source Link

Usage

From source file:at.pollux.thymeleaf.shiro.dialect.test.TestIniRealm.java

License:Apache License

@Override
protected void add(SimpleAccount account) {
    String username = (String) account.getPrincipals().getPrimaryPrincipal();

    // Let's add some additional principals for testing
    SimplePrincipalCollection principalCollection = new SimplePrincipalCollection();
    principalCollection.addAll(account.getPrincipals());
    principalCollection.add(counter.getAndIncrement(), "integerRealm");
    TestObjPrincipal objPrinc = new TestObjPrincipal(username.toUpperCase() + " " + username.toUpperCase());
    principalCollection.add(objPrinc, "objRealm");
    account.setPrincipals(principalCollection);

    super.add(account);
}

From source file:cn.itganhuo.app.web.shiro.ShiroDbRealm.java

License:Apache License

/**
 * ?/* www  .j a  v a  2s.com*/
 * <ol>
 * <li>???shiro?</li>
 * <li>?????????</li>
 * </ol>
 *
 * @param user_id ?
 * @version 0.0.2-SNAPSHOT
 * @author -?
 */
public void clearUserCache(String user_id) {
    log.debug("Began to clear the user cache.");

    SimplePrincipalCollection spc = new SimplePrincipalCollection();
    spc.add(user_id, getName());
    super.clearCachedAuthorizationInfo(spc);
}

From source file:com.ablesky.asdeploy.test.ShiroTestUtils.java

License:Apache License

public static void mockCurrentUser(User user, boolean isSuperAdmin) {
    Subject subject = Mockito.mock(Subject.class);
    Mockito.when(subject.isAuthenticated()).thenReturn(true);
    Mockito.when(subject.getPrincipal()).thenReturn(user.getUsername());
    SimplePrincipalCollection principals = new SimplePrincipalCollection(user.getUsername(), "testRealm");
    principals.add(user, "testRealm");
    Mockito.when(subject.getPrincipals()).thenReturn(principals);
    Mockito.when(subject.hasRole(Role.NAME_SUPER_ADMIN)).thenReturn(isSuperAdmin);
    bindSubject(subject);//from  w ww. j a  v a  2 s  .  c om
}

From source file:com.freedomotic.persistence.UserConverter.java

License:Open Source License

/**
 *
 * @param reader/*  ww w.j  a v  a2 s.co m*/
 * @param uc
 * @return
 */
@Override
public Object unmarshal(HierarchicalStreamReader reader, UnmarshallingContext uc) {
    SimplePrincipalCollection pc = new SimplePrincipalCollection();
    User user = null;
    reader.moveDown(); //principals
    while (reader.hasMoreChildren()) {
        reader.moveDown();
        String realm = reader.getAttribute("realm");
        pc.add(reader.getValue(), realm);
        // reader.getAttribute("primary"); // ???
        reader.moveUp();
    }
    reader.moveUp(); //end principals
    reader.moveDown(); // credentials
    user = new User(pc, reader.getValue(), Freedomotic.INJECTOR.getInstance(Auth.class));
    reader.moveUp();
    reader.moveDown(); //roles
    while (reader.hasMoreChildren()) {
        reader.moveDown();
        user.addRole(reader.getAttribute("name"));
        reader.moveUp();
    }
    reader.moveUp();
    reader.moveDown(); //properties
    while (reader.hasMoreChildren()) {
        reader.moveDown();
        user.setProperty(reader.getAttribute("name"), reader.getAttribute("value"));
        reader.moveUp();
    }
    reader.moveUp();
    return user;
}

From source file:com.tensorwrench.shiro.realm.MongoUserPasswordRealmAuthorizationTest.java

License:Apache License

@Test
@MongoData("/principals.json")
public void getsUserRoles() {
    SimplePrincipalCollection principals = new SimplePrincipalCollection();
    principals.add("sample-principal-user", "fooRealm");
    AuthorizationInfo info = realm.doGetAuthorizationInfo(principals);
    assertEqualsNoOrder(info.getRoles().toArray(), new String[] { "role:user" });
}

From source file:com.tensorwrench.shiro.realm.MongoUserPasswordRealmAuthorizationTest.java

License:Apache License

@Test
@MongoData("/principals.json")
public void getsAdminRoles() {
    SimplePrincipalCollection principals = new SimplePrincipalCollection();
    principals.add("sample-principal-admin", "fooRealm");
    AuthorizationInfo info = realm.doGetAuthorizationInfo(principals);
    assertEqualsNoOrder(info.getRoles().toArray(), new String[] { "role:user", "role:admin" });
}

From source file:ddf.security.impl.SubjectImplTest.java

License:Open Source License

private PrincipalCollection createTestCollection() {
    SimplePrincipalCollection collection = new SimplePrincipalCollection();
    collection.add(TEST_SUBJECT_NAME, TEST_REALM_NAME);
    return collection;
}

From source file:ddf.security.realm.sts.AbstractStsRealm.java

License:Open Source License

/**
 * Perform authentication based on the supplied token.
 *///www . jav a2s .c  o  m
@Override
protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken token) {
    String method = "doGetAuthenticationInfo(    AuthenticationToken token )";
    LOGGER.entry(method);

    Object credential;

    if (token instanceof SAMLAuthenticationToken) {
        credential = token.getCredentials();
    } else if (token instanceof BaseAuthenticationToken) {
        credential = ((BaseAuthenticationToken) token).getCredentialsAsXMLString();
    } else {
        credential = token.getCredentials().toString();
    }
    if (credential == null) {
        String msg = "Unable to authenticate credential.  A NULL credential was provided in the supplied authentication token. This may be due to an error with the SSO server that created the token.";
        LOGGER.error(msg);
        throw new AuthenticationException(msg);
    } else {
        //removed the credentials from the log message for now, I don't think we should be dumping user/pass into log
        LOGGER.debug("Received credentials.");
    }

    if (!settingsConfigured) {
        configureStsClient();
        settingsConfigured = true;
    } else {
        setClaimsOnStsClient(createClaimsElement());
    }

    SecurityToken securityToken;
    if (token instanceof SAMLAuthenticationToken && credential instanceof SecurityToken) {
        securityToken = renewSecurityToken((SecurityToken) credential);
    } else {
        securityToken = requestSecurityToken(credential);
    }

    LOGGER.debug("Creating token authentication information with SAML.");
    SimpleAuthenticationInfo simpleAuthenticationInfo = new SimpleAuthenticationInfo();
    SimplePrincipalCollection principals = new SimplePrincipalCollection();
    SecurityAssertion assertion = new SecurityAssertionImpl(securityToken);
    principals.add(assertion.getPrincipal(), NAME);
    principals.add(assertion, NAME);
    simpleAuthenticationInfo.setPrincipals(principals);
    simpleAuthenticationInfo.setCredentials(credential);

    LOGGER.exit(method);
    return simpleAuthenticationInfo;
}

From source file:ddf.security.realm.sts.StsRealm.java

License:Open Source License

/**
 * Creates a new principal object from an incoming security token.
 *
 * @param token SecurityToken that contains the principals.
 * @return new SimplePrincipalCollection
 *//*www  . j  a va 2  s  . c om*/
private SimplePrincipalCollection createPrincipalFromToken(SecurityToken token) {
    SimplePrincipalCollection principals = new SimplePrincipalCollection();
    SecurityAssertion securityAssertion = null;
    try {
        securityAssertion = new SecurityAssertionSaml(token, usernameAttributeList);
        Principal principal = securityAssertion.getPrincipal();
        if (principal != null) {
            principals.add(principal.getName(), getName());
        }
    } catch (Exception e) {
        LOGGER.warn(
                "Encountered error while trying to get the Principal for the SecurityToken. Security functions may not work properly.",
                e);
    }
    if (securityAssertion != null) {
        principals.add(securityAssertion, getName());
    }
    return principals;
}

From source file:ddf.security.service.impl.SecurityManagerImplTest.java

License:Open Source License

/**
 * Creates mock objects and uses those to pass through the system when an authentication token is
 * used./*from   w ww .ja v a 2  s.com*/
 *
 * @throws SecurityServiceException
 */
@Test
public void testAuthToken() throws SecurityServiceException {
    // mock setup
    SimplePrincipalCollection principals = new SimplePrincipalCollection();
    SecurityToken secToken = new SecurityToken();
    principals.add(secToken, REALM_NAME);

    AuthenticationToken authToken = mock(AuthenticationToken.class);
    when(authToken.getCredentials()).thenReturn("testUser");
    AuthenticationInfo info = mock(AuthenticationInfo.class);
    when(info.getPrincipals()).thenReturn(principals);

    // realm
    Realm realm = mock(Realm.class);
    when(realm.getAuthenticationInfo(authToken)).thenReturn(info);
    when(realm.supports(authToken)).thenReturn(Boolean.TRUE);
    when(realm.getName()).thenReturn(REALM_NAME);

    SecurityManagerImpl manager = new SecurityManagerImpl();
    manager.setRealms(Arrays.asList(new Realm[] { realm }));
    Subject subject = manager.getSubject(authToken);
    assertNotNull(subject);
}