/** * Assembles the Distinguished Name that should be used the given username. */ public DistinguishedName buildDn(String username) { DistinguishedName dn = new DistinguishedName(userDnBase); dn.add(usernameAttribute, username); return dn; } }
/** * Returns the DN for the context representing this LDAP record. By default this is * using {@link javax.naming.Context#getNameInNamespace()} instead of * {@link org.springframework.ldap.core.DirContextAdapter#getDn()} since the latter * returns a partial DN if a base has been specified. * @param adapter - the Context to extract the DN from * @return - the String representing the full DN */ private String getAdapterDN(DirContextAdapter adapter) { // returns the full DN rather than the sub DN if a base is specified return adapter.getNameInNamespace(); }
private DirContextAdapter loadUserAsContext(final DistinguishedName dn, final String username) { return (DirContextAdapter) template.executeReadOnly(new ContextExecutor() { public Object executeWithContext(DirContext ctx) throws NamingException { try { Attributes attrs = ctx.getAttributes(dn, attributesToRetrieve); return new DirContextAdapter(attrs, LdapUtils.getFullDn(dn, ctx)); } catch (NameNotFoundException notFound) { throw new UsernameNotFoundException( "User " + username + " not found", notFound); } } }); }
@Bean @ConditionalOnMissingBean(LdapOperations.class) public LdapTemplate ldapTemplate(ContextSource contextSource) { LdapTemplate ldapTemplate = new LdapTemplate(contextSource); ldapTemplate.setIgnorePartialResultException(true); return ldapTemplate; } }
/** * * @param dn the distinguished name of the entry - may be either relative to the base * context or a complete DN including the name of the context (either is supported). * @param username the user whose roles are required. * @return the granted authorities returned by the group search */ @SuppressWarnings("unchecked") List<GrantedAuthority> getUserAuthorities(final DistinguishedName dn, final String username) { SearchExecutor se = new SearchExecutor() { public NamingEnumeration<SearchResult> executeSearch(DirContext ctx) throws NamingException { DistinguishedName fullDn = LdapUtils.getFullDn(dn, ctx); SearchControls ctrls = new SearchControls(); ctrls.setReturningAttributes(new String[] { groupRoleAttributeName }); return ctx.search(groupSearchBase, groupSearchFilter, new String[] { fullDn.toUrl(), username }, ctrls); } }; AttributesMapperCallbackHandler roleCollector = new AttributesMapperCallbackHandler( roleMapper); template.search(se, roleCollector); return roleCollector.getList(); }
private DirContextAdapter createUserContext() { DirContextAdapter ctx = new DirContextAdapter(); ctx.setDn(new DistinguishedName("ignored=ignored")); ctx.setAttributeValue("uid", "ghengis"); ctx.setAttributeValue("userPassword", "pillage"); ctx.setAttributeValue("carLicense", "HORS1"); ctx.setAttributeValue("cn", "Ghengis Khan"); ctx.setAttributeValue("description", "Scary"); ctx.setAttributeValue("destinationIndicator", "West"); ctx.setAttributeValue("displayName", "Ghengis McCann"); ctx.setAttributeValue("givenName", "Ghengis"); ctx.setAttributeValue("homePhone", "+467575436521"); ctx.setAttributeValue("initials", "G"); ctx.setAttributeValue("employeeNumber", "00001"); ctx.setAttributeValue("homePostalAddress", "Steppes"); ctx.setAttributeValue("mail", "ghengis@mongolia"); ctx.setAttributeValue("mobile", "always"); ctx.setAttributeValue("o", "Hordes"); ctx.setAttributeValue("ou", "Horde1"); ctx.setAttributeValue("postalAddress", "On the Move"); ctx.setAttributeValue("postalCode", "Changes Frequently"); ctx.setAttributeValue("roomNumber", "Yurt 1"); ctx.setAttributeValue("roomNumber", "Yurt 1"); ctx.setAttributeValue("sn", "Khan"); ctx.setAttributeValue("street", "Westward Avenue"); ctx.setAttributeValue("telephoneNumber", "+442075436521"); return ctx; }
public LdapUserDetailsManager(ContextSource contextSource) { template = new LdapTemplate(contextSource); }
public Object executeWithContext(DirContext ctx) throws NamingException { Attributes attrs = ctx.getAttributes(dn, attributesToRetrieve); // Object object = ctx.lookup(LdapUtils.getRelativeName(dn, ctx)); return new DirContextAdapter(attrs, new DistinguishedName(dn), new DistinguishedName(ctx.getNameInNamespace())); } });
/** * Gets the full dn of a name by prepending the name of the context it is relative to. * If the name already contains the base name, it is returned unaltered. */ public static DistinguishedName getFullDn(DistinguishedName dn, Context baseCtx) throws NamingException { DistinguishedName baseDn = new DistinguishedName(baseCtx.getNameInNamespace()); if (dn.contains(baseDn)) { return dn; } baseDn.append(dn); return baseDn; }
public Essence(DirContextOperations ctx) { super(ctx); setCn(ctx.getStringAttributes("cn")); setGivenName(ctx.getStringAttribute("givenName")); setSn(ctx.getStringAttribute("sn")); setDescription(ctx.getStringAttribute("description")); setTelephoneNumber(ctx.getStringAttribute("telephoneNumber")); Object passo = ctx.getObjectAttribute("userPassword"); if (passo != null) { String password = LdapUtils.convertPasswordToString(passo); setPassword(password); } }
public void setGroupSearchBase(String groupSearchBase) { this.groupSearchBase = new DistinguishedName(groupSearchBase); }
protected void populateContext(DirContextAdapter adapter) { adapter.setAttributeValue("givenName", givenName); adapter.setAttributeValue("sn", sn); adapter.setAttributeValues("cn", getCn()); adapter.setAttributeValue("description", getDescription()); adapter.setAttributeValue("telephoneNumber", getTelephoneNumber()); if (getPassword() != null) { adapter.setAttributeValue("userPassword", getPassword()); } adapter.setAttributeValues("objectclass", new String[] { "top", "person" }); }
public Essence(DirContextOperations ctx) { setDn(ctx.getDn()); }
public void deleteUser(String username) { DistinguishedName dn = usernameMapper.buildDn(username); removeAuthorities(dn, getUserAuthorities(dn, username)); template.unbind(dn); }
private String getPassword(DirContextOperations user) { Object passwordAttrValue = user.getObjectAttribute(this.passwordAttributeName); if (passwordAttrValue == null) { return null; } if (passwordAttrValue instanceof byte[]) { return new String((byte[]) passwordAttrValue); } return String.valueOf(passwordAttrValue); }
@Override public void handleRecord(LdapAttributes attributes) { callbackAttributeName = attributes.getName().toString(); } }
@Override public UserInfo findByUserId(String userId) { return ldapTemplate .searchForObject(ldapQueryCriteria().and(loginIdAttrName).is(userId), ldapUserInfoMapper); }
public Object executeWithContext(DirContext ctx) throws NamingException { Attributes attrs = ctx.getAttributes(dn, attributesToRetrieve); // Object object = ctx.lookup(LdapUtils.getRelativeName(dn, ctx)); return new DirContextAdapter(attrs, new DistinguishedName(dn), new DistinguishedName(ctx.getNameInNamespace())); } });
/** * Creates a DN from a group name. * * @param group the name of the group * @return the DN of the corresponding group, including the groupSearchBase */ protected DistinguishedName buildGroupDn(String group) { DistinguishedName dn = new DistinguishedName(groupSearchBase); dn.add(groupRoleAttributeName, group.toLowerCase()); return dn; }
@Override public void handleRecord(LdapAttributes attributes) { callbackAttributeName = attributes.getName().toString(); } }