簡體   English   中英

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

[英]How update password with Spring Ldap template?

我在使用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.

請幫我用這種方法。 如何更新該密碼?

我認為是userPassword屬性

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM