skip to main content
Using the Driver : Authentication : Configuring the Driver for Kerberos Authentication : Specifying User Credentials for Kerberos Authentication (Delegation of Credentials)
  

Try DataDirect Drivers Now
Specifying User Credentials for Kerberos Authentication (Delegation of Credentials)
By default, when Kerberos authentication is used, the driver takes advantage of the user name and password maintained by the operating system to authenticate users to the database. By allowing the database to share the user name and password used for the operating system, users with a valid operating system account can log into the database without supplying a user name and password.
Many application servers or Web servers act on behalf of the client user logged on the machine on which the application is running, rather than the server user. If you want the driver to use user credentials other than the operating system user name and password, include code in your application to obtain and pass a javax.security.auth.Subject used for authentication as shown in the following example.

import javax.security.auth.Subject;
import javax.security.auth.login.LoginContext;
import java.sql.*;
// The following code creates a javax.security.auth.Subject instance
// used for authentication. Refer to the Java Authentication
// and Authorization Service documentation for details on using a
// LoginContext to obtain a Subject.
LoginContext lc = null;
Subject subject = null;
try {
    lc = new LoginContext("JaasSample", new TextCallbackHandler());
    lc.login();
    subject = lc.getSubject();
}
catch (Exception le) {
    ... // display login error
}// This application passes the javax.security.auth.Subject
// to the driver by executing the driver code as the subject
Connection con =
   (Connection) Subject.doAs(subject, new PrivilegedExceptionAction() {
   
   public Object run() {
   
         Connection con = null;
    try {
         Class.forName("com.ddtek.jdbc.cassandra.CassandraDriver");
         String url = "jdbc:datadirect:cassandra://myServer:27017";
         con = DriverManager.getConnection(url);
        }
    catch (Exception except) {
     ... //log the connection error
            Return null;
       }
      
         return con;
    }
});
//  This application now has a connection that was authenticated with
//  the subject. The application can now use the connection.
Statement   stmt = con.createStatement();
String      sql = "SELECT * FROM employee";
ResultSet   rs = stmt.executeQuery(sql);
... // do something with the results