【问题标题】:How update password with Spring Ldap template?如何使用 Spring Ldap 模板更新密码?
【发布时间】:2019-11-23 07:21:58
【问题描述】:

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

我的宝乔:

@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.

请帮我用这个方法。如何更新密码?

【问题讨论】:

    标签: java spring ldap passwords


    【解决方案1】:

    我认为是用户密码属性

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-09-13
      • 1970-01-01
      • 2020-04-27
      • 1970-01-01
      • 2020-02-29
      • 2020-12-21
      • 1970-01-01
      • 2014-10-21
      相关资源
      最近更新 更多