class="java" name="code">
//LDAP密码修改
final String host = "192.168.1.101";
final int port = 389;
final LDAPConnectionFactory factory = new LDAPConnectionFactory(host, port);
Connection connection = null;
try {
connection = factory.getConnection();
//checkSupportedExtendedOperations(connection);
extendedOperations=RootDSE.readRootDSE(connection).getSupportedExtendedOperations();
final String ladpAdmin = "cn=Directory Manager";
final char[] ladpAdminPassword = "xerllent".toCharArray();
connection.bind(ladpAdmin, ladpAdminPassword);
// Uncomment a method to run one of the examples.
// For a Cancel Extended request, see the SearchAsync example.
//usePasswordModifyExtendedRequest(connection);
if (extendedOperations != null && !extendedOperations.isEmpty() && extendedOperations.contains(PasswordModifyExtendedRequest.OID)) {
//final String userIdentity = "uid=test3,ou=People,dc=example,dc=com"; //通过
final String userIdentity = "u:test3"; //通过
//final String userIdentity = "uid:test3"; //错误写法
final char[] oldPassword = "test3".toCharArray();
final char[] newPassword = "test3".toCharArray();
final PasswordModifyExtendedRequest ldaprequest =
Requests.newPasswordModifyExtendedRequest()
.setUserIdentity(userIdentity)
.setOldPassword(oldPassword)
.setNewPassword(newPassword);
final PasswordModifyExtendedResult result = connection.extendedRequest(ldaprequest);
if (result.isSuccess()) {
System.out.println("Changed password for " + userIdentity);
} else {
System.err.println(result.getDiagnosticMessage());
}
} else {
System.err.println("PasswordModifyExtendedRequest not supported");
}
} catch (ErrorResultException e) {
System.err.println(e.getMessage());
System.exit(e.getResult().getResultCode().intValue());
return;
} finally {
if (connection != null) {
connection.close();
}
}
--------------------------------------------------------------------------
Changed password for uid=test3,ou=People,dc=example,dc=com
Changed password for u:test3
Protocol Error: 无法处理密码修改扩展请求,因为它包含无效的 userIdentity 字段。提供的 userIdentity 字符串为 "uid:test3"