在 Controller 中,我如何验证密码,使其包含至少1个字母,1个数字,1个特殊字符并且至少8位数字。我尝试使用的代码如下:
boolean validatePassword(String password) {
System.out.println("In validate")
def pattern = /^.*(?=.{7,})(?=.*\d)(?=.*[a-zA-Z])(?=.*[!@#$%*&+()]).*$/
def matcher = password =~ pattern
System.out.println("HERERERE")
return matcher.getCount() ? true : false
}
如果说一切都无效,这将不起作用。
我已经安装了spring security ui插件。有没有办法可以使用其验证功能?
我知道我可以用它来编码密码。
最佳答案
与其尝试在一个正则表达式中完成所有工作,不如将测试分开。由于在Groovy中,Matcher
通过调用find()
强制转换为boolean,因此下面的命令应该起作用,并使意图更清晰。
boolean validatePassword(String pass) {
return (pass) && (pass.length() > 7) && (pass =~ /\p{Alpha}/) &&
(pass =~ /\p{Digit}/) && (pass =~ /[!@#$%*&+()]/)
}
关于grails - Grails验证密码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12513348/