All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.alachisoft.ncache.security.CacheADAuthenticationProvider Maven / Gradle / Ivy

There is a newer version: 5.3.3
Show newest version
package com.alachisoft.ncache.security;

import javax.naming.Context;
import javax.naming.NamingException;
import javax.naming.ldap.InitialLdapContext;
import javax.naming.ldap.LdapContext;
import java.util.Hashtable;

public class CacheADAuthenticationProvider implements IAuthenticationProvider {

    public CacheADAuthenticationProvider() {
    }

    @Override
    public final boolean Authenticate(Object credentials) {
        NamePasswordCredential cacheCred = (NamePasswordCredential) credentials;
        boolean authenticate;
        try {
//            int charIndex = cacheCred.getName().lastIndexOf("\\", cacheCred.getName().length());
//            if (charIndex == -1) {
//                charIndex = cacheCred.getName().lastIndexOf("@", cacheCred.getName().length());
//            }
//            String userName = cacheCred.getName().substring(0,charIndex + 1);

            String providerUrl = "";
            if (cacheCred.getLdapPort().isEmpty() || cacheCred.getLdapPort() == null) {
                providerUrl = cacheCred.getDomainController() + ":389";
            } else {
                providerUrl = cacheCred.getDomainController() + ":" + cacheCred.getLdapPort();
            }
//            String Username ="";
//            int charIndex = cacheCred.getDomainController().lastIndexOf("//", cacheCred.getDomainController().length());
//            if (charIndex != -1) {
//                 Username= cacheCred.getName() + "@" + cacheCred.getDomainController().substring(charIndex + 2, cacheCred.getDomainController().length());
//            }


            Hashtable env = new Hashtable();
            env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory");
            env.put(Context.SECURITY_AUTHENTICATION, "Simple");
            //it can be  something that you use for windows login
            //it can also be
            env.put(Context.SECURITY_PRINCIPAL, cacheCred.getName());
            env.put(Context.SECURITY_CREDENTIALS, cacheCred.getPassword());
            //in following property we specify ldap protocol and connection url.
            //generally the port is 389
            env.put(Context.PROVIDER_URL, providerUrl);
            LdapContext ctx = new InitialLdapContext(env, null);
            // ctx.bind(Username, cacheCred.getPassword());
            authenticate = true;
        } catch (NamingException nex) {
//            System.out.println(
//            nex.getMessage());
//
//            //SP2,logging exception in event viewer
//           nex.printStackTrace();
            authenticate = false;
        }


        return authenticate;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy