当前位置: 移动技术网 > IT编程>开发语言>Java > 高安全性密码的正则表达式验证

高安全性密码的正则表达式验证

2019年10月26日  | 移动技术网IT编程  | 我要评论

在许多情况下,要求用户的必须符合规则:包含数字,小写英文字母,大写英文字母,特殊字符~!@#$%^&之一,且长度必须>=6

有许多方法,其中一个是正则表达式.

在java中使用正则表达式,并不是很方便,最后还是使用:

import java.util.regex.matcher;
import java.util.regex.pattern;

具体代码如下(此处没有检验长度,只是检验是否包含特定字符):

/**
     * @author lzf
     * @created on 2019年10月24日 下午2:48:38
     * @功能   判断密码是否都包含了 数字,小写英文字母,大写英文字母,特别符号(~!@#$%^&*_)
     * @param passwd 明文密码字符串
     * @return  如果都包含,则返回true,否则返回false..
     */
    private boolean ispasswordvalid(string passwd){
        string regexps="(?<one>[0-9])|(?<two>[a-z])|(?<four>[a-z])|(?<three>[~!@#$%^&*_])";
        pattern r = pattern.compile(regexps);
        matcher ismatch = r.matcher(passwd);
        boolean isfindnumberok=false;
        boolean isfindsmallalphabetok=false;
        boolean isfindbigalphabetok=false;
        boolean isfindspecialsymblok=false;
        while (ismatch.find()){
            string one=ismatch.group("one");
            if (one!=null && isfindnumberok==false){
                isfindnumberok=true;
            }
            string two=ismatch.group("two");
            if (two!=null && isfindsmallalphabetok==false){
                isfindsmallalphabetok=true;
            }
            string three=ismatch.group("three");
            if (three!=null && isfindspecialsymblok==false){
                isfindspecialsymblok=true;
            }
            string four=ismatch.group("four");
            if (four!=null && isfindbigalphabetok==false){
                isfindbigalphabetok=true;
            }
            //system.out.println(one+"--"+two+"----"+three+"----"+four);
        }
        
        if (  isfindnumberok && isfindsmallalphabetok && isfindbigalphabetok && isfindspecialsymblok){
            return true;                    
            //system.out.println("在字符串["+srcstr+"]中发现了数字、小写字母、大写字母和特定符号");
        }
        else{
            return false;
        }
    }

上面的代码,可以满足结果,但不是很高效,甚至特定情况下,不如逐个分析字符串来得高效。不过用于验证密码输入是否满足规则的业务通常对性能要求不高,所以也可以将就!

 

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网