1 package org.collectionspace.authentication;
3 import java.sql.Connection;
4 import java.sql.PreparedStatement;
5 import java.sql.ResultSet;
6 import java.sql.SQLException;
8 import javax.security.auth.login.AccountException;
9 import javax.security.auth.login.AccountNotFoundException;
11 import org.collectionspace.authentication.realm.db.CSpaceDbRealm;
12 import org.postgresql.util.PSQLState;
13 import org.springframework.context.ApplicationListener;
14 import org.springframework.security.authentication.event.AuthenticationSuccessEvent;
15 import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
17 public class CSpaceAuthenticationSuccessEvent implements ApplicationListener<AuthenticationSuccessEvent> {
19 final private static String UPDATE_USER_SQL =
20 "UPDATE users SET lastlogin = now() WHERE username = ?";
23 public void onApplicationEvent(AuthenticationSuccessEvent event) {
24 // TODO Auto-generated method stub
25 System.out.println(); //org.springframework.security.authentication.UsernamePasswordAuthenticationToken@8a633e91: Principal: org.collectionspace.authentication.CSpaceUser@b122ec20: Username: admin@core.collectionspace.org; Password: [PROTECTED]; Enabled: true; AccountNonExpired: true; credentialsNonExpired: true; AccountNonLocked: true; Granted Authorities: ROLE_1_TENANT_ADMINISTRATOR,ROLE_SPRING_ADMIN; Credentials: [PROTECTED]; Authenticated: true; Details: {grant_type=password, username=admin@core.collectionspace.org}; Granted Authorities: ROLE_1_TENANT_ADMINISTRATOR, ROLE_SPRING_ADMIN
26 String username = null;
27 CSpaceDbRealm cspaceDbRealm = new CSpaceDbRealm();
29 if (event.getSource() instanceof UsernamePasswordAuthenticationToken) {
30 UsernamePasswordAuthenticationToken eventSource = (UsernamePasswordAuthenticationToken)event.getSource();
31 if (eventSource.getPrincipal() instanceof CSpaceUser) {
32 CSpaceUser cspaceUser = (CSpaceUser) eventSource.getPrincipal();
33 username = cspaceUser.getUsername();
35 setLastLogin(cspaceDbRealm, username);
36 } catch (Exception e) {
37 // TODO Auto-generated catch block
44 private void setLastLogin(CSpaceDbRealm cspaceDbRealm, String username) throws AccountException {
45 Connection conn = null;
46 PreparedStatement ps = null;
50 conn = cspaceDbRealm.getConnection();
51 ps = conn.prepareStatement(UPDATE_USER_SQL);
52 ps.setString(1, username);
53 int affected = ps.executeUpdate();
55 String errMsg = String.format("No matching username '%s' found.", username);
56 throw new AccountException(errMsg);
58 } catch (SQLException ex) {
59 // Assuming PostgreSQL
60 if (PSQLState.UNDEFINED_COLUMN.getState().equals(ex.getSQLState())) {
61 System.err.println("'users' table is missing 'lastlogin' column.");
63 AccountException ae = new AccountException("Authentication query failed: " + ex.getLocalizedMessage());
67 } catch (AccountNotFoundException ex) {
69 } catch (Exception ex) {
70 AccountException ae = new AccountException("Unknown Exception");
77 } catch (SQLException e) {
83 } catch (SQLException e) {
89 } catch (SQLException ex) {