33
Application Development Using Spring LDAP Balaji Varanasi

LDAP Development Using Spring LDAP

  • Upload
    ldapcon

  • View
    159

  • Download
    2

Embed Size (px)

DESCRIPTION

 

Citation preview

Page 1: LDAP Development Using Spring LDAP

License CC-BY-SA1

Application Development Using Spring LDAP

Balaji Varanasi

Page 2: LDAP Development Using Spring LDAP

About Me

Development Manager Author LDAPUnit Creator

License CC-BY-SA

Page 3: LDAP Development Using Spring LDAP

Agenda

Page 4: LDAP Development Using Spring LDAP

JNDI

Page 5: LDAP Development Using Spring LDAP

Java LDAP Application Development

Page 6: LDAP Development Using Spring LDAP

Java LDAP Development

• JNDI Way Of Development

• Connect to LDAP Server

• Perform LDAP Operations

• Close resources

Page 7: LDAP Development Using Spring LDAP

JNDI – Connecting to LDAP

Properties environment = new Properties();environment.setProperty(DirContext.INITIAL_CONTEXT_FACTORY,

"com.sun.jndi.ldap.LdapCtxFactory");environment.setProperty(DirContext.PROVIDER_URL, "ldap://localhost:11389");environment.setProperty(DirContext.SECURITY_PRINCIPAL, "cn=Directory Manager");environment.setProperty(DirContext.SECURITY_CREDENTIALS, "opendj");

DirContext context = new InitialDirContext(environment);

Page 8: LDAP Development Using Spring LDAP

JNDI – Performing LDAP OperationSearchControls searchControls = new SearchControls();searchControls.setSearchScope(SearchControls.SUBTREE_SCOPE);searchControls.setReturningAttributes(new String[]{"givenName", "sn",

"telephoneNumber"});

searchResults = context.search(BASE_PATH, "(objectClass=inetOrgPerson)", searchControls);

while (searchResults.hasMore()) {SearchResult result = searchResults.next();Attributes attributes = result.getAttributes();// Read single valued attributesString firstName = (String)attributes.get("givenName").get(); String lastName = (String)attributes.get("sn").get();

// Read the multi-valued attributeAttribute phoneAttribute = attributes.get("telephoneNumber");String[] phone = new String[phoneAttribute.size()];NamingEnumeration phoneValues = phoneAttribute.getAll();

for(int i = 0; phoneValues.hasMore(); i++) {phone[i] = (String)phoneValues.next();

}}

Page 9: LDAP Development Using Spring LDAP

JNDI – Close Resources

if (searchResults != null) {

searchResults.close();}

if(context != null) {

context.close();}

Page 10: LDAP Development Using Spring LDAP

JNDI LDAP Operations Demo

Page 11: LDAP Development Using Spring LDAP

What’s wrong with JNDI?

• JNDI Drawbacks

• Plumbing Code

• Explicit Resource Management

• Checked Exceptions

Page 12: LDAP Development Using Spring LDAP

Spring LDAP

Page 13: LDAP Development Using Spring LDAP

Spring LDAP

“Makes it easier to build Spring-based applications that use the Lightweight Directory

Access Protocol”

Page 14: LDAP Development Using Spring LDAP

Spring LDAP

“Makes it easier to build Spring-based applications that use the Lightweight Directory

Access Protocol”

Page 15: LDAP Development Using Spring LDAP

Spring LDAP

• Rich set of features– Template and utility classes

– Search Filters

– ODM

– LDIF Parsing

– Transaction Management

– Connection Pooling

– Unchecked exception hierarchy

• Currently 1.3.2

• 2.0.0 in works

Page 16: LDAP Development Using Spring LDAP

Spring LDAP Application Development

Page 17: LDAP Development Using Spring LDAP

Spring LDAP Development

• Core Concepts

• Context Source

• LdapTemplate

Page 18: LDAP Development Using Spring LDAP

Spring LDAP Development

• Context Source

• Abstracts LDAP Connection

• LdapContextSource implementation

Page 19: LDAP Development Using Spring LDAP

LdapTemplate

• Provides Overloaded

• Search

• Lookup

• Bind/Unbind

• Authenticate methods

• Thread safe

Page 20: LDAP Development Using Spring LDAP

Spring LDAP Template Demo

Page 21: LDAP Development Using Spring LDAP

Integration Testing LDAP Code

• Integration Testing requirements

• Ability to spin up LDAP servers programmatically

• Programmatically start and stop servers

• We need to load data for each set of tests

• Embedded Servers

• Lightweight in nature

• Quick startup time

• Ease of configuration

• OpenDJ/OpenDS, ApacheDS, UnboundID

Page 22: LDAP Development Using Spring LDAP

LdapUnit

• Simplifies LDAP Testing

• Supports three embedded servers

• Provides abstraction for other servers to be plugged in

• Puts LDAP Server in a known state

• Works with Spring LDAP or standalone Java code

• Version 0.6.0

• Code on GitHub: https://github.com/bava/ldapunit

Page 23: LDAP Development Using Spring LDAP

LdapUnit Demo

Page 24: LDAP Development Using Spring LDAP

Spring LDAP ODM

Page 25: LDAP Development Using Spring LDAP

Spring LDAP ODM

• ORM for Databases

• Annotation Driven

• @Entry

• @Id

• @Attribute

• @Transient

Page 26: LDAP Development Using Spring LDAP

ODM Demo

Page 27: LDAP Development Using Spring LDAP

Spring LDAP ODM

• ORM Differences

• Caching of LDAP Entries not possible

• No XML mapping support

• Lazy loading of Entries not possible

Page 28: LDAP Development Using Spring LDAP

What else can we improve?

Page 29: LDAP Development Using Spring LDAP

Spring LDAP Authentication

public boolean authenticate(String userid, String password) {

DistinguishedName dn = new DistinguishedName(BASE_DN);dn.add("uid", userid);

DirContext authenticatedContext = null;try {

authenticatedContext = contextSource.getContext(dn.toString(), password);return true;

}catch(NamingException e) {

e.printStackTrace();return false;

}finally {

LdapUtils.closeContext(authenticatedContext);}

}

Page 30: LDAP Development Using Spring LDAP

Spring LDAP Authentication

@Overridepublic boolean authenticate(String userid, String password) {

return ldapTemplate.authenticate("","(uid=" + userid + ")", password);}

Page 31: LDAP Development Using Spring LDAP

Spring LDAP Authentication

public boolean authenticate(String userid, String password) {EmployeeAuthenticationErrorCallback errorCallback = new

EmployeeAuthenticationErrorCallback();boolean isAuthenticated = ldapTemplate.authenticate("","(uid=" + userid + ")",

password, errorCallback);if(!isAuthenticated) {

System.out.println(errorCallback.getAuthenticationException());}return isAuthenticated;

}

Page 32: LDAP Development Using Spring LDAP

Questions

Page 33: LDAP Development Using Spring LDAP

License CC-BY-SA33

Thanks!