我正在开发一个 Web 应用程序,让用户可以在 Active Directory 中重置自己的密码。我一直通过以管理员身份绑定来做到这一点,它工作正常,但没有强制执行目录策略(重用历史、字符等)。我无法以用户身份绑定,因为我没有当前密码。
我读到了 Windows 2008 R2 SP1 中引入的 LDAP_SERVER_POLICY_HINTS 控件,用于在 Active Directory 中执行此操作,甚至发现有人使用 Spring LDAP 进行操作
由于我使用的是 UnboundID 并且没有为此提供标准控件,因此我认为我必须创建自己的控件类。记录的 OID 是 1.2.840.113556.1.4.2239 和值 {48, 3, 2, 1, 1}
public class PolicyHintsControl extends Control {
private static final long serialVersionUID = 1L;
public final static String LDAP_SERVER_POLICY_HINTS_OID = "1.2.840.113556.1.4.2066";
public final static byte[] LDAP_SERVER_POLICY_HINTS_DATA = { 48,
(byte) 132, 0, 0, 0, 3, 2, 1, 1 };
public PolicyHintsControl() {
super(LDAP_SERVER_POLICY_HINTS_OID, false, new ASN1OctetString(
LDAP_SERVER_POLICY_HINTS_DATA));
}
@Override
public String getControlName() {
return "LDAP Server Policy Hints Control";
}
@Override
public void toString(StringBuilder buffer) {
buffer.append("LDAPServerPolicyHints(isCritical=");
buffer.append(isCritical());
buffer.append(')');
}
}
所以我在修改请求中添加了这个新控件,如下所示:
public static void main(String[] args) throws Exception {
final String host = "ldap.example.com";
final int port = 636;
String adminDn = "admin@example.com";
String adminPassword = "passwd";
String userDn = "CN=user,ou=people,dc=example,dc=com";
String userPassword = "passwd";
String keystoreFile = "/path/to/keystore.jks";
String keystorePassword = "passwd";
String passwordAttribute = "unicodePwd";
//Password change requires SSL
KeyStore keyStore = KeyStore.getInstance(KeyStore.getDefaultType());
keyStore.load(new FileInputStream(keystoreFile), keystorePassword.toCharArray());
TrustManagerFactory factory = TrustManagerFactory.getInstance("x509");
factory.init(keyStore);
final SSLUtil sslUtil = new SSLUtil(factory.getTrustManagers());
SSLSocketFactory socketFactory = sslUtil.createSSLSocketFactory();
Debug.setEnabled(true);
// Connect as the configured administrator
LDAPConnection ldapConnection = new LDAPConnection(socketFactory, host,
port, adminDn, adminPassword);
// Set password in AD format
final String newQuotedPassword = "\"" + userPassword + "\"";
final byte[] newPasswordBytes = newQuotedPassword.getBytes("UTF-16LE");
String encryptedNewPwd = new String(newPasswordBytes);
//Build modifications array and request
final ArrayList<Modification> modifications = new ArrayList<Modification>();
modifications.add(new Modification(ModificationType.REPLACE,
passwordAttribute, encryptedNewPwd));
ModifyRequest modifyRequest = new ModifyRequest(userDn, modifications);
//Add the policy hints control
modifyRequest.addControl(new PolicyHintsControl());
//Modify already
ldapConnection.modify(modifyRequest);
ldapConnection.close();
}
我得到以下异常:
Exception in thread "main" LDAPException(resultCode=53 (unwilling to perform), errorMessage='0000052D: SvcErr: DSID-031A120C, problem 5003 (WILL_NOT_PERFORM), data 0
', diagnosticMessage='0000052D: SvcErr: DSID-031A120C, problem 5003 (WILL_NOT_PERFORM), data 0
')
经过更多研究后,我发现 Windows 2012 中针对同一控件的另一个更新将 OID 更改为 1.2.840.113556.1.4.2066 并弃用了旧的 OID。
由于可以使用任何版本的 AD 配置此应用程序,因此我想优雅地处理每种情况(Windows 2012、Windows 2008 R2 SP1 等)。我的问题是:
- 有没有人用 UnboundID 成功地做到了这一点?
- 无论如何要知道在修改请求之前控件是否可用?
- 为同一控件处理不同版本的 AD 的不同 OID 的最佳方法是什么?同班还是不同班?