// --------------------------------------------------------- Public Methods
/** *{@inheritDoc} *<p> *IfthereareanyerrorswiththeJDBCconnection,executingthequeryoranythingthismethodreturnsnull *(doesn'tauthenticate).Thiseventisalsologged,andtheconnectionwillbeclosedsothatasubsequentrequest *willautomaticallyre-openit.
*/
@Override public Principal authenticate(String username, String credentials) {
// No user or no credentials // Can't possibly authenticate, don't bother the database then if (username == null || credentials == null) { returnnull;
}
Connection dbConnection = null;
// Ensure that we have an open database connection
dbConnection = open(); if (dbConnection == null) { // If the db connection open fails, return "not authenticated" returnnull;
}
try { // Acquire a Principal object for this user return authenticate(dbConnection, username, credentials);
} finally {
close(dbConnection);
}
}
/** *ReturnthePrincipalassociatedwiththespecifiedusernameandcredentials,ifthereisone;otherwisereturn *<code>null</code>. * *@paramdbConnectionThedatabaseconnectiontobeused *@paramusernameUsernameofthePrincipaltolookup *@paramcredentialsPasswordorothercredentialstouseinauthenticatingthisusername * *@returntheassociatedprincipal,or<code>null</code>ifthereisnone.
*/ protected Principal authenticate(Connection dbConnection, String username, String credentials) { // No user or no credentials // Can't possibly authenticate, don't bother the database then if (username == null || credentials == null) { if (containerLog.isTraceEnabled()) {
containerLog.trace(sm.getString("dataSourceRealm.authenticateFailure", username));
} returnnull;
}
// Look up the user's credentials
String dbCredentials = getPassword(dbConnection, username);
if (dbCredentials == null) { // User was not found in the database. // Waste a bit of time as not to reveal that the user does not exist.
getCredentialHandler().mutate(credentials);
if (containerLog.isTraceEnabled()) {
containerLog.trace(sm.getString("dataSourceRealm.authenticateFailure", username));
} returnnull;
}
// Validate the user's credentials boolean validated = getCredentialHandler().matches(credentials, dbCredentials);
if (validated) { if (containerLog.isTraceEnabled()) {
containerLog.trace(sm.getString("dataSourceRealm.authenticateSuccess", username));
}
} else { if (containerLog.isTraceEnabled()) {
containerLog.trace(sm.getString("dataSourceRealm.authenticateFailure", username));
} returnnull;
}
ArrayList<String> list = getRoles(dbConnection, username);
// Create and return a suitable Principal for this user returnnew GenericPrincipal(username, list);
}
// 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(sm.getString("dataSourceRealm.commit"), 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
}
if (allRolesMode != AllRolesMode.STRICT_MODE && !isRoleStoreDefined()) { // Using an authentication only configuration and no role store has // been defined so don't spend cycles looking returnnull;
}
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.