gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 03:05:27 27 4
gpt4 key购买 nike

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

关于java - 如何使用 Spring Ldap 模板更新密码?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57025355/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com