| Method from org.apache.catalina.realm.DataSourceRealm Detail: |
public Principal authenticate(String username,
String credentials) {
// No user - can't possibly authenticate, don't bother the database then
if (username == null) {
return null;
}
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
return authenticate(dbConnection, username, credentials);
} catch (SQLException e) {
// Log the problem for posterity
containerLog.error(sm.getString("dataSourceRealm.exception"), e);
// Return "not authenticated" for this request
return (null);
} finally {
close(dbConnection);
}
}
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. |
protected Principal authenticate(Connection dbConnection,
String username,
String credentials) throws SQLException {
String dbCredentials = getPassword(dbConnection, username);
// 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 (containerLog.isTraceEnabled())
containerLog.trace(
sm.getString("dataSourceRealm.authenticateSuccess",
username));
} else {
if (containerLog.isTraceEnabled())
containerLog.trace(
sm.getString("dataSourceRealm.authenticateFailure",
username));
return (null);
}
ArrayList< String > list = getRoles(dbConnection, username);
// Create and return a suitable Principal for this user
return (new GenericPrincipal(this, username, credentials, list));
}
Return the Principal associated with the specified username and
credentials, if there is one; otherwise return null. |
protected void close(Connection dbConnection) {
// Do nothing if the database connection is already closed
if (dbConnection == null)
return;
// Commit if not auto committed
try {
if (!dbConnection.getAutoCommit()) {
dbConnection.commit();
}
} catch (SQLException e) {
containerLog.error("Exception committing connection before closing:", e);
}
// Close this database connection, and log any errors
try {
dbConnection.close();
} catch (SQLException e) {
containerLog.error(sm.getString("dataSourceRealm.close"), e); // Just log it here
}
}
Close the specified database connection. |
public String getDataSourceName() {
// ------------------------------------------------------------- Properties
return dataSourceName;
}
Return the name of the JNDI JDBC DataSource. |
public boolean getLocalDataSource() {
return localDataSource;
}
Return if the datasource will be looked up in the webapp JNDI Context. |
protected String getName() {
return (name);
}
Return a short name for this Realm implementation. |
protected String getPassword(String username) {
Connection dbConnection = null;
// Ensure that we have an open database connection
dbConnection = open();
if (dbConnection == null) {
return null;
}
try {
return getPassword(dbConnection, username);
} finally {
close(dbConnection);
}
}
Return the password associated with the given principal's user name. |
protected String getPassword(Connection dbConnection,
String username) {
ResultSet rs = null;
PreparedStatement stmt = null;
String dbCredentials = null;
try {
stmt = credentials(dbConnection, username);
rs = stmt.executeQuery();
if (rs.next()) {
dbCredentials = rs.getString(1);
}
return (dbCredentials != null) ? dbCredentials.trim() : null;
} catch(SQLException e) {
containerLog.error(
sm.getString("dataSourceRealm.getPassword.exception",
username));
} finally {
try {
if (rs != null) {
rs.close();
}
if (stmt != null) {
stmt.close();
}
} catch (SQLException e) {
containerLog.error(
sm.getString("dataSourceRealm.getPassword.exception",
username));
}
}
return null;
}
Return the password associated with the given principal's user name. |
protected Principal getPrincipal(String username) {
Connection dbConnection = open();
if (dbConnection == null) {
return new GenericPrincipal(this,username, null, null);
}
try {
return (new GenericPrincipal(this,
username,
getPassword(dbConnection, username),
getRoles(dbConnection, username)));
} finally {
close(dbConnection);
}
}
Return the Principal associated with the given user name. |
public String getRoleNameCol() {
return roleNameCol;
}
Return the column in the user role table that names a role. |
protected ArrayList getRoles(String username) {
Connection dbConnection = null;
// Ensure that we have an open database connection
dbConnection = open();
if (dbConnection == null) {
return null;
}
try {
return getRoles(dbConnection, username);
} finally {
close(dbConnection);
}
}
Return the roles associated with the given user name. |
protected ArrayList getRoles(Connection dbConnection,
String username) {
ResultSet rs = null;
PreparedStatement stmt = null;
ArrayList< String > list = null;
try {
stmt = roles(dbConnection, username);
rs = stmt.executeQuery();
list = new ArrayList< String >();
while (rs.next()) {
String role = rs.getString(1);
if (role != null) {
list.add(role.trim());
}
}
return list;
} catch(SQLException e) {
containerLog.error(
sm.getString("dataSourceRealm.getRoles.exception", username));
}
finally {
try {
if (rs != null) {
rs.close();
}
if (stmt != null) {
stmt.close();
}
} catch (SQLException e) {
containerLog.error(
sm.getString("dataSourceRealm.getRoles.exception",
username));
}
}
return null;
}
Return the roles associated with the given user name |
public String getUserCredCol() {
return userCredCol;
}
Return the column in the user table that holds the user's credentials. |
public String getUserNameCol() {
return userNameCol;
}
Return the column in the user table that holds the user's name. |
public String getUserRoleTable() {
return userRoleTable;
}
Return the table that holds the relation between user's and roles. |
public String getUserTable() {
return userTable;
}
Return the table that holds user data.. |
protected Connection open() {
try {
Context context = null;
if (localDataSource) {
context = ContextBindings.getClassLoader();
context = (Context) context.lookup("comp/env");
} else {
StandardServer server =
(StandardServer) ServerFactory.getServer();
context = server.getGlobalNamingContext();
}
DataSource dataSource = (DataSource)context.lookup(dataSourceName);
return dataSource.getConnection();
} catch (Exception e) {
// Log the problem for posterity
containerLog.error(sm.getString("dataSourceRealm.exception"), e);
}
return null;
}
Open the specified database connection. |
public void setDataSourceName(String dataSourceName) {
this.dataSourceName = dataSourceName;
}
Set the name of the JNDI JDBC DataSource. |
public void setLocalDataSource(boolean localDataSource) {
this.localDataSource = localDataSource;
}
Set to true to cause the datasource to be looked up in the webapp JNDI
Context. |
public void setRoleNameCol(String roleNameCol) {
this.roleNameCol = roleNameCol;
}
Set the column in the user role table that names a role. |
public void setUserCredCol(String userCredCol) {
this.userCredCol = userCredCol;
}
Set the column in the user table that holds the user's credentials. |
public void setUserNameCol(String userNameCol) {
this.userNameCol = userNameCol;
}
Set the column in the user table that holds the user's name. |
public void setUserRoleTable(String userRoleTable) {
this.userRoleTable = userRoleTable;
}
Set the table that holds the relation between user's and roles. |
public void setUserTable(String userTable) {
this.userTable = userTable;
}
Set the table that holds user data. |
public void start() throws LifecycleException {
// Perform normal superclass initialization
super.start();
// Create the roles PreparedStatement string
StringBuffer temp = new StringBuffer("SELECT ");
temp.append(roleNameCol);
temp.append(" FROM ");
temp.append(userRoleTable);
temp.append(" WHERE ");
temp.append(userNameCol);
temp.append(" = ?");
preparedRoles = temp.toString();
// Create the credentials PreparedStatement string
temp = new StringBuffer("SELECT ");
temp.append(userCredCol);
temp.append(" FROM ");
temp.append(userTable);
temp.append(" WHERE ");
temp.append(userNameCol);
temp.append(" = ?");
preparedCredentials = temp.toString();
}
Prepare for active use of the public methods of this Component. |
public void stop() throws LifecycleException {
// Perform normal superclass finalization
super.stop();
}
Gracefully shut down active use of the public methods of this Component. |