04-12-2013 04:21 PM
04-14-2013 06:19 PM
//******************************************************************************
//* Author: Michael Hoffmann
//* Date  : 14.04.2013
//* File  : LdapUserManagerFactory.java
//******************************************************************************
package ldap;
//******************************************************************************
//* Dependencies
//******************************************************************************
import org.activiti.engine.impl.interceptor.Session;
import org.activiti.engine.impl.interceptor.SessionFactory;
import org.activiti.engine.impl.persistence.entity.UserEntityManager;
//******************************************************************************
//* Class Definition
//******************************************************************************
public class LdapUserManagerFactory implements SessionFactory {
    //————————————————————————–
    // Member
    //————————————————————————–
 
 private LdapSetting ldapSetting;
 
 LdapUserManagerFactory(LdapSetting ldapSetting)
 { this.ldapSetting = ldapSetting; }
 
    //————————————————————————–
    // Interface
    //————————————————————————–
 
 @Override
 public Class<?> getSessionType() 
 { return UserEntityManager.class; }
 @Override
 public Session openSession() 
 { return new LdapUserManager(ldapSetting); }
}
//******************************************************************************
//* Author: Michael Hoffmann
//* Date  : 14.04.2013
//* File  : LdapUserManager.java
//******************************************************************************
package ldap;
//******************************************************************************
//* Dependencies
//******************************************************************************
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.naming.NamingException;
import org.activiti.engine.impl.persistence.entity.UserEntityManager;
//******************************************************************************
//* Class Definition
//******************************************************************************
public class LdapUserManager extends UserEntityManager {
    //————————————————————————–
    // Member
    //————————————————————————–
 
    private static final Logger LOGGER = 
    Logger.getLogger(LdapUserManager.class.getName());
    
    private LdapSetting ldapSetting;
    private LdapAdapter ldapAdapter;
    
    //————————————————————————–
    // Ctor
    //————————————————————————–
    
    public LdapUserManager(LdapSetting ldapSetting)
    { 
      this.ldapSetting = ldapSetting;
      this.ldapAdapter = new LdapAdapter();
    }
    //————————————————————————–
    // Inheritance
    //————————————————————————–
    
    @Override
    public Boolean checkPassword(String userId, String password) {
        
        LOGGER.log(Level.INFO,"checking password…");
        String uid = "cn=" + userId +",ou=users,ou=system";
        
        try {
            
            ldapSetting.setSecurityPrincipal(uid);
            ldapSetting.setSecurityCredential(password);
            
            if(ldapAdapter.authenticate(ldapSetting))
            {
             ldapAdapter.close();
             LOGGER.log(Level.INFO,"LDAP authentication successful: " + uid);
             return true;
            }
        } 
        catch (NamingException e) 
        { e.printStackTrace(); }
       
        LOGGER.log(Level.INFO,"LDAP authentication failed: " + uid);
        return Boolean.FALSE;
    }
}
//******************************************************************************
//* Author: Michael Hoffmann
//* Date  : 14.04.2013
//* File  : LdapSetting.java
//******************************************************************************
package ldap;
//******************************************************************************
//* Dependencies
//******************************************************************************
// …
//******************************************************************************
//* Class Definition
//******************************************************************************
public class LdapSetting {
    //————————————————————————–
    // Member
    //————————————————————————–
    
    private String ldapUrl;
    private String ldapPort;
    private String securityAuthentication;
    private String securityCredential;
    private String securityPrincipal;
    
    //————————————————————————–
    // Getter
    //————————————————————————–
    
    public String getLdapUrl() 
    { return ldapUrl; }
    
    public String getLdapPort() 
    { return ldapPort; }
    public String getSecurityAuthentication() 
    { return securityAuthentication; }
    
    public String getSecurityCredential() 
    { return securityCredential; }
    
    public String getSecurityPrincipal() 
    { return securityPrincipal; }
    //————————————————————————–
    // Setter
    //————————————————————————–
    
    public void setLdapUrl(String ldapUrl) 
    { this.ldapUrl = ldapUrl; }
    
    public void setLdapPort(String ldapPort) 
    { this.ldapPort = ldapPort; }
    public void setSecurityAuthentication(String securityAuthentication) 
    { this.securityAuthentication = securityAuthentication; }
    
    public void setSecurityCredential(String securityCredential) 
    { this.securityCredential = securityCredential; }
    
    public void setSecurityPrincipal(String securityPrincipal) 
    { this.securityPrincipal = securityPrincipal; }
    
    // Additional Getter
    public String getProviderUrl()
    { return ldapUrl + ":" + ldapPort; }
    
}
//******************************************************************************
//* Author: Michael Hoffmann
//* Date  : 14.04.2013
//* File  : LdapAdapter.java
//******************************************************************************
package ldap;
//******************************************************************************
//* Dependencies
//******************************************************************************
import java.util.Hashtable;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
//******************************************************************************
//* Class Definition
//******************************************************************************
public class LdapAdapter {
    //————————————————————————–
    // Member
    //————————————————————————–
    
    private final static String FACTORY =
    "com.sun.jndi.ldap.LdapCtxFactory";
    
    private static final Logger LOGGER = 
    Logger.getLogger(LdapAdapter.class.getName());
    
    private Context context;
    
    //————————————————————————–
    // Ldap
    //————————————————————————–
    
    public boolean authenticate(LdapSetting ldapSetting) throws NamingException
    {
        LOGGER.log(Level.INFO,"try to connect");
        Hashtable<String, String> env = new Hashtable<String, String>();
        env.put(Context.INITIAL_CONTEXT_FACTORY,FACTORY);
        env.put(Context.PROVIDER_URL,ldapSetting.getProviderUrl());
        env.put(Context.SECURITY_AUTHENTICATION,ldapSetting.getSecurityAuthentication());
        env.put(Context.SECURITY_PRINCIPAL,ldapSetting.getSecurityPrincipal());
        env.put(Context.SECURITY_CREDENTIALS,ldapSetting.getSecurityCredential());
        context    = new InitialContext(env);
        LOGGER.log(Level.INFO,"connected");
        return context != null;
    }
    
    public void close() throws NamingException
    {
        context.close();
        LOGGER.log(Level.INFO,"connection closed");
    }
}
 
  Snippet: activiti-standalone-context.xml
  …
  <bean id="processEngineConfiguration" class="org.activiti.spring.SpringProcessEngineConfiguration">
   <property name="dataSource" ref="dataSource" />
   <property name="transactionManager" ref="transactionManager" />
   <property name="databaseSchemaUpdate" value="true" />
   <property name="jobExecutorActivate" value="true" />
    <property name="customFormTypes">
      <list>
        <bean class="org.activiti.explorer.form.UserFormType"/>
        <bean class="org.activiti.explorer.form.ProcessDefinitionFormType"/> 
        <bean class="org.activiti.explorer.form.MonthFormType"/>   
      </list>
    </property>
 
 <property name="customSessionFactories">
      <list>
        <bean class="ldap.LdapUserManagerFactory">
          <constructor-arg ref="LdapSettingBean" />
        </bean>
            </list>
    </property>
 
  </bean>
  
  <bean id="LdapSettingBean" class="ldap.LdapSetting">
    <property name="ldapUrl" value="ldap://localhost" />
    <property name="ldapPort" value="10389" />
    <property name="securityAuthentication" value="simple" />
  </bean> 
  …
db=postgresql
jdbc.driver=org.postgresql.Driver
jdbc.url=jdbc:smileytongue:ostgresql://localhost:5432/activiti
jdbc.username=postgres
jdbc.password=admin
04-15-2013 05:54 AM
04-19-2013 02:45 AM
04-19-2013 03:10 AM
08-14-2013 06:06 AM
04-19-2013 03:50 AM
Tags
Find what you came for
We want to make your experience in Hyland Connect as valuable as possible, so we put together some helpful links.