/** * Shuts down the database and removes the realm from the realm map. */ protected void shutDown(String testName) { Connection conn = null; Statement sql = null; DataSource ds = dsMap.get(testName); try { Connection c = ds.getConnection(); Statement s = c.createStatement(); s.executeUpdate("SHUTDOWN"); } catch (SQLException ex) { // ignore } finally { JdbcUtils.closeStatement(sql); JdbcUtils.closeConnection(conn); dsMap.remove(testName); realmMap.remove(testName); } }
/** * Creates and adds test data to user_role and roles_permissions tables. */ protected void createRolesAndPermissions(DataSource ds) { Connection conn = null;; Statement sql = null; try { conn = ds.getConnection(); sql = conn.createStatement(); sql.executeUpdate("create table user_roles (username varchar(20), role_name varchar(20))"); sql.executeUpdate("insert into user_roles values ('" + username + "', '" + testRole + "')"); sql.executeUpdate("create table roles_permissions (role_name varchar(20), permission varchar(40))"); sql.executeUpdate( "insert into roles_permissions values ('" + testRole + "', '" + testPermissionString + "')"); } catch (SQLException ex) { Assert.fail("Exception adding test role and permission"); } finally { JdbcUtils.closeStatement(sql); JdbcUtils.closeConnection(conn); } } }
JdbcUtils.closeConnection(conn);
JdbcUtils.closeConnection(conn);
/** * Creates a test database with a separate salt column in the users table. Sets the * DataSource of the realm associated with the test to a DataSource connected to the database. */ protected void createSaltColumnSchema(String testName) { jdbcDataSource ds = new jdbcDataSource(); ds.setDatabase("jdbc:hsqldb:mem:" + name); ds.setUser("SA"); ds.setPassword(""); Connection conn = null; Statement sql = null; try { conn = ds.getConnection(); sql = conn.createStatement(); sql.executeUpdate( "create table users (username varchar(20), password varchar(20), password_salt varchar(20))"); Sha256Hash sha256Hash = new Sha256Hash(plainTextPassword, salt); String password = sha256Hash.toHex(); sql.executeUpdate("insert into users values ('" + username + "', '" + password + "', '" + salt + "')"); } catch (SQLException ex) { Assert.fail("Exception creating test database"); } finally { JdbcUtils.closeStatement(sql); JdbcUtils.closeConnection(conn); } createRolesAndPermissions(ds); realmMap.get(testName).setDataSource(ds); dsMap.put(testName, ds); }
} finally { JdbcUtils.closeStatement(sql); JdbcUtils.closeConnection(conn);
JdbcUtils.closeConnection(conn);
JdbcUtils.closeConnection(conn);
protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken token) throws AuthenticationException { UsernamePasswordToken upToken = (UsernamePasswordToken) token; String username = upToken.getUsername(); // Null username is invalid if (username == null) { throw new AccountException("Null usernames are not allowed by this realm."); } Connection conn = null; AuthenticationInfo info = null; try { conn = dataSource.getConnection(); String password = getPasswordForUser(conn, username); if (password == null) { throw new UnknownAccountException("No account found for user [" + username + "]"); } info = buildAuthenticationInfo(username, password.toCharArray()); } catch (SQLException e) { final String message = "There was a SQL error while authenticating user [" + username + "]"; if (log.isErrorEnabled()) { log.error(message, e); } // Rethrow any SQL errors as an authentication exception throw new AuthenticationException(message, e); } finally { JdbcUtils.closeConnection(conn); } return info; }