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

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

Introduction

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

Prototype

void add(Object principal, String realmName);

Source Link

Document

Adds the given principal to this collection.

Usage

From source file:org.solrsystem.ingest.shiro.JndiLdapRealmWithUser.java

License:Apache License

@Override
protected AuthenticationInfo createAuthenticationInfo(AuthenticationToken token, Object ldapPrincipal,
        Object ldapCredentials, LdapContext ldapContext) throws NamingException {
    SimpleAuthenticationInfo authenticationInfo = (SimpleAuthenticationInfo) super.createAuthenticationInfo(
            token, ldapPrincipal, ldapCredentials, ldapContext);
    MutablePrincipalCollection mpc = (MutablePrincipalCollection) authenticationInfo.getPrincipals();
    final SearchControls constraints = new SearchControls();
    constraints.setSearchScope(SearchControls.SUBTREE_SCOPE);

    // get all attributes
    constraints.setReturningAttributes(null);
    String templ = getUserDnTemplate();
    String userDn = MessageFormat.format(templ, mpc.getPrimaryPrincipal());
    final NamingEnumeration<SearchResult> answer = ldapContext.search(userDn, "(objectClass=*)", constraints);

    if (answer.hasMore()) {
        Attributes attrs = answer.next().getAttributes();
        if (answer.hasMore()) {
            throw new NamingException("Non-unique user specified by:" + userDn);
        }//w  ww.j a v  a 2 s. c o m
        //TODO: make this Guicy
        User user = new UserFromLdap(attrs, mpc);

        // at present there should only be one realm involved.
        Iterator<String> realmIter = mpc.getRealmNames().iterator();
        String firstRealm = realmIter.next();
        if (realmIter.hasNext()) {
            // ugh, need a new solution here
            String explanation = String.format("More than one realm found! (%s and %s)", firstRealm,
                    realmIter.next());
            throw new NamingException(explanation);
        }
        mpc.add(user, firstRealm);
    } else {
        throw new NamingException("Invalid User specified by:" + userDn);
    }

    return authenticationInfo;
}