diff options
Diffstat (limited to 'jetty-security/src/main/java/org/eclipse/jetty/security/JDBCLoginService.java')
-rw-r--r-- | jetty-security/src/main/java/org/eclipse/jetty/security/JDBCLoginService.java | 118 |
1 files changed, 26 insertions, 92 deletions
diff --git a/jetty-security/src/main/java/org/eclipse/jetty/security/JDBCLoginService.java b/jetty-security/src/main/java/org/eclipse/jetty/security/JDBCLoginService.java index e2f3d8aa4e..7f38d07a49 100644 --- a/jetty-security/src/main/java/org/eclipse/jetty/security/JDBCLoginService.java +++ b/jetty-security/src/main/java/org/eclipse/jetty/security/JDBCLoginService.java @@ -52,7 +52,7 @@ import org.eclipse.jetty.util.security.Credential; * An example properties file for configuration is in * <code>${jetty.home}/etc/jdbcRealm.properties</code> */ -public class JDBCLoginService extends MappedLoginService +public class JDBCLoginService extends AbstractLoginService { private static final Logger LOG = Log.getLogger(JDBCLoginService.class); @@ -64,8 +64,6 @@ public class JDBCLoginService extends MappedLoginService protected String _userTableKey; protected String _userTablePasswordField; protected String _roleTableRoleField; - protected int _cacheTime; - protected long _lastHashPurge; protected Connection _con; protected String _userSql; protected String _roleSql; @@ -74,11 +72,11 @@ public class JDBCLoginService extends MappedLoginService /** * JDBCKnownUser */ - public class JDBCKnownUser extends KnownUser + public class JDBCUserPrincipal extends UserPrincipal { int _userKey; - public JDBCKnownUser(String name, Credential credential, int key) + public JDBCUserPrincipal(String name, Credential credential, int key) { super(name, credential); _userKey = key; @@ -123,9 +121,6 @@ public class JDBCLoginService extends MappedLoginService /* ------------------------------------------------------------ */ - /** - * @see org.eclipse.jetty.security.MappedLoginService#doStart() - */ @Override protected void doStart() throws Exception { @@ -149,20 +144,18 @@ public class JDBCLoginService extends MappedLoginService String _userRoleTable = properties.getProperty("userroletable"); String _userRoleTableUserKey = properties.getProperty("userroletableuserkey"); String _userRoleTableRoleKey = properties.getProperty("userroletablerolekey"); - _cacheTime = new Integer(properties.getProperty("cachetime")); + if (_jdbcDriver == null || _jdbcDriver.equals("") || _url == null || _url.equals("") || _userName == null || _userName.equals("") - || _password == null - || _cacheTime < 0) + || _password == null) { LOG.warn("UserRealm " + getName() + " has not been properly configured"); } - _cacheTime *= 1000; - _lastHashPurge = 0; + _userSql = "select " + _userTableKey + "," + _userTablePasswordField + " from " + _userTable + " where " + _userTableUserField + " = ?"; _roleSql = "select r." + _roleTableRoleField + " from " @@ -177,7 +170,7 @@ public class JDBCLoginService extends MappedLoginService + " = u." + _userRoleTableRoleKey; - Loader.loadClass(this.getClass(), _jdbcDriver).newInstance(); + Loader.loadClass(_jdbcDriver).newInstance(); super.doStart(); } @@ -222,79 +215,11 @@ public class JDBCLoginService extends MappedLoginService } } - /* ------------------------------------------------------------ */ - @Override - public UserIdentity login(String username, Object credentials, ServletRequest request) - { - long now = System.currentTimeMillis(); - if (now - _lastHashPurge > _cacheTime || _cacheTime == 0) - { - _users.clear(); - _lastHashPurge = now; - closeConnection(); - } - - return super.login(username,credentials, request); - } - - /* ------------------------------------------------------------ */ - @Override - protected void loadUsers() - { - } + - /* ------------------------------------------------------------ */ - @Deprecated - protected UserIdentity loadUser(String username) - { - try - { - if (null == _con) - connectDatabase(); - - if (null == _con) - throw new SQLException("Can't connect to database"); - - try (PreparedStatement stat1 = _con.prepareStatement(_userSql)) - { - stat1.setObject(1, username); - try (ResultSet rs1 = stat1.executeQuery()) - { - if (rs1.next()) - { - int key = rs1.getInt(_userTableKey); - String credentials = rs1.getString(_userTablePasswordField); - - - List<String> roles = new ArrayList<String>(); - - try (PreparedStatement stat2 = _con.prepareStatement(_roleSql)) - { - stat2.setInt(1, key); - try (ResultSet rs2 = stat2.executeQuery()) - { - while (rs2.next()) - roles.add(rs2.getString(_roleTableRoleField)); - } - } - return putUser(username, Credential.getCredential(credentials), roles.toArray(new String[roles.size()])); - } - } - } - } - catch (SQLException e) - { - LOG.warn("UserRealm " + getName() + " could not load user information from database", e); - closeConnection(); - } - return null; - } - - /** - * @see org.eclipse.jetty.security.MappedLoginService#loadUserInfo(java.lang.String) - */ - public KnownUser loadUserInfo (String username) + /* ------------------------------------------------------------ */ + public UserPrincipal loadUserInfo (String username) { try { @@ -314,7 +239,7 @@ public class JDBCLoginService extends MappedLoginService int key = rs1.getInt(_userTableKey); String credentials = rs1.getString(_userTablePasswordField); - return new JDBCKnownUser (username, Credential.getCredential(credentials), key); + return new JDBCUserPrincipal (username, Credential.getCredential(credentials), key); } } } @@ -329,13 +254,10 @@ public class JDBCLoginService extends MappedLoginService } - - /** - * @see org.eclipse.jetty.security.MappedLoginService#loadRoleInfo(org.eclipse.jetty.security.MappedLoginService.KnownUser) - */ - public String[] loadRoleInfo (KnownUser user) + /* ------------------------------------------------------------ */ + public String[] loadRoleInfo (UserPrincipal user) { - JDBCKnownUser jdbcUser = (JDBCKnownUser)user; + JDBCUserPrincipal jdbcUser = (JDBCUserPrincipal)user; try { @@ -369,6 +291,18 @@ public class JDBCLoginService extends MappedLoginService } + /* ------------------------------------------------------------ */ + /** + * @see org.eclipse.jetty.util.component.AbstractLifeCycle#doStop() + */ + @Override + protected void doStop() throws Exception + { + closeConnection(); + super.doStop(); + } + + /* ------------------------------------------------------------ */ /** * Close an existing connection */ |