This posting is provided "AS IS" with no warranties, and confers no
rights. Use of included script samples are subject to the terms
specified at http://www.microsoft.com/info/cpyright.htm
Business Scenario
Many Java applications now utilize Active Directory as a source of
authentication, in some situations it may be required to set Active
Directory password from within Java applications. I encountered a
scenario where majority of the users of a Java application were on
Active Directory, but for a small percentage of users that do not log-in
to Active Directory from their desktops we needed to provide a
functionality within the application to set user passwords.
Prerequisites
- This scenario was only tested against a Windows 2003 Domain
- JKD 1.5.0_03 was used to run the sample code
- You will need to connect to Active Directory with a user account that has permissions to reset passwords
- PKI Certificates used in this scenario were issued by Microsoft
Certificate Server configured in Active Directory integrated mode
Setup SSL trust between Active Directory Domain Controller(s) and Java application
Active Directory Domain Controllers will only allow password set
operations over an SSL channel, therefore both parties should have a
common trusted root certificate in their certificate stores. The
simplest way to accomplish this is to export a trusted root certificate
from a Domain Controller and import it into Java certificate store on
the client machine.
Configuring SSL on Active Directory Domain Controllers
Active Directory Domain Controllers automatically enroll for domain
controller certificate and utilize it for secure LDAP communications if
Active Directory integrated Microsoft Certificate Server is deployed
within the Forest. So in other words, if you deployed Microsoft
Certificate Server in Active Directory integrated mode, then you don't
need to do anything else on Active Directory side, all domain
controllers will use SSL on port 636.
For instructions on how to setup Microsoft Certificate Server follow this link
.
Importing Trusted Root Certificate on a Java client machine
On the client side we need to import a mutually trusted root
certificate into Java certificate store. In our case we will export the
root certificate issued by Microsoft Certificate Server and import it
into Java store on the client.
1. On a Domain Controller log-in as an administrator and open
Internet Explorer. Go to Tools->Internet Options->Content and
click on Certificates
2. Switch to Trusted Root Certificate Authorities Tab and Select the
certificate issued by your Active Directory integrated Certificate
Server. Click on Export
3. Choose Base-64 encoded X.509(.CER)
4. Specify file name for the exported certificate
5. Finish the export and copy the exported .cer file to the Java client machine
6. At the client machine execute the following command.
Note the location of the jks file, you will need to reference it later on in the code.
Alias and keystore password are arbitrary values
Sample program to change Active Directory user password from Java
Now that SSL staff is out of the way compile this sample code and run it from the Java client
This code was developed by Jeremy Mortis
here is link to the original code
import javax.naming.*;
import javax.naming.directory.*;
import javax.naming.ldap.*;
import java.util.*;
import java.security.*;
public class ADConnection {
DirContext ldapContext;
String baseName = ",cn=users,DC=fabrikam,DC=com";
String serverIP = "10.1.1.7";
public ADConnection() {
try {
Hashtable ldapEnv = new Hashtable(11);
ldapEnv.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory");
ldapEnv.put(Context.PROVIDER_URL, "ldap://" + serverIP + ":636");
ldapEnv.put(Context.SECURITY_AUTHENTICATION, "simple");
ldapEnv.put(Context.SECURITY_PRINCIPAL, "cn=administrator" + baseName);
ldapEnv.put(Context.SECURITY_CREDENTIALS, "PA$$w0rd");
ldapEnv.put(Context.SECURITY_PROTOCOL, "ssl");
ldapContext = new InitialDirContext(ldapEnv);
}
catch (Exception e) {
System.out.println(" bind error: " + e);
e.printStackTrace();
System.exit(-1);
}
}
public void updatePassword(String username, String password) {
try {
String quotedPassword = "\"" + password + "\"";
char unicodePwd[] = quotedPassword.toCharArray();
byte pwdArray[] = new byte[unicodePwd.length * 2];
for (int i=0; i<unicodePwd.length; i++) {
pwdArray[i*2 + 1] = (byte) (unicodePwd[i] >>> 8);
pwdArray[i*2 + 0] = (byte) (unicodePwd[i] & 0xff);
}
ModificationItem[] mods = new ModificationItem[1];
mods[0] = new ModificationItem(DirContext.REPLACE_ATTRIBUTE,
new BasicAttribute("UnicodePwd", pwdArray));
ldapContext.modifyAttributes("cn=" + username + baseName, mods);
}
catch (Exception e) {
System.out.println("update password error: " + e);
System.exit(-1);
}
}
public static void main(String[] args) {
Security.addProvider(new com.sun.net.ssl.internal.ssl.Provider());
// the keystore that holds trusted root certificates
System.setProperty("javax.net.ssl.trustStore", "c:\\myCaCerts.jks");
System.setProperty("javax.net.debug", "all");
ADConnection adc = new ADConnection();
adc.updatePassword("Java User2", "pass@word3");
}
}
分享到:
相关推荐
Covers how to back up Active Directory, perform authoritative and nonauthoritative restores, check DIT file integrity, perform online and offline defrags, and search for deleted objects. Chapter 17, ...
Migration from Windows Server 2008 and 2008 R2 to 2012 How-to
根据给定的文件信息,我们将详细讨论关于Syngress出版的电子书《How to Cheat at Installing, Configuring and Troubleshooting Active Directory and DNS》中所涉及的核心知识点。该书的重点在于为读者提供快速的、...
### 如何(几乎)通过C#操作Active Directory中的所有事务 #### 引言 当涉及到通过编程方式访问Microsoft的Active Directory时,许多开发者似乎在整合各个部分以完成所需任务方面遇到了不少困难。由于存在多种与...
由于提供的文件内容主要是一本名为《Active Directory Domain Services 2008 How-To》的书籍的版权信息和出版细节,并没有包含具体的IT知识点,所以无法直接从这部分内容生成详细的IT知识点。但是,可以基于书籍标题...
It introduces how to setup for OpenMP in Visual Studio 2005 with Inter Fortran 10.1. With a simple 'Hello world ' example
《Java How to Program 9th》是一本专为初学者和有一定基础的程序员设计的Java编程教程,旨在全面深入地介绍Java编程语言。这个版本是该书的第九版,更新了最新的Java SE 11内容,确保读者能够掌握当前最前沿的编程...
### Java How to Program (Late Objects) - 关键知识点解析 #### 一、书籍概述 《Java How to Program》(第十版,对象延迟版本)是一本经典的计算机编程教材,旨在为初学者提供系统全面的Java语言学习指导。本书...
《Java: How to Program, 9th Edition》是由Paul Deitel和Harvey Deitel合著的一本经典Java编程教程,适合初学者和有一定经验的程序员深入学习Java语言。这本书全面覆盖了Java的基础知识,进阶特性,以及面向对象...
### Java How to Program, Early Objects, 11th Edition #### 核心知识点概览 本书《Java How to Program, Early Objects, 11th Edition》由Deitel出版社出版,是一本面向初学者和有一定编程基础的学习者介绍Java...
LDAP Class, Very Usefull Library & Class, ToShow How To Work With Active Directory From LDAP In DElphi
教材是java how to program,这是第一二章的英文ppt课件
本书以初学者为起点,循序渐进地介绍了面向对象的Java编程语言,系统地讨论了Java的基本概念和编程技术。全书共分为18章,首先从基本的Java理论开始,讲解了Java的基本数据类型和控制结构,Java中的方法、数组和字符...
Java How to Program, Early Objects, 11th Edition Java How to Program, Early Objects, 11th Edition Java How to Program, Early Objects, 11th Edition Java How to Program, Early Objects, 11th Edition
How To Set Up and Use the SAP ME Assembly Feature
How to set up OpenScape Office V3 LX Demo
Think Java: How to Think Like a Computer Scientist by Allen B. Downey, Chris Mayfield 2016 | ISBN: 1491929561 Currently used at many colleges, universities, and high schools, this hands-on ...