I checked both the logs and the database and neither showed evidence of the postAuthenticate() method actually being called. Here is my custom handler:
public class CustomAuthenticationHandler extends QueryDatabaseAuthenticationHandler {
Logger LOGGER = LoggerFactory.getLogger(CustomAuthenticationHandler.class);
private final String sql;
private final String fieldPassword;
private final String fieldExpired;
private final String fieldDisabled;
private final Map<String, Object> principalAttributeMap;
public CustomAuthenticationHandler(final String name,
final ServicesManager servicesManager,
final PrincipalFactory principalFactory,
final Integer order,
final DataSource dataSource,
final String sql,
final String fieldPassword,
final String fieldExpired,
final String fieldDisabled,
final Map<String, Object> attributes) {
super(name, servicesManager, principalFactory, order, dataSource, sql, fieldPassword, fieldExpired, fieldDisabled, attributes);
this.sql = sql;
this.fieldPassword = fieldPassword;
this.fieldExpired = fieldExpired;
this.fieldDisabled = fieldDisabled;
this.principalAttributeMap = attributes;
if (StringUtils.isBlank(this.fieldPassword)) {
LOGGER.warn("When the password field is left undefined, CAS will skip comparing database and user passwords for equality "
+ ", (specially if the query results do not contain the password field),"
+ "and will instead only rely on a successful query execution with returned results in order to verify credentials");
}
}
@Override
public AuthenticationHandlerExecutionResult postAuthenticate(Credential credential, AuthenticationHandlerExecutionResult result) {
LOGGER.debug("==================================================INSIDE POSTAUTHENTICATE==================================================");
Integer updateResult = updateLastLogin((UsernamePasswordCredential) credential);
if(updateResult != 1)
LOGGER.debug("==================================================BAD UPDATE==================================================");
else
LOGGER.debug("==================================================GOOD UPDATE==================================================");
return super.postAuthenticate(credential, result);
}
private Integer updateLastLogin(final UsernamePasswordCredential credential) {
LOGGER.info("INSIDE updateLastLogin");
return getJdbcTemplate().update("update user_table set last_login = NOW() WHERE username = '" + credential.getUsername() + "';");
}
}
As previously mentioned, the handler itself seems to be getting picked up successfully since I'm seeing these messages in the logs:
cas_1 | 2019-11-26 17:09:29,675 TRACE [org.apereo.cas.authentication.DefaultAuthenticationBuilder] - <Recording authentication handler result success under key [CustomAuthenticationHandler]>
cas_1 | 2019-11-26 17:09:29,675 DEBUG [org.apereo.cas.authentication.PolicyBasedAuthenticationManager] - <Authentication handler [CustomAuthenticationHandler] successfully authenticated [UsernamePasswordCredential(username=myusername, source=null, customFields={})]>