/** Override the parent method to update the last login date on successful authentication. */ protected void successfulAuthentication( HttpServletRequest request, HttpServletResponse response, FilterChain chain, Authentication auth) throws IOException, ServletException { super.successfulAuthentication(request, response, chain, auth); Object principal = auth.getPrincipal(); // find authenticated username String username = null; if (principal instanceof UserDetails) { // using custom authentication with Spring Security UserDetail service username = ((UserDetails) principal).getUsername(); } else if (principal instanceof String) { // external authentication returns only username username = String.valueOf(principal); } if (username != null) { try { WikiUser wikiUser = WikiBase.getDataHandler().lookupWikiUser(username); if (wikiUser != null) { wikiUser.setLastLoginDate(new Timestamp(System.currentTimeMillis())); WikiBase.getDataHandler().writeWikiUser(wikiUser, wikiUser.getUsername(), ""); // update password reset challenge fields, just in case wikiUser.setChallengeValue(null); wikiUser.setChallengeDate(null); wikiUser.setChallengeIp(null); wikiUser.setChallengeTries(0); WikiBase.getDataHandler().updatePwResetChallengeData(wikiUser); } } catch (WikiException e) { // log but do not throw - failure to update last login date is non-fatal logger.error("Failure while updating last login date for " + username, e); } } }
public void success( HttpServletRequest request, HttpServletResponse response, Authentication authResult) throws IOException, ServletException { super.successfulAuthentication(request, response, null, authResult); }