如何在Appeon PowerBuilder 2022 R2中验证强登录密码?
PowerBuilder 2022 R2 密码验证实现方案
分三个核心模块逐个校验,用PB原生语法实现,避免复杂正则依赖,提升可读性与可维护性。
1. 基础密码复杂度验证
实现函数of_check_complexity,逐个校验密码的长度与字符组成要求:
public function boolean of_check_complexity(string as_password, ref string as_error_msg); boolean lb_has_upper, lb_has_lower, lb_has_digit, lb_has_special integer li_len, i li_len = len(as_password) if li_len < 8 or li_len > 15 then as_error_msg = "密码长度必须在8-15字符之间" return false end if // 遍历字符检查各类别 for i = 1 to li_len char lc_char = mid(as_password, i, 1) if lc_char >= 'A' and lc_char <= 'Z' then lb_has_upper = true else if lc_char >= 'a' and lc_char <= 'z' then lb_has_lower = true else if lc_char >= '0' and lc_char <= '9' then lb_has_digit = true else lb_has_special = true end if next if not lb_has_upper then as_error_msg = "密码必须包含至少1个英文大写字母" return false end if if not lb_has_lower then as_error_msg = "密码必须包含至少1个英文小写字母" return false end if if not lb_has_digit then as_error_msg = "密码必须包含至少1个阿拉伯数字" return false end if if not lb_has_special then as_error_msg = "密码必须包含至少1个非字母数字的特殊字符" return false end if as_error_msg = "" return true end function
2. 新旧密码差异验证
实现函数of_check_diff_count,统计新旧密码的字符差异数,确保至少变更4个字符:
public function boolean of_check_diff_count(string as_old_pwd, string as_new_pwd, ref string as_error_msg); integer li_min_len, i, li_diff_count if len(as_old_pwd) <> len(as_new_pwd) then li_diff_count = abs(len(as_old_pwd) - len(as_new_pwd)) li_min_len = min(len(as_old_pwd), len(as_new_pwd)) else li_diff_count = 0 li_min_len = len(as_old_pwd) end if // 对比相同长度部分的字符 for i = 1 to li_min_len if mid(as_old_pwd, i, 1) <> mid(as_new_pwd, i, 1) then li_diff_count += 1 end if next if li_diff_count < 4 then as_error_msg = "新密码与旧密码至少需变更4个字符" return false end if as_error_msg = "" return true end function
3. 历史密码复用检查
前提准备
数据库需存储用户最近6次的加密密码记录,示例表结构:
CREATE TABLE user_password_history ( user_id VARCHAR(50), encrypted_pwd VARCHAR(64), -- SHA256加密后为64位字符串 create_datetime DATETIME, PRIMARY KEY (user_id, create_datetime) )
PB实现函数
public function boolean of_check_history_password(string as_user_id, string as_new_pwd, ref string as_error_msg); datastore lds_history string ls_encrypted_new_pwd integer li_count // 用PB内置SHA256加密新密码 ls_encrypted_new_pwd = HashSHA256(as_new_pwd) // 查询用户最近6次历史密码 lds_history = create datastore lds_history.dataobject = "d_user_password_history" // 对应上述表的数据窗口 lds_history.settransobject(sqlca) lds_history.retrieve(as_user_id) // 检查是否存在匹配 li_count = 0 do while li_count < lds_history.rowcount() and li_count < 6 li_count += 1 if lds_history.getitemstring(li_count, "encrypted_pwd") = ls_encrypted_new_pwd then as_error_msg = "禁止复用最近6次使用过的密码" destroy lds_history return false end if loop destroy lds_history as_error_msg = "" return true end function
4. 整合验证入口
将三个模块整合为一个入口函数,方便调用:
public function boolean of_validate_new_password(string as_user_id, string as_old_pwd, string as_new_pwd, ref string as_error_msg); if not of_check_complexity(as_new_pwd, as_error_msg) then return false if not of_check_diff_count(as_old_pwd, as_new_pwd, as_error_msg) then return false if not of_check_history_password(as_user_id, as_new_pwd, as_error_msg) then return false return true end function
使用示例
在密码修改窗口的按钮点击事件中调用:
string ls_error_msg n_password_validation ln_pwd_val ln_pwd_val = create n_password_validation if ln_pwd_val.of_validate_new_password(sle_user_id.text, sle_old_pwd.text, sle_new_pwd.text, ls_error_msg) then messagebox("提示", "密码修改成功") // 执行密码更新与历史记录插入逻辑 else messagebox("错误", ls_error_msg) end if destroy ln_pwd_val
内容的提问来源于stack exchange,提问作者Chitti
相关产品推荐
相关产品推荐

