JDBC to SQL Server using AD credentials

I am trying to figure out how to write a simple app that connects to a Microsoft SQL Server, using Active Directory credentials.I don't have any experience doing this, so any beginner information would be very helpful.Thanks folks!
[253 byte] By [decassida] at [2007-11-26 22:36:28]
# 1

hi,

You can use classes in the javax.naming.directory package to access Active Directory.

The following code is taken from our app which checks an AD server. You can adapt it to your needs. You must import javax.naming.* and javax.naming.directory.*

try

{

Hashtable env = new Hashtable();

env.put(Context.INITIAL_CONTEXT_FACTORY,

"com.sun.jndi.ldap.LdapCtxFactory");

env.put(Context.PROVIDER_URL,

"ldap://" + getProperty("SERVER") + ":" +

getProperty("PORT"));

env.put(Context.SECURITY_PRINCIPAL,

getProperty("USER_RDN") + "," +

getProperty("BASE_DN"));

env.put(Context.SECURITY_CREDENTIALS,

getProperty("USER_PASSWORD"));

DirContext ctx = new InitialDirContext(env);

ctx.getAttributes(getProperty("BASE_DN"));

ctx.close();

info("connection_succeeded");

}

catch (CommunicationException comEx)

{

error(MessageResource.getMessage("communication_exception",

comEx.getMessage()));

return false;

}

catch (AuthenticationException authEx)

{

error(MessageResource.getMessage("authentication_exception",

authEx.getMessage()));

return false;

}

catch (NamingException nameEx)

{

error(MessageResource.getMessage("naming_exception",

nameEx.toString()));

return false;

}

return true;

take a look this commercial solution : http://www.datadirect.com/developer/jdbc/topics/winauth/index.ssp

hth

java_2006a at 2007-7-10 11:46:42 > top of Java-index,Database Connectivity,Java Database Connectivity (JDBC)...
# 2

Thank you for your reply. Unfortunately, I still have questions.

Are the tokens written in red caps to be altered based on my setup? I would assume so, but I would be worried about storing cleartext passwords in the class files.

Once the authentication via AD (I think that's just a glorified LDAP, right?), I should be able to connect to the database like normal, right?

Thanks again for your help. Unfortunately, I think I'm biting off more than I can chew.

decassida at 2007-7-10 11:46:42 > top of Java-index,Database Connectivity,Java Database Connectivity (JDBC)...