如何使用Spring Ldap模板更新密码?

时间:2019-07-14 07:19:41

标签: java spring ldap passwords

我在使用Spring LDAP模板更改密码时遇到问题。我不使用Spring安全性。我通过示例创建了应用程序,然后尝试编写一种更改用户密码的新方法。

我的pojo:

@Data
@AllArgsConstructor
@NoArgsConstructor
@ToString(exclude = "uid")
public class Person {

    private String uid;
    private String fullName;
    private String lastName;
    private String password;

    public Person(String uid, String fullName, String lastName) {
        super();
        this.uid = uid;
        this.fullName = fullName;
        this.lastName = lastName;
    }

}

具有方法的存储库:

@Service
public class PersonRepository implements BaseLdapNameAware {

    @Autowired
    private LdapTemplate ldapTemplate;
    private LdapName baseLdapPath;

    public void setBaseLdapPath(LdapName baseLdapPath) {
        this.baseLdapPath = baseLdapPath;
    }

    public Person findOne(String uid) {
        Name dn = LdapNameBuilder.newInstance(baseLdapPath).add("ou", "people").add("uid", uid).build();
        return ldapTemplate.lookup(dn, new PersonContextMapper());
    }

    public List<Person> findByName(String name) {
        LdapQuery q = query().where("objectclass").is("person").and("cn").whitespaceWildcardsLike(name);
        return ldapTemplate.search(q, new PersonContextMapper());
    }

    public void update(Person p) {
        ldapTemplate.rebind(buildDn(p), null, buildAttributes(p));
    }

    public void updateLastName(Person p) {
        Attribute attr = new BasicAttribute("sn", p.getLastName());
        ModificationItem item = new ModificationItem(DirContext.REPLACE_ATTRIBUTE, attr);
        ldapTemplate.modifyAttributes(buildDn(p), new ModificationItem[] { item });
    }

    public void updatePassword(Person p) {
        Attribute attr = new BasicAttribute("password", p.getPassword());
        ModificationItem item = new ModificationItem(DirContext.REPLACE_ATTRIBUTE, attr);
        ldapTemplate.modifyAttributes(buildDn(p), new ModificationItem[] { item });
    }

    private Name buildDn(Person p) {
        return LdapNameBuilder.newInstance(baseLdapPath).add("ou", "people").add("uid", p.getUid()).build();
    }

    private Attributes buildAttributes(Person p) {
        Attributes attrs = new BasicAttributes();
        BasicAttribute ocAttr = new BasicAttribute("objectclass");
        ocAttr.add("top");
        ocAttr.add("person");
        attrs.put(ocAttr);
        attrs.put("ou", "people");
        attrs.put("uid", p.getUid());
        attrs.put("cn", p.getFullName());
        attrs.put("sn", p.getLastName());
        attrs.put("password", p.getPassword());
        return attrs;
    }

    private static class PersonContextMapper extends AbstractContextMapper<Person> {
        public Person doMapFromContext(DirContextOperations context) {
            Person person = new Person();
            person.setFullName(context.getStringAttribute("cn"));
            person.setLastName(context.getStringAttribute("sn"));
            person.setUid(context.getStringAttribute("uid"));
            return person;
        }
    }
}

然后,当我尝试使用方法“ update”或“ updatePassword”更新密码时,我将收到一个新的异常。

    nested exception is org.springframework.ldap.InvalidAttributeValueException: 'password' has no values.; nested exception is javax.naming.directory.InvalidAttributeValueException: 'password' has no values.; remaining name 'uid=jahn,ou=people,dc=memorynotfound,dc=com'
Caused by: javax.naming.directory.InvalidAttributeValueException: 'password' has no values.

请帮助我使用此方法。如何更新该密码?

1 个答案:

答案 0 :(得分:0)

我认为这是userPassword属性