Ldap AD
9485 단어 LDAPadspring-ldap
이
public LdapContext getLdapContext() throws NamingException {
String userName = "App01"; //
String password = "password"; //
String host = "192.168.1.1"; // AD
String port = "389"; //
String url = new String("ldap://" + host + ":" + port);
Hashtable env = new Hashtable();
env.put(Context.SECURITY_AUTHENTICATION, "simple");// simple
env.put(Context.SECURITY_PRINCIPAL,
"cn=App01,cn=users,DC=com"); //
env.put(Context.SECURITY_CREDENTIALS, password);
env.put(Context.INITIAL_CONTEXT_FACTORY,
"com.sun.jndi.ldap.LdapCtxFactory");
env.put(Context.PROVIDER_URL, url);
return new InitialLdapContext(env, null);
}
삼
public void add() {
try {
String newUserName = "test1";
BasicAttributes attrs = new BasicAttributes();
BasicAttribute objclassSet = new BasicAttribute("objectclass");
objclassSet.add("person");
objclassSet.add("top");
objclassSet.add("organizationalPerson");
objclassSet.add("user");
attrs.put(objclassSet);
attrs.put("sn", newUserName);
attrs.put("uid", newUserName);
attrs.put("cn", newUserName);
attrs.put("userPassword", "password");
attrs.put("sAMAccountName","test1");
attrs.put("userAccountControl","2");
attrs.put("mail",newUserName+"@163.com");
attrs.put("displayName"," ");
attrs.put("homePhone","666666");
attrs.put("telephoneNumber","13888888888");
attrs.put("title","Test1");
getLdapContext().createSubcontext("cn=" + newUserName + ",cn=users,DC=com", attrs);
} catch (Exception e) {
e.printStackTrace();
}
}
사
public void testModify() {
String uid = "test1";
String userDN = "cn=" + uid + ",cn=users,DC=com";
Attributes attrs = new BasicAttributes(true);
attrs.put("userPassword", "test2");
attrs.put("title", "Manager");
try {
getLdapContext().modifyAttributes(userDN, DirContext.REPLACE_ATTRIBUTE, attrs);
} catch (NamingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
오
public void removeUser(String userName){
try {
getLdapContext().destroySubcontext("cn=" + userName + ",cn=users,DC=com");
} catch (NamingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
육
public void search() {
SearchControls searchCtls = new SearchControls(); // Create the search
// controls
searchCtls.setSearchScope(SearchControls.SUBTREE_SCOPE); // Specify
String searchFilter = "(&(objectClass=user)(cn=test2))";//"(objectClass=user)";//
String[] returnedAtts = new String[] { "distinguishedName","userPassword",
"department","title","userPassword","sAMAccountName", "flags", "displayName","whenChanged" };
searchCtls.setReturningAttributes(returnedAtts); //
String searchBase = "DC=com";
NamingEnumeration<SearchResult> answer = null;
List<Map<String, String>> adList = new ArrayList<Map<String, String>>();
try {
answer = this.getLdapContext().search(searchBase, searchFilter,
searchCtls);
while (answer.hasMoreElements()) {
SearchResult searchResult = answer.next();
Attributes attributes = searchResult.getAttributes();
Map<String, String> accountInfo = new HashMap<String, String>();
for (NamingEnumeration<?> namingEnumeration = attributes
.getAll(); namingEnumeration.hasMore();) {
Attribute attribute = (Attribute) namingEnumeration.next();
String attrId = attribute.getID().toString();
attribute.getAttributeDefinition();
int i = 0;
String attrValue = "";
for (NamingEnumeration<?> e = attribute.getAll(); e
.hasMore();) {
String val = e.next().toString();
if (i != 0) {
attrValue += ";";
}
i++;
attrValue += val;
}
System.out.println("attrId:" + attrId+" attrValue:" + attrValue);
accountInfo.put(attrId, attrValue);
}
adList.add(accountInfo);
System.out.println("
");
}
System.out.println("size:" + adList.size());
} catch (AuthenticationException e) {
e.printStackTrace();
System.out.println("AD !");
} catch (NamingException e) {
e.printStackTrace();
System.out.println("AD , !");
}
}
7 Spring Ldap
private LdapTemplate ldapTemplate;
public void setLdapTemplate(LdapTemplate ldapTemplate) {
this.ldapTemplate = ldapTemplate;
}
public void getAllUser() {
AndFilter andFilter = new AndFilter();
andFilter.and(new EqualsFilter("objectclass", "person"));
//andFilter.and(new EqualsFilter("cn", "xwl"));
List list = ldapTemplate.search("cn=users,DC=com", andFilter.encode(),
new UserAttributeMapper());
for(Object u:list){
System.out.println(((Users)u).getName());
System.out.println(((Users)u).getPwd()+"
");
}
System.out.println(list.size());
}
public void bind1() {
BasicAttribute objclassSet = new BasicAttribute("objectclass");
objclassSet.add("person");
objclassSet.add("top");
objclassSet.add("organizationalPerson");
objclassSet.add("user");
Attributes attr = new BasicAttributes();
attr.put(objclassSet);
// , null
attr.put("sn", "test1");
attr.put("uid", "test1");
attr.put("cn", "xwl1");
attr.put("sAMAccountName", "test1");
attr.put("userPassword", "1qa2ws3ed54");
attr.put("userAccountControl", "2");
attr.put("mail", "[email protected]");
ldapTemplate.bind(("cn=xwl1,cn=users,DC=com"), null, attr);
}
public static void main(String[] args) {
ApplicationContext cxt = new ClassPathXmlApplicationContext("app_ldap.xml");
LdapPersonInfoImpl userDao = (LdapPersonInfoImpl) cxt
.getBean("ldapPersonInfoImpl");
//List<String> users =
userDao.getAllUser();//getAllPersonNames();
// for(String str:users)
// System.out.println(str);
// userDao.bind1();
}
public class UserAttributeMapper implements AttributesMapper {
private Logger log=Logger.getLogger(UserAttributeMapper.class);
@Override
public Object mapFromAttributes(Attributes attr) throws NamingException {
Users user = new Users();
user.setName(attr.get("sAMAccountName").get().toString());
try {
user.setPwd(new String((byte[])attr.get(LdapContextSourceBean.AD_USER_PASS_WORD).get(), "GB2312"));
} catch (Exception e) {
log.error(" User Passwrod get fail",e);
}
return user;
}
}
팔
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">
<beans>
<bean id="contextSource"
class="org.springframework.ldap.core.support.LdapContextSource">
<property name="url" value="ldap://192.168.1.1:389" />
<property name="userDn" value="cn=App01,cn=users,DC=com" />
<property name="password" value="password" />
</bean>
<bean id="ldapTemplate"
class="org.springframework.ldap.core.LdapTemplate">
<constructor-arg ref="contextSource" />
</bean>
<bean id="ldapPersonInfoImpl" class="com.ladp.LdapPersonInfoImpl">
<property name="ldapTemplate">
<ref bean="ldapTemplate" />
</property>
</bean>
</beans>
spring-ldap-core-1.3.2.RELEASE.jar
구
십
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
LDAPS를 사용한 Redmine과의 AD 연동 설정이 기사는 지식과 경험이 너무 부족한 신인 SE가 쓴다 3월 Microsoft의 보안 업데이트와 함께, 이제 LDAP 서명, LDAP 채널 바인딩이 기본적으로 활성화됩니다. 영향을 주지하고 있는 소프트웨어 중에는 L...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.