【问题标题】:The script for two factor authentication for getting code from local DB用于从本地数据库获取代码的两因素身份验证脚本
【发布时间】:2021-09-03 11:40:50
【问题描述】:

我的应用程序需要自动化。 首先它会自动输入用户名和密码并点击登录按钮 然后是一个已经存储在数据库中的 OTP 代码,它会自动从数据库中获取静态 OTP 代码并输入到 OTP 文本字段中,然后它允许用户进入应用程序。如果您有自动化脚本,请提供。

【问题讨论】:

  • 请提供足够的代码,以便其他人更好地理解或重现问题。

标签: java database selenium two-factor-authentication


【解决方案1】:

JDBC 不支持本地数据库连接。我的解决方法是使用 SQL CMD 实用程序。

public static String localdbInstanceName = "YOUR LOCAL DB INSTANCE NAME";
public static String sqlCmdCommandPreffix = "sqlcmd -S " + localdbInstanceName + " -E -d master -Q ";

// method to recieve result text from CMD command 
public static List<String> getCmdResults(String command, Boolean wait, Boolean printResults, Boolean printErrors) {
    int exitCode = - 1;
    List<String> results = new ArrayList<String>();
    Process process;
    BufferedReader bufferedReader;
    InputStreamReader inputStreamReader;
    String p1 = null;
    String p2 = null;
    try {
        process = Runtime.getRuntime().exec(command);
        inputStreamReader = new InputStreamReader(process.getInputStream());
        bufferedReader = new BufferedReader(inputStreamReader);
        String line;
        while ((line = bufferedReader.readLine()) != null) {
            if (line.trim().length() > 0) {
                results.add(line.trim());
            }
        }
        p1 = System.lineSeparator() + command + System.lineSeparator() + System.lineSeparator();
        p2 = String.join(System.lineSeparator(), results) + System.lineSeparator() + System.lineSeparator();
        if (printResults) {
            log.info(p1 + p2);
        }
        if (wait) {
            try {
                exitCode = process.waitFor();
            } catch (InterruptedException e) {                  
            }
        }
        else {
            exitCode = process.exitValue();
        }
        if (exitCode != 0 && printErrors) {
            log.error(p1 + p2);
        }
    } catch (IOException e) {
        e.printStackTrace();
    }
    return results;
}

    // example with specific query
    public static String getStartOnStartPage(String dbName, String userId, String regId) {
        String q1 = "SELECT Value FROM [" + dbName + "].[dbo].[Parameter] ";
        String q2 = "WHERE Variable = 'StartOnStartPage' AND UserId = " + userId + " AND IdRegistrace = " + regId + ";";
        List<String> results = getCmdResults(sqlCmdCommandPreffix + "\"" + q1 + q2 + "\"", false, false, true);
        if (results.size() > 0) {
            return results.get(0);
        }
        else {
            return null;
        }
    }

对于 SQL Server express,你可以使用这个:

public static List<String> getSqlResults(Connection connection, String query, String key) {
    List<String> results = new ArrayList<String>();
    try {
        Statement statement = connection.createStatement();
        statement.setQueryTimeout(60);
        ResultSet result = statement.executeQuery(query);
        while (result.next()) {
            results.add(result.getString(key));
        }
    } catch (SQLException e) {
        e.printStackTrace();
    }
    return results;
}

public static Map<String, String> mapSqlResults(Connection connection, String query, String key, String value) {
    Map<String, String> map = new HashMap<String, String>();
    ResultSet result = null;
    try {
        result = connection.createStatement().executeQuery(query);
        while (result.next()) {
            map.put(result.getString(key), result.getString(value));
        }
    } catch (SQLException e) {
        e.printStackTrace();
    }
    return map;
}

【讨论】:

    猜你喜欢
    • 2013-06-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-07-25
    • 2019-07-21
    • 1970-01-01
    • 1970-01-01
    • 2013-07-31
    相关资源
    最近更新 更多