]> git.aero2k.de Git - tmp/jakarta-migration.git/blob
f83cfdb311ad760fac4f629423e0d757abc67445
[tmp/jakarta-migration.git] /
1 package org.collectionspace.authentication;
2
3 import java.sql.Connection;
4 import java.sql.PreparedStatement;
5 import java.sql.ResultSet;
6 import java.sql.SQLException;
7
8 import javax.security.auth.login.AccountException;
9 import javax.security.auth.login.AccountNotFoundException;
10
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;
16
17 public class CSpaceAuthenticationSuccessEvent implements ApplicationListener<AuthenticationSuccessEvent> {
18         
19         final private static String UPDATE_USER_SQL =
20                         "UPDATE users SET lastlogin = now() WHERE username = ?";
21
22         @Override
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();
28                 
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();
34                                 try {
35                                         setLastLogin(cspaceDbRealm, userName);
36                                 } catch (Exception e) {
37                                         // TODO Auto-generated catch block
38                                         e.printStackTrace();
39                                 }
40                                 System.out.println();
41                         }
42                 }
43         }
44         
45         private void setLastLogin(CSpaceDbRealm cspaceDbRealm, String username) throws AccountException {
46         Connection conn = null;
47         PreparedStatement ps = null;
48         ResultSet rs = null;
49         try {
50             conn = cspaceDbRealm.getConnection();
51             // Get the salt
52
53             ps = conn.prepareStatement(UPDATE_USER_SQL);
54             ps.setString(1, username);
55             rs = ps.executeQuery();
56             if (rs.next() == false) {
57                 throw new AccountNotFoundException("No matching username found");
58             }
59         } catch (SQLException ex) {
60                 // Assuming PostgreSQL
61             if (PSQLState.UNDEFINED_COLUMN.getState().equals(ex.getSQLState())) {
62                 System.err.println("'USERS' table is missing 'lastlogin' column.");
63             } else {
64                 AccountException ae = new AccountException("Authentication query failed: " + ex.getLocalizedMessage());
65                 ae.initCause(ex);
66                 throw ae;
67             }
68         } catch (AccountNotFoundException ex) {
69             throw ex;
70         } catch (Exception ex) {
71             AccountException ae = new AccountException("Unknown Exception");
72             ae.initCause(ex);
73             throw ae;
74         } finally {
75             if (rs != null) {
76                 try {
77                     rs.close();
78                 } catch (SQLException e) {
79                 }
80             }
81             if (ps != null) {
82                 try {
83                     ps.close();
84                 } catch (SQLException e) {
85                 }
86             }
87             if (conn != null) {
88                 try {
89                     conn.close();
90                 } catch (SQLException ex) {
91                 }
92             }
93         }
94     }
95
96
97 }