Commit 5b9b3346 authored by benjamin.ertl's avatar benjamin.ertl
Browse files

add spring ldap

parent 281d30d1
......@@ -74,6 +74,35 @@
<version>4.1</version>
</dependency>
<!-- Spring -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>4.2.4.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>4.2.4.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
<version>4.2.4.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-commons</artifactId>
<version>1.11.2.RELEASE</version>
</dependency>
<!-- LDAP -->
<dependency>
<groupId>org.springframework.ldap</groupId>
<artifactId>spring-ldap-core</artifactId>
<version>2.0.4.RELEASE</version>
</dependency>
<!-- OpenID Connect -->
<dependency>
<groupId>com.nimbusds</groupId>
......
package edu.kit.scc;
import java.util.List;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.AbstractApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
import edu.kit.scc.dao.UserDAO;
import edu.kit.scc.dto.UserDTO;
import edu.kit.scc.http.HttpClient;
import edu.kit.scc.http.HttpResponse;
import edu.kit.scc.ldap.LDAPUserDAO;
public class Main {
......@@ -29,11 +41,32 @@ public class Main {
// scimClient.getUsers("admin", "admin");
// scimClient.getGroups("admin", "admin");
Utils.printProperties();
HttpClient client = new HttpClient();
HttpResponse response = client.makeHTTPPostRequest("password=password", "http://localhost:50070");
log.debug(response.toString());
// Utils.printProperties();
// HttpClient client = new HttpClient();
// HttpResponse response =
// client.makeHTTPPostRequest("password=password",
// "http://localhost:50070");
// log.debug(response.toString());
// client.makePOST("localhost", 50070, "user", "password",
// "http://localhost:50070");
// Resource resource = new ClassPathResource("springldap.xml");
// BeanFactory factory = new XmlBeanFactory(resource);
ApplicationContext ctx = new ClassPathXmlApplicationContext("springldap.xml");
UserDAO ldapUser = (LDAPUserDAO) ctx.getBean("ldapUser");
List<UserDTO> userList = ldapUser.getAllUserNames();
for (int i = 0; i < userList.size(); i++)
log.info("User name {}", ((UserDTO) userList.get(i)).getCommonName());
List<UserDTO> userDetails = ldapUser.getUserDetails("John Smith", "Smith");
for (int i = 0; i < userDetails.size(); i++)
log.info("Description {}", ((UserDTO) userDetails.get(i)).getDescription());
UserDTO newUser = new UserDTO();
newUser.setCommonName("me");
newUser.setLastName("too");
ldapUser.insertUser(newUser);
((AbstractApplicationContext) ctx).close();
}
}
package edu.kit.scc.dao;
import java.util.List;
import edu.kit.scc.dto.UserDTO;
public interface UserDAO {
public List<UserDTO> getAllUserNames();
public List<UserDTO> getUserDetails(String commonName, String lastName);
public void insertUser(UserDTO userDTO);
public void updateUser(UserDTO userDTO);
public void deleteUser(UserDTO userDTO);
}
package edu.kit.scc.dto;
public class UserDTO {
String commonName;
String lastName;
String description;
public String getCommonName() {
return commonName;
}
public void setCommonName(String commonName) {
this.commonName = commonName;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
}
\ No newline at end of file
package edu.kit.scc.ldap;
import java.util.List;
import javax.naming.InvalidNameException;
import javax.naming.directory.Attributes;
import javax.naming.directory.BasicAttribute;
import javax.naming.directory.BasicAttributes;
import javax.naming.ldap.LdapName;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.ldap.core.LdapTemplate;
import org.springframework.ldap.filter.AndFilter;
import org.springframework.ldap.filter.EqualsFilter;
import org.springframework.ldap.support.LdapUtils;
import edu.kit.scc.dao.UserDAO;
import edu.kit.scc.dto.UserDTO;
public class LDAPUserDAO implements UserDAO {
private static final Logger log = LoggerFactory.getLogger(LDAPUserDAO.class);
private LdapTemplate ldapTemplate;
public void setLdapTemplate(LdapTemplate ldapTemplate) {
this.ldapTemplate = ldapTemplate;
}
@Override
public List<UserDTO> getAllUserNames() {
return ldapTemplate.search("", "(objectclass=person)", new UserAttributeMapper());
}
@Override
public List<UserDTO> getUserDetails(String commonName, String lastName) {
AndFilter andFilter = new AndFilter();
andFilter.and(new EqualsFilter("objectclass", "person")).and(new EqualsFilter("cn", commonName))
.and(new EqualsFilter("sn", lastName));
log.debug("LDAP query {}", andFilter.encode());
return ldapTemplate.search("", andFilter.encode(), new UserAttributeMapper());
}
@Override
public void insertUser(UserDTO userDTO) {
BasicAttribute personBasicAttribute = new BasicAttribute("objectclass");
personBasicAttribute.add("person");
Attributes personAttributes = new BasicAttributes();
personAttributes.put(personBasicAttribute);
personAttributes.put("cn", userDTO.getCommonName());
personAttributes.put("sn", userDTO.getLastName());
personAttributes.put("description", userDTO.getDescription());
LdapName newUserDN = LdapUtils.emptyLdapName();
try {
newUserDN = new LdapName("o=sshService");
newUserDN.add("uid=" + userDTO.getCommonName());
log.debug(newUserDN.toString());
// ldapTemplate.bind(newUserDN, null, personAttributes);
} catch (InvalidNameException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
@Override
public void updateUser(UserDTO userDTO) {
BasicAttribute personBasicAttribute = new BasicAttribute("objectclass");
personBasicAttribute.add("person");
Attributes personAttributes = new BasicAttributes();
personAttributes.put(personBasicAttribute);
personAttributes.put("cn", userDTO.getCommonName());
personAttributes.put("sn", userDTO.getLastName());
personAttributes.put("description", userDTO.getDescription());
LdapName newUserDN = LdapUtils.emptyLdapName();
try {
newUserDN = new LdapName("o=sshService");
newUserDN.add("uid=" + userDTO.getCommonName());
log.debug(newUserDN.toString());
// ldapTemplate.rebind(newUserDN, null, personAttributes);
} catch (InvalidNameException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
@Override
public void deleteUser(UserDTO userDTO) {
LdapName newUserDN = LdapUtils.emptyLdapName();
try {
newUserDN = new LdapName("o=sshService");
newUserDN.add("uid=" + userDTO.getCommonName());
log.debug(newUserDN.toString());
// ldapTemplate.unbind(newUserDN);
} catch (InvalidNameException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
package edu.kit.scc.ldap;
import javax.naming.NamingException;
import javax.naming.directory.Attribute;
import javax.naming.directory.Attributes;
import org.springframework.ldap.core.AttributesMapper;
import edu.kit.scc.dto.UserDTO;
public class UserAttributeMapper implements AttributesMapper<UserDTO> {
@Override
public UserDTO mapFromAttributes(Attributes attributes) throws NamingException {
UserDTO userDTO = new UserDTO();
String commonName = (String) attributes.get("cn").get();
if (commonName != null)
userDTO.setCommonName(commonName);
String lastName = (String) attributes.get("sn").get();
if (lastName != null)
userDTO.setLastName(lastName);
Attribute description = attributes.get("description");
if (description != null)
userDTO.setDescription((String) description.get());
return userDTO;
}
}
# Root logger option
log4j.rootLogger=DEBUG, stdout
log4j.rootLogger=INFO, stdout
log4j.logger.edu.kit.scc=DEBUG
# Redirect log messages to console
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
......
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:ldap="http://www.springframework.org/schema/ldap"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/ldap http://www.springframework.org/schema/ldap/spring-ldap.xsd">
<ldap:context-source url="ldap://192.168.122.202:10389"
base="o=sshService" username="uid=admin,ou=system" password="secret" />
<ldap:ldap-template id="ldapTemplate" />
<bean id="ldapUser" class="edu.kit.scc.ldap.LDAPUserDAO">
<property name="ldapTemplate" ref="ldapTemplate" />
</bean>
</beans>
\ No newline at end of file
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment