如何检索属性";“独角兽”;在activedirectory中通过java编程实现
首先,我为我糟糕的英语道歉。我是巴西人,所以如果课文有任何错误,请不要介意 我在这里读了很多关于在Active Directory中检索属性“unicodePwd”的文章,但没有一篇真正帮助了我 我为什么需要这些信息?我会解释: 我这里有一些java例程,它们将用户信息从不同的系统统一到另一个系统。 该例程获取主Oracle数据库中所需的信息,并在其他数据库(基本上是Oracle和MySQL)中设置信息 例如:我们有一个私有云系统,运行在CentOS Linux操作系统中,它有自己的MySQL数据库。为了统一用户信息,包括用户密码,我们从Oracle主数据库获取信息,并设置do本系统的MySQL数据库,以统一用户详细信息和登录信息 我在这里的所有常规都很有效,没有问题,但现在我们有了一个新的挑战 我们需要与我们的Active Directory用户进行同样的统一,获取这个主要Oracle数据库中所需的信息,然后将所有信息设置为Active Directory用户,包括用户密码 我已经在Active Directory用户中成功地更新了密码,但我不希望每次运行此java例程时都更新密码,而只希望在主Oracle数据库中更改密码时更新密码 示例:当其中一个用户更改Oracle主数据库中的密码时,java例程将获取该用户信息,然后在Active Directory中的同一用户中进行设置。为了正确地执行此操作,例程在Active Directory中获取相同的信息,然后比较两个密码(Oracle的密码和Active Directory的密码),最后,如果密码不同,例程将更新密码,但如果密码不不同,例程将不执行任何操作 这就是为什么我需要在Active Directory中检索属性“unicodePwd” 以下是我的一些代码:如何检索属性";“独角兽”;在activedirectory中通过java编程实现,java,active-directory,utf-16le,Java,Active Directory,Utf 16le,首先,我为我糟糕的英语道歉。我是巴西人,所以如果课文有任何错误,请不要介意 我在这里读了很多关于在Active Directory中检索属性“unicodePwd”的文章,但没有一篇真正帮助了我 我为什么需要这些信息?我会解释: 我这里有一些java例程,它们将用户信息从不同的系统统一到另一个系统。 该例程获取主Oracle数据库中所需的信息,并在其他数据库(基本上是Oracle和MySQL)中设置信息 例如:我们有一个私有云系统,运行在CentOS Linux操作系统中,它有自己的MySQL数
import java.util.Hashtable;
import javax.naming.Context;
import javax.naming.NamingEnumeration;
import javax.naming.NamingException;
import javax.naming.directory.*;
import org.apache.commons.mail.EmailException;
import javax.naming.ldap.InitialLdapContext;
import javax.naming.ldap.LdapContext;
public class ldapQuery {
String distinguishedName = "";
String department = "";
String physicalDeliveryOfficeName = "";
String telephoneNumber = "";
String mobile = "";
String title = "";
String sAMAccountName = "";
String unicodePwd = "";
public ldapQuery(String mail) {
try {
final Hashtable<String, String> env = new Hashtable<String, String>();
final String adminName = "CN=MY DOMAIN ADMIN,CN=MY DOMAIN ADMIN FOLDER LOCALIZATION,DC=MY DOMAIN,DC=MY DOMAIN,DC=MY DOMAIN";
final String adminPasswd = "MY DOMAIN ADMIN PASSWORD";
final String ldapUrl = "ldaps://MY ACTIVE DIRECTORY SERVER:636";
final String factory = "com.sun.jndi.ldap.LdapCtxFactory";
final String authType = "simple";
final String protocol = "ssl";
env.put(Context.INITIAL_CONTEXT_FACTORY, factory);
env.put(Context.SECURITY_AUTHENTICATION, authType);
env.put(Context.SECURITY_PRINCIPAL, adminName);
env.put(Context.SECURITY_CREDENTIALS, adminPasswd);
env.put(Context.SECURITY_PROTOCOL, protocol);
env.put(Context.PROVIDER_URL, ldapUrl);
DirContext ctx = new InitialLdapContext (env,null);
SearchControls searchCtls = new SearchControls();
String returnedAtts[] = {"sAMAccountName", "distinguishedName","department", "physicalDeliveryOfficeName", "telephoneNumber", "mobile", "title", "unicodePwd"};
searchCtls.setReturningAttributes(returnedAtts);
searchCtls.setSearchScope(SearchControls.SUBTREE_SCOPE);
String searchFilter = "(&(objectClass=user)(mail=" + mail +"))";
String searchBase = "DC=MY DOMAIN,DC=MY DOMAIN,DC=MY DOMAIN";
int totalResults = 0;
NamingEnumeration<SearchResult> answer =ctx.search(searchBase, searchFilter, searchCtls);
while (answer.hasMoreElements()) {
SearchResult sr = (SearchResult)answer.next();
totalResults++;
Attributes attrs = sr.getAttributes();
if (attrs != null) {
distinguishedName = (String) attrs.get("distinguishedName").get();
department = (String) attrs.get("department").get();
physicalDeliveryOfficeName = (String) attrs.get("physicalDeliveryOfficeName").get();
telephoneNumber = (String) attrs.get("telephoneNumber").get();
mobile = (String) attrs.get("mobile").get();
title = (String) attrs.get("title").get();
sAMAccountName = (String) attrs.get("sAMAccountName").get();
Attribute passwd = attrs.get("unicodePwd");
unicodePwd = unicodePwd + passwd;
if (department == null) {
department = "";
}
if (physicalDeliveryOfficeName == null) {
physicalDeliveryOfficeName = "";
}
if (telephoneNumber == null) {
telephoneNumber = "";
}
if (mobile == null) {
mobile = "";
}
if (title == null) {
title = "";
}
}
}
}
catch (NamingException e){
System.err.println("FAIL MESSAGE: " + e);
}
}
public String ldapSearchResultDistinguishedName() {
return distinguishedName;
}
public String ldapSearchResultDepartment() {
return department;
}
public String ldapSearchResultPhysicalDeliveryOfficeName() {
return physicalDeliveryOfficeName;
}
public String ldapSearchResultTelephoneNumber() {
return telephoneNumber;
}
public String ldapSearchResultMobile() {
return mobile;
}
public String ldapSearchResultTitle() {
return title;
}
public String ldapSearchResultUnicodePwd() {
return unicodePwd;
}
public String ldapSearchResultSAMAccountName() {
return sAMAccountName;
}
}
import java.util.Hashtable;
导入javax.naming.Context;
导入javax.naming.NamingEnumeration;
导入javax.naming.NamingException;
导入javax.naming.directory.*;
导入org.apache.commons.mail.EmailException;
导入javax.naming.ldap.InitialLdapContext;
导入javax.naming.ldap.LdapContext;
公共类ldapQuery{
字符串differentiedName=“”;
字符串部门=”;
字符串physicalDeliveryOfficeName=“”;
字符串号=”;
字符串mobile=“”;
字符串标题=”;
字符串sAMAccountName=“”;
字符串unicodePwd=“”;
公共ldapQuery(字符串邮件){
试一试{
final Hashtable env=新Hashtable();
最后一个字符串adminName=“CN=MY DOMAIN ADMIN,CN=MY DOMAIN ADMIN文件夹本地化,DC=MY DOMAIN,DC=MY DOMAIN,DC=MY DOMAIN”;
最后一个字符串adminPasswd=“我的域管理员密码”;
最后一个字符串ldapUrl=”ldaps://MY ACTIVE DIRECTORY服务器:636”;
最后一个字符串factory=“com.sun.jndi.ldap.LdapCtxFactory”;
最后一个字符串authType=“simple”;
最终字符串协议=“ssl”;
环境投入(Context.INITIAL\u Context\u工厂,工厂);
环境put(Context.SECURITY\u身份验证,authType);
环境放置(Context.SECURITY\u主体,adminName);
环境put(Context.SECURITY\u凭证,adminPasswd);
环境put(Context.SECURITY_协议,协议);
env.put(Context.PROVIDER\u URL,ldapUrl);
DirContext ctx=新的初始值ldapcontext(env,null);
SearchControls searchCtls=新的SearchControls();
字符串returnedAtts[]={“sAMAccountName”、“DifferentiedName”、“department”、“physicalDeliveryOfficeName”、“telephoneNumber”、“mobile”、“title”、“Unicode Depwd”};
searchCtls.设置ReturningAttributes(returnedAtts);
searchCtls.setSearchScope(SearchControls.SUBTREE_范围);
字符串searchFilter=“(&(objectClass=user)(mail=“+mail+”)”);
String searchBase=“DC=MY-DOMAIN,DC=MY-DOMAIN,DC=MY-DOMAIN”;
int totalResults=0;
NamingEnumeration answer=ctx.search(searchBase、searchFilter、searchCtls);
while(answer.hasMoreElements()){
SearchResult sr=(SearchResult)answer.next();
totalResults++;
Attributes attrs=sr.getAttributes();
如果(属性!=null){
DifferentizedName=(字符串)attrs.get(“DifferentizedName”).get();
department=(字符串)attrs.get(“department”).get();
physicalDeliveryOfficeName=(字符串)attrs.get(“physicalDeliveryOfficeName”).get();
电话号码=(字符串)attrs.get(“电话号码”).get();
mobile=(字符串)attrs.get(“mobile”).get();
title=(字符串)attrs.get(“title”).get();
sAMAccountName=(字符串)attrs.get(“sAMAccountName”).get();
属性passwd=attrs.get(“unicodePwd”);
unicodePwd=unicodePwd+passwd;
如果(部门==null){
部门=”;
}
如果(physicalDeliveryOfficeName==null){
physicalDeliveryOfficeName=“”;
}
如果(电话号码==null){
电话号码=”;
}
if(mobile==null){
mobile=“”;
}
if(title==null){
title=“”;
}
}
}
}
捕获(NamingE例外){
System.err.println(“失败消息:+e”);
}
}
普布利