Results 1 to 7 of 7

Thread: How to authenticate user from LDAP..

  1. #1
    Join Date
    Nov 2008
    Location
    Pakistan
    Posts
    29

    Exclamation How to authenticate user from LDAP..

    hello
    can any One tell me how i create a method in which i authenticate user from LDAP using spring LDAP API...
    i want user enter his/her cn and Password and i check these attributes from LDAP.. is it possible or is there any method which return me boolean when i pass cn and password

  2. #2

    Default

    Noman,
    The LDAP reference documentation talks about authentication here: http://static.springsource.org/sprin...ntication.html

  3. #3
    Join Date
    Nov 2008
    Location
    Pakistan
    Posts
    29

    Default

    thanks for your replay and link..
    i am facing some compile time error when i try the first method which is in the link
    Using ContextSource for user authentication

    may be i am doing something wrong
    Code:
     try {
    	  ctx = ContextSource.getContext(userDn, credentials);
    	    return true;
    i am getting error in this line which is
    The method getContext(String, String) is undefined for the type ContextSource

    import interface from ldap is org.springframework.ldap.ContextSource



    what is this can you tell me about it

  4. #4

    Default

    You are trying to call getContext on ContextSource class. Instead, you need to create a new instance of ContextSource and invoke getContext on it. Here is a way to programatically create ContextSource

    Code:
                    LdapContextSource contextSource = new LdapContextSource();
    		contextSource.setUrl("YOUR_LDAP_URL");                  
    		contextSource.setUserDn("YOUR_OPTIONAL_ADMIN_USER_DN");
    		contextSource.setPassword("YOUR_OPTIONAL_ADMIN_PWD");
    		try {
    			contextSource.afterPropertiesSet();
    		}
    		catch(Exception e) {
    			e.printStackTrace();
    		}
                    ctx = contextSource.getContext(userDn, credentials);
        
                    return true;

  5. #5
    Join Date
    Nov 2008
    Location
    Pakistan
    Posts
    29

    Cool

    hi bvar
    thanx for the code...

    well it seem to be meaningless we again create ContextSource and provide the all the information which we already set in Spring configuration file...

    there is not any sophisticated way to do this..

  6. #6

    Default

    Ahh!! I didn't realize that you were using a configuration file. In that case, you can simply inject the context source. Right?

    For example if this is your configuration:

    Code:
         <bean id="contextSource" class="org.springframework.ldap.core.support.LdapContextSource">
            	<property name="url" value="XXXXXXX" />
            	<property name="userDn" value="XXXXXX" />
    			<property name="password" value="XXXXXX" />
            	<property name="base" value=""/>
    		</bean>
    Then you can use the following code:

    Code:
      import javax.naming.directory.DirContext;
    
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.beans.factory.annotation.Qualifier;
    import org.springframework.ldap.core.ContextSource;
    import org.springframework.ldap.support.LdapUtils;
    
    public class AuthenticationDao {
    	
    	@Autowired
    	@Qualifier("contextSource")
    	private ContextSource contextSource;
    	
    	public boolean authenticate(String userDn, String credentials) {
    		  DirContext ctx = null;
    		  try {
    		    ctx = contextSource.getContext(userDn, credentials);
    		    return true;
    		  } catch (Exception e) {
    		    // Context creation failed - authentication did not succeed
    		    return false;
    		  } finally {
    		    // It is imperative that the created DirContext instance is always closed
    		    LdapUtils.closeContext(ctx);
    		  }
    	}	  
    }

  7. #7
    Join Date
    Nov 2008
    Location
    Pakistan
    Posts
    29

    Default

    hmmm ok nice to see your response but still facing problem...
    i replace the class which you mention in contaxt bean
    org.springframework.ldap.core.support.LdapContextS ource


    now here is my spring configuration file.

    Code:
    <bean id="ldapContextSource"
    class="org.springframework.ldap.core.support.LdapContextSource">
    <property name="url" value="ldap://XXXXX:389" />
    <property name="base" value="ou=schools,DC=lti-student,DC=test" />
    <property name="userDn" value="XXXX" />
    <property name="password" value="XXXX" />
    </bean>
    
    <bean id="ldapTemplate" class="org.springframework.ldap.core.LdapTemplate">
    <constructor-arg ref="ldapContextSource" />
    </bean> 
    	
    	<bean id="ldapContactDao"
    		class="com.javaworld.sample.LDAPContactDAO">
    		<property name="ldapTemplate" ref="ldapTemplate" />
    	</bean>
    </beans>


    but with this configuration i am getting exception

    Code:
    Exception in thread "main" org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'ldapContact' defined in class path resource [com/javaworld/sample/springldap.xml]: Initialization of bean failed; nested exception is org.springframework.beans.TypeMismatchException: Failed to convert property value of type [org.springframework.ldap.core.LdapTemplate] to required type [org.springframework.ldap.LdapTemplate] for property 'ldapTemplate'; nested exception is java.lang.IllegalArgumentException: Cannot convert value of type [org.springframework.ldap.core.LdapTemplate] to required type [org.springframework.ldap.LdapTemplate] for property 'ldapTemplate': no matching editors or conversion strategy found
    	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:480)
    	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory$1.run(AbstractAutowireCapableBeanFactory.java:409)
    	at java.security.AccessController.doPrivileged(Native Method)
    	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:380)
    	at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:264)
    	at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:222)
    	at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:261)
    	at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:185)
    	at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:164)
    	at com.javaworld.sample.SpringFrameworkLDAPClient.main(SpringFrameworkLDAPClient.java:23)
    Caused by: org.springframework.beans.TypeMismatchException: Failed to convert property value of type [org.springframework.ldap.core.LdapTemplate] to required type [org.springframework.ldap.LdapTemplate] for property 'ldapTemplate'; nested exception is java.lang.IllegalArgumentException: Cannot convert value of type [org.springframework.ldap.core.LdapTemplate] to required type [org.springframework.ldap.LdapTemplate] for property 'ldapTemplate': no matching editors or conversion strategy found
    	at org.springframework.beans.BeanWrapperImpl.convertForProperty(BeanWrapperImpl.java:391)
    	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.convertForProperty(AbstractAutowireCapableBeanFactory.java:1289)
    	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyPropertyValues(AbstractAutowireCapableBeanFactory.java:1250)
    	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1010)
    	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:472)
    	... 9 more
    Caused by: java.lang.IllegalArgumentException: Cannot convert value of type [org.springframework.ldap.core.LdapTemplate] to required type [org.springframework.ldap.LdapTemplate] for property 'ldapTemplate': no matching editors or conversion strategy found
    	at org.springframework.beans.TypeConverterDelegate.convertIfNecessary(TypeConverterDelegate.java:231)
    	at org.springframework.beans.TypeConverterDelegate.convertIfNecessary(TypeConverterDelegate.java:138)
    	at org.springframework.beans.BeanWrapperImpl.convertForProperty(BeanWrapperImpl.java:386)
    	... 13 more
    what is this can you please tell me about it i also googling on that

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •