List of usage examples for org.apache.shiro.subject MutablePrincipalCollection add
void add(Object principal, String realmName);
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; }