Tigase用户登录

src/main/java/tigase/db/jdbc/TigaseCustomAuth.java

/**
     * Performs user login. Normally used when there is a special SP used for this
     * purpose. This is an alternative way to a method requiring retrieving user
     * password. Therefore at least one of those queries must be defined:
     * <code>user-login-query</code> or <code>get-password-query</code>.
     *
     * If both queries are defined then <code>user-login-query</code> is used.
     * Normally this method should be only used with plain text password
     * authentication or sasl-plain.
     *
     * The Tigase server expects a result set with user_id to be returned from the
     * query if login is successful and empty results set if the login is
     * unsuccessful.
     *
     * Takes 2 arguments: <code>(user_id (JID), password)</code>
     *
     * Example query:
     *
     * <pre>
     * select user_id from tig_users where (user_id = ?) AND (user_pw = ?)
     * </pre>
     */
    public static final String DEF_USERLOGIN_KEY = "user-login-query";
/** Field description */
    public static final String DEF_USERLOGIN_QUERY = "{ call TigUserLoginPlainPw(?, ?) }";
// ~--- methods --------------------------------------------------------------

    @Override
    public void initRepository(final String connection_str, Map<String, String> params)
            throws DBInitException {
        try {
            data_repo = RepositoryFactory.getDataRepository(null, connection_str, params);
            initdb_query = getParamWithDef(params, DEF_INITDB_KEY, DEF_INITDB_QUERY);

            if (initdb_query != null) {
                data_repo.initPreparedStatement(initdb_query, initdb_query);
            }

            adduser_query = getParamWithDef(params, DEF_ADDUSER_KEY, DEF_ADDUSER_QUERY);

            if ((adduser_query != null)) {
                data_repo.initPreparedStatement(adduser_query, adduser_query);
            }

            deluser_query = getParamWithDef(params, DEF_DELUSER_KEY, DEF_DELUSER_QUERY);

            if ((deluser_query != null)) {
                data_repo.initPreparedStatement(deluser_query, deluser_query);
            }

            getpassword_query = getParamWithDef(params, DEF_GETPASSWORD_KEY, DEF_GETPASSWORD_QUERY);

            if ((getpassword_query != null)) {
                data_repo.initPreparedStatement(getpassword_query, getpassword_query);
            }

            updatepassword_query =
                    getParamWithDef(params, DEF_UPDATEPASSWORD_KEY, DEF_UPDATEPASSWORD_QUERY);

            if ((updatepassword_query != null)) {
                data_repo.initPreparedStatement(updatepassword_query, updatepassword_query);
            }

            userlogin_query = getParamWithDef(params, DEF_USERLOGIN_KEY, DEF_USERLOGIN_QUERY);
            if (userlogin_query  != null) {
                data_repo.initPreparedStatement(userlogin_query, userlogin_query);//查询数据库,调用TigUserLoginPlainPw 方法对账号密码进行校验
                userlogin_active = true;
            }//登录验证位置

            userlogout_query =
                    getParamWithDef(params, DEF_USERLOGOUT_KEY, DEF_USERLOGOUT_QUERY);

            if ((userlogout_query != null)) {
                data_repo.initPreparedStatement(userlogout_query, userlogout_query);
            }

            userscount_query =
                    getParamWithDef(params, DEF_USERS_COUNT_KEY, DEF_USERS_COUNT_QUERY);

            if ((userscount_query != null)) {
                data_repo.initPreparedStatement(userscount_query, userscount_query);
            }

            userdomaincount_query =
                    getParamWithDef(params, DEF_USERS_DOMAIN_COUNT_KEY,
                            DEF_USERS_DOMAIN_COUNT_QUERY);

            if ((userdomaincount_query != null)) {
                data_repo.initPreparedStatement(userdomaincount_query, userdomaincount_query);
            }

            nonsasl_mechs =
                    getParamWithDef(params, DEF_NONSASL_MECHS_KEY, DEF_NONSASL_MECHS).split(",");
            sasl_mechs = getParamWithDef(params, DEF_SASL_MECHS_KEY, DEF_SASL_MECHS).split(",");

            if ((params != null) && (params.get("init-db") != null)) {
                initDb();
            }
        } catch (Exception e) {
            data_repo = null;

            throw new DBInitException(
                    "Problem initializing jdbc connection: " + connection_str, e);
        }
    }

src/main/java/tigaase/db/derby/StoredProcedures.java

/**
     * Method description
     *
     *账号密码MD5解密,客户端传输过来的账号密码是经过MD5加密过的
     * @param userId
     * @param userPw
     * @param data
     *
     * @throws SQLException
     */
    public static void tigUserLoginPlainPw(String userId, String userPw, ResultSet[] data)
            throws SQLException {
        String encMethod = tigGetDBProperty("password-encoding");
        String encp = encodePassword(encMethod, userId, userPw);

        tigUserLogin(userId, encp, data);
    }

数据库查询

/**
     * Method description
     *
     *
     * @param userId
     * @param userPw
     * @param data
     *
     * @throws SQLException
     */
    public static void tigUserLogin(String userId, String userPw, ResultSet[] data)
            throws SQLException {
        Connection conn = DriverManager.getConnection("jdbc:default:connection");

        conn.setTransactionIsolation(Connection.TRANSACTION_READ_COMMITTED);

        try {
            PreparedStatement ps =
                conn.prepareStatement("select user_id from tig_users where (account_status > 0) AND ( lower(user_id) = ?) AND (user_pw = ?)");

            ps.setString(1, userId.toLowerCase());
            ps.setString(2, userPw);

            ResultSet rs = ps.executeQuery();

            if (rs.next()) {
                PreparedStatement x = conn.prepareStatement("values '" + userId + "'");

                data[0] = x.executeQuery();

                PreparedStatement flps =
                    conn.prepareStatement("update tig_users set online_status = online_status + 1, last_login = current timestamp where lower(user_id) =  ?");

                flps.setString(1, userId.toLowerCase());
                flps.executeUpdate();
            } else {
                PreparedStatement x = conn.prepareStatement("values '-'");

                data[0] = x.executeQuery();

                PreparedStatement flps =
                    conn.prepareStatement("update tig_users set failed_logins = failed_logins + 1 where lower(user_id) = ?");

                flps.setString(1, userId.toLowerCase());
                flps.executeUpdate();
            }
        } catch (SQLException e) {

            // e.printStackTrace();
            // log.log(Level.SEVERE, "SP error", e);
            throw e;
        } finally {
            conn.close();
        }
    }

参考
https://blog.csdn.net/huwenfeng_2011/article/details/43413377

猜你喜欢

转载自blog.csdn.net/w690333243/article/details/80206170