Fields Summary |
---|
private StringBuffer | preparedRolesThe generated string for the roles PreparedStatement |
private StringBuffer | preparedCredentialsThe generated string for the credentials PreparedStatement |
protected String | dataSourceNameThe name of the JNDI JDBC DataSource |
protected static final String | infoDescriptive information about this Realm implementation. |
protected static final String | nameDescriptive information about this Realm implementation. |
protected String | roleNameColThe column in the user role table that names a role |
protected static final org.apache.catalina.util.StringManager | smThe string manager for this package. |
protected String | userCredColThe column in the user table that holds the user's credintials |
protected String | userNameColThe column in the user table that holds the user's name |
protected String | userRoleTableThe table that holds the relation between user's and roles |
protected String | userTableThe table that holds user data. |
Methods Summary |
---|
public java.security.Principal | authenticate(java.lang.String username, java.lang.String credentials)Return the Principal associated with the specified username and
credentials, if there is one; otherwise return null .
If there are any errors with the JDBC connection, executing
the query or anything we return null (don't authenticate). This
event is also logged, and the connection will be closed so that
a subsequent request will automatically re-open it.
Connection dbConnection = null;
try {
// Ensure that we have an open database connection
dbConnection = open();
if (dbConnection == null) {
// If the db connection open fails, return "not authenticated"
return null;
}
// Acquire a Principal object for this user
Principal principal = authenticate(dbConnection,
username, credentials);
if( !dbConnection.getAutoCommit() ) {
dbConnection.commit();
}
// Release the database connection we just used
close(dbConnection);
dbConnection = null;
// Return the Principal (if any)
return (principal);
} catch (SQLException e) {
// Log the problem for posterity
log(sm.getString("dataSourceRealm.exception"), e);
// Close the connection so that it gets reopened next time
if (dbConnection != null)
close(dbConnection);
// Return "not authenticated" for this request
return (null);
}
|
private java.security.Principal | authenticate(java.sql.Connection dbConnection, java.lang.String username, java.lang.String credentials)Return the Principal associated with the specified username and
credentials, if there is one; otherwise return null .
ResultSet rs = null;
PreparedStatement stmt = null;
ArrayList list = null;
try {
// Look up the user's credentials
String dbCredentials = null;
stmt = credentials(dbConnection, username);
rs = stmt.executeQuery();
while (rs.next()) {
dbCredentials = rs.getString(1).trim();
}
rs.close();
rs = null;
stmt.close();
stmt = null;
if (dbCredentials == null) {
return (null);
}
// Validate the user's credentials
boolean validated = false;
if (hasMessageDigest()) {
// Hex hashes should be compared case-insensitive
validated = (digest(credentials).equalsIgnoreCase(dbCredentials));
} else
validated = (digest(credentials).equals(dbCredentials));
if (validated) {
if (debug >= 2)
log(sm.getString("dataSourceRealm.authenticateSuccess",
username));
} else {
if (debug >= 2)
log(sm.getString("dataSourceRealm.authenticateFailure",
username));
return (null);
}
// Accumulate the user's roles
list = new ArrayList();
stmt = roles(dbConnection, username);
rs = stmt.executeQuery();
while (rs.next()) {
list.add(rs.getString(1).trim());
}
} finally {
if (rs != null) {
rs.close();
}
if (stmt != null) {
stmt.close();
}
}
// Create and return a suitable Principal for this user
return (new GenericPrincipal(this, username, credentials, list));
|
private void | close(java.sql.Connection dbConnection)Close the specified database connection.
// Do nothing if the database connection is already closed
if (dbConnection == null)
return;
// Close this database connection, and log any errors
try {
dbConnection.close();
} catch (SQLException e) {
log(sm.getString("dataSourceRealm.close"), e); // Just log it here
}
|
private java.sql.PreparedStatement | credentials(java.sql.Connection dbConnection, java.lang.String username)Return a PreparedStatement configured to perform the SELECT required
to retrieve user credentials for the specified username.
PreparedStatement credentials =
dbConnection.prepareStatement(preparedCredentials.toString());
credentials.setString(1, username);
return (credentials);
|
public java.lang.String | getDataSourceName()Return the name of the JNDI JDBC DataSource.
// ------------------------------------------------------------- Properties
return dataSourceName;
|
protected java.lang.String | getName()Return a short name for this Realm implementation.
return (this.name);
|
protected java.lang.String | getPassword(java.lang.String username)Return the password associated with the given principal's user name.
return (null);
|
protected java.security.Principal | getPrincipal(java.lang.String username)Return the Principal associated with the given user name.
return (null);
|
public java.lang.String | getRoleNameCol()Return the column in the user role table that names a role.
return roleNameCol;
|
public java.lang.String | getUserCredCol()Return the column in the user table that holds the user's credentials.
return userCredCol;
|
public java.lang.String | getUserNameCol()Return the column in the user table that holds the user's name.
return userNameCol;
|
public java.lang.String | getUserRoleTable()Return the table that holds the relation between user's and roles.
return userRoleTable;
|
public java.lang.String | getUserTable()Return the table that holds user data..
return userTable;
|
private java.sql.Connection | open()Open the specified database connection.
try {
StandardServer server = (StandardServer) ServerFactory.getServer();
Context context = server.getGlobalNamingContext();
DataSource dataSource = (DataSource)context.lookup(dataSourceName);
return dataSource.getConnection();
} catch (Exception e) {
// Log the problem for posterity
log(sm.getString("dataSourceRealm.exception"), e);
}
return null;
|
private java.sql.PreparedStatement | roles(java.sql.Connection dbConnection, java.lang.String username)Return a PreparedStatement configured to perform the SELECT required
to retrieve user roles for the specified username.
PreparedStatement roles =
dbConnection.prepareStatement(preparedRoles.toString());
roles.setString(1, username);
return (roles);
|
public void | setDataSourceName(java.lang.String dataSourceName)Set the name of the JNDI JDBC DataSource.
this.dataSourceName = dataSourceName;
|
public void | setRoleNameCol(java.lang.String roleNameCol)Set the column in the user role table that names a role.
this.roleNameCol = roleNameCol;
|
public void | setUserCredCol(java.lang.String userCredCol)Set the column in the user table that holds the user's credentials.
this.userCredCol = userCredCol;
|
public void | setUserNameCol(java.lang.String userNameCol)Set the column in the user table that holds the user's name.
this.userNameCol = userNameCol;
|
public void | setUserRoleTable(java.lang.String userRoleTable)Set the table that holds the relation between user's and roles.
this.userRoleTable = userRoleTable;
|
public void | setUserTable(java.lang.String userTable)Set the table that holds user data.
this.userTable = userTable;
|
public void | start()Prepare for active use of the public methods of this Component.
// Create the roles PreparedStatement string
preparedRoles = new StringBuffer("SELECT ");
preparedRoles.append(roleNameCol);
preparedRoles.append(" FROM ");
preparedRoles.append(userRoleTable);
preparedRoles.append(" WHERE ");
preparedRoles.append(userNameCol);
preparedRoles.append(" = ?");
// Create the credentials PreparedStatement string
preparedCredentials = new StringBuffer("SELECT ");
preparedCredentials.append(userCredCol);
preparedCredentials.append(" FROM ");
preparedCredentials.append(userTable);
preparedCredentials.append(" WHERE ");
preparedCredentials.append(userNameCol);
preparedCredentials.append(" = ?");
// Perform normal superclass initialization
super.start();
|
public void | stop()Gracefully shut down active use of the public methods of this Component.
// Perform normal superclass finalization
super.stop();
|