Sample JDBC Client Code
The following code example for a JDBC client can be adapted for your clients.
jdbc:compositesw:dbapi@Host_Name:9401?domain=MyDomain&dataSource=MyDataSource&authenticationMethod=kerberos&kerberos.spn=HTTP@FullyQualified_Host_Name
import java.security.PrivilegedExceptionAction;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.HashMap;
import java.util.Properties;
import javax.security.auth.Subject;
import javax.security.auth.spi.LoginModule;
public class TestCompositeKerberos {
static String loginModule = "com.sun.security.auth.module.Krb5LoginModule";
public static void main(String[] args) throws Exception{
// System.setProperty("java.security.krb5.conf","C:\\WINDOWS\\krb5.ini");
connectWithDefaultUser();
connectWithSpecificUser();
}
public static void connectWithDefaultUser(){
Connection con = null;
Statement stat = null;
try {
Class.forName("cs.jdbc.driver.CompositeDriver");
String url = "jdbc:compositesw:dbapi@Host_Name:9401?
domain=MyDomain&dataSource=MyDataSource";
Properties props = new Properties();
props.put("authenticationMethod", "kerberos");
props.put("kerberos.spn", "HTTP@FullyQualified_Host_Name");
con = DriverManager.getConnection(url, props);
stat = con.createStatement();
ResultSet rs = stat.executeQuery
("SELECT * FROM test.test.C_CUSTOMER");
rs.next();
System.err.println(rs.getString(2));
}catch (Exception except) {
except.printStackTrace();
}finally{
try{
if(stat != null){
stat.close();
}
if(con != null){
con.close();
}
}catch(Exception e){}
}
}
public static void connectWithSpecificUser() throws Exception{
Subject subject = getSubject(username, password);
Subject.doAs(subject, new PrivilegedExceptionAction(){
public Object run(){
Connection con = null;
Statement stat = null;
try {
Class.forName("cs.jdbc.driver.CompositeDriver");
String url = "jdbc:compositesw:dbapi@Host_Name:9401?
domain=MyDomain&dataSource=MyDataSource";
Properties props = new Properties();
props.put("authenticationMethod", "kerberos");
props.put("kerberos.spn", "HTTP@FullyQualified_Host_Name");
con = DriverManager.getConnection(url, props);
stat = con.createStatement();
ResultSet rs = stat.executeQuery
("SELECT * FROM test.test.C_CUSTOMER");
rs.next();
System.err.println(rs.getString(1));
return null;
}catch (Exception except) {
except.printStackTrace();
return null;
}finally{
try{
if(stat != null){
stat.close();
}
if(con != null){
con.close();
}
}catch(Exception e){}
}
}
});
}
private static Subject getSubject(String principle,
String password)throws Exception{
LoginModule krb5Module = (LoginModule)Class.forName(loginModule).newInstance();
Subject subject = new Subject();
HashMap sharedState = new HashMap();
sharedState.put("javax.security.auth.login.password", password.toCharArray());
sharedState.put("javax.security.auth.login.name", principle);
HashMap options = new HashMap();
options.put("principal", principle);
options.put("debug", "true");
options.put("storeKey", "true");
options.put("useFirstPass", "true");
krb5Module.initialize(subject, null, sharedState, options);
try{
krb5Module.login();
krb5Module.commit();
}catch(Exception e){
e.printStackTrace();
krb5Module.abort();
return null;
}
return subject;
}
}