public LdapUserDetailsManager(ContextSource contextSource) { template = new LdapTemplate(contextSource); }
@Override public List<UserInfo> findByUserIds(List<String> userIds) { if (CollectionUtils.isEmpty(userIds)) { return null; } else { ContainerCriteria criteria = query().where(loginIdAttrName).is(userIds.get(0)); userIds.stream().skip(1).forEach(userId -> criteria.or(loginIdAttrName).is(userId)); return ldapTemplate.search(ldapQueryCriteria().and(criteria), ldapUserInfoMapper); } }
public boolean userExists(String username) { DistinguishedName dn = usernameMapper.buildDn(username); try { Object obj = template.lookup(dn); if (obj instanceof Context) { LdapUtils.closeContext((Context) obj); } return true; } catch (org.springframework.ldap.NameNotFoundException e) { return false; } }
@Bean @ConditionalOnMissingBean(LdapOperations.class) public LdapTemplate ldapTemplate(ContextSource contextSource) { LdapTemplate ldapTemplate = new LdapTemplate(contextSource); ldapTemplate.setIgnorePartialResultException(true); return ldapTemplate; } }
private void changePasswordUsingAttributeModification (DistinguishedName userDn, String oldPassword, String newPassword) { final ModificationItem[] passwordChange = new ModificationItem[] { new ModificationItem( DirContext.REPLACE_ATTRIBUTE, new BasicAttribute(passwordAttributeName, newPassword)) }; if (oldPassword == null) { template.modifyAttributes(userDn, passwordChange); return; } template.executeReadWrite(dirCtx -> { LdapContext ctx = (LdapContext) dirCtx; ctx.removeFromEnvironment("com.sun.jndi.ldap.connect.pool"); ctx.addToEnvironment(Context.SECURITY_PRINCIPAL, LdapUtils.getFullDn(userDn, ctx).toString()); ctx.addToEnvironment(Context.SECURITY_CREDENTIALS, oldPassword); // TODO: reconnect doesn't appear to actually change the credentials try { ctx.reconnect(null); } catch (javax.naming.AuthenticationException e) { throw new BadCredentialsException( "Authentication for password change failed."); } ctx.modifyAttributes(userDn, passwordChange); return null; }); }
LdapTemplateContextSource loadLdap(LdapServerInventory inv, Map<String, Object> baseEnvironmentProperties) { LdapContextSource ldapContextSource = buildLdapContextSource(inv, baseEnvironmentProperties); LdapTemplate ldapTemplate; ldapTemplate = new LdapTemplate(); ldapTemplate.setIgnorePartialResultException(true); ldapTemplate.setContextSource(ldapContextSource); return new LdapTemplateContextSource(ldapTemplate, ldapContextSource); }
public void update(String dn, String fullname, String lastname, String description) { DirContextAdapter ctx = (DirContextAdapter) ldapTemplate.lookup(dn); ctx.setAttributeValue("sn", lastname); ctx.setAttributeValue("description", description); ldapTemplate.modifyAttributes(ctx); }
@Override public void createRecursivelyAndUnbindSubnode() { DirContextAdapter ctx = new DirContextAdapter(); ctx.setAttributeValues("objectclass", new String[]{"top", "organizationalUnit"}); ctx.setAttributeValue("ou", "dummy"); ctx.setAttributeValue("description", "dummy description"); ldapTemplate.bind("ou=dummy", ctx, null); ldapTemplate.bind("ou=dummy,ou=dummy", ctx, null); ldapTemplate.unbind("ou=dummy,ou=dummy"); ldapTemplate.unbind("ou=dummy"); }
public void update(Organisation orga) { Name dn = this.buildDn(orga.getId()); DirContextOperations context = ldapTemplateEcriture.lookupContext(dn); context = this.prepareContextForUpdate(context,orga); ldapTemplateEcriture.modifyAttributes(context); }
private ErrorCode testAddLdapServerConnection(LdapServerInventory inv) { Map<String, Object> properties = new HashMap<>(); String timeout = Integer.toString(LdapGlobalProperty.LDAP_ADD_SERVER_CONNECT_TIMEOUT); properties.put("com.sun.jndi.ldap.connect.timeout", timeout); LdapTemplateContextSource ldapTemplateContextSource = new LdapUtil().loadLdap(inv, properties); try { AndFilter filter = new AndFilter(); // Any search conditions filter.and(new EqualsFilter(LdapConstant.LDAP_UID_KEY, "")); ldapTemplateContextSource.getLdapTemplate().authenticate("", filter.toString(), ""); logger.info("LDAP connection was successful"); } catch (Exception e) { logger.debug("Cannot connect to LDAP server", e); return operr("Cannot connect to LDAP server, %s", e.toString()); } return null; }
public void updateAndRename(String dn, String newDn, String description) { DirContextAdapter ctx = (DirContextAdapter) ldapTemplate.lookup(dn); ctx.setAttributeValue("description", description); ldapTemplate.modifyAttributes(ctx); ldapTemplate.rename(dn, newDn); }
@Test public void testCreate() { dummyDao.create("Sweden", "company1", "some testperson", "testperson", "some description"); log.debug("Verifying result"); String expectedDn = "cn=some testperson, ou=company1, ou=Sweden"; Object ldapResult = ldapTemplate.lookup(expectedDn); assertThat(ldapResult).isNotNull(); ldapTemplate.unbind(expectedDn); }
@Test public void testSearch_PartialResult_IgnoreSet() throws Exception { tested.setIgnorePartialResultException(true); expectGetReadOnlyContext(); when(searchExecutorMock.executeSearch(dirContextMock)).thenThrow(new javax.naming.PartialResultException()); tested.search(searchExecutorMock, handlerMock, dirContextProcessorMock); verify(dirContextProcessorMock).preProcess(dirContextMock); verify(dirContextProcessorMock).postProcess(dirContextMock); verify(dirContextMock).close(); }
public void createUser(UserDetails user) { DirContextAdapter ctx = new DirContextAdapter(); copyToContext(user, ctx); DistinguishedName dn = usernameMapper.buildDn(user.getUsername()); logger.debug("Creating new user '" + user.getUsername() + "' with DN '" + dn + "'"); template.bind(dn, ctx, null); // Check for any existing authorities which might be set for this DN and remove // them List<GrantedAuthority> authorities = getUserAuthorities(dn, user.getUsername()); if (authorities.size() > 0) { removeAuthorities(dn, authorities); } addAuthorities(dn, user.getAuthorities()); }
/** * {@inheritDoc} */ @Override public void search(String base, String filter, NameClassPairCallbackHandler handler) { SearchControls controls = getDefaultSearchControls(defaultSearchScope, DONT_RETURN_OBJ_FLAG, ALL_ATTRIBUTES); if (handler instanceof ContextMapperCallbackHandler) { assureReturnObjFlagSet(controls); } search(base, filter, controls, handler); }
/** * {@inheritDoc} */ @Override public <T> List<T> search(String base, String filter, int searchScope, String[] attrs, ContextMapper<T> mapper) { return search(base, filter, getDefaultSearchControls(searchScope, RETURN_OBJ_FLAG, attrs), mapper); }
public void deleteUser(String username) { DistinguishedName dn = usernameMapper.buildDn(username); removeAuthorities(dn, getUserAuthorities(dn, username)); template.unbind(dn); }
/** * {@inheritDoc} */ @Override public void search(LdapQuery query, NameClassPairCallbackHandler callbackHandler) { SearchControls searchControls = searchControlsForQuery(query, DONT_RETURN_OBJ_FLAG); search(query.base(), query.filter().encode(), searchControls, callbackHandler); }
public void updateUser(UserDetails user) { DistinguishedName dn = usernameMapper.buildDn(user.getUsername()); logger.debug("Updating user '" + user.getUsername() + "' with DN '" + dn + "'"); List<GrantedAuthority> authorities = getUserAuthorities(dn, user.getUsername()); DirContextAdapter ctx = loadUserAsContext(dn, user.getUsername()); ctx.setUpdateMode(true); copyToContext(user, ctx); // Remove the objectclass attribute from the list of mods (if present). List<ModificationItem> mods = new LinkedList<>(Arrays.asList(ctx .getModificationItems())); ListIterator<ModificationItem> modIt = mods.listIterator(); while (modIt.hasNext()) { ModificationItem mod = (ModificationItem) modIt.next(); Attribute a = mod.getAttribute(); if ("objectclass".equalsIgnoreCase(a.getID())) { modIt.remove(); } } template.modifyAttributes(dn, mods.toArray(new ModificationItem[mods.size()])); // template.rebind(dn, ctx, null); // Remove the old authorities and replace them with the new one removeAuthorities(dn, authorities); addAuthorities(dn, user.getAuthorities()); }
@Test public void testList_PartialResultException_Ignore() throws NamingException { expectGetReadOnlyContext(); javax.naming.PartialResultException pre = new javax.naming.PartialResultException(); when(dirContextMock.list(NAME)).thenThrow(pre); tested.setIgnorePartialResultException(true); List list = tested.list(NAME); verify(dirContextMock).close(); assertThat(list).isNotNull(); assertThat(list).isEmpty(); }