我想从支持bean中的xhtml获取强度指标。 如果"指标"弱者说,我会做一些行动。
现在这是我的xhtml代码
<h:form id="changePasswordForm" >
<p:messages id="changePasswordMessages" />
<h:panelGrid columns="3" >
<h:outputText for="oldPassword" value="CurrentPassword" />
<p:password id="oldPassword" value="#{changePasswordBean.oldPassword}"
label="CurrentPassword" required="true" feedback="false" minLength="6" />
<p:message for="oldPassword" display="icon" />
<p:spacer height="4px" />
<p:spacer height="4px" />
<p:spacer height="4px" />
<h:outputText for="newPassword1" value="#{NewPassword}" />
<p:password id="newPassword1" value="#{changePasswordBean.newPassword1}"
label="NewPassword" required="true" feedback="true" minLength="6" match="newPassword2"/>
<p:message for="newPassword1" display="icon" />
<h:outputText for="newPassword2" value="#{ConfirmPassword}" />
<p:password id="newPassword2" value="#{changePasswordBean.newPassword2}"
label="ConfirmPassword" required="true" feedback="true" minLength="6" />
<p:message for="newPassword2" display="icon" />
</h:panelGrid>
<table style="border:0; width:100%;">
<tr>
<td colspan="2">
<p:separator style="margin:0;" />
</td>
</tr>
<tr>
<td class="input" style="width:50%;">
<p:commandButton value="#{Save}"
process=":changePasswordForm"
update=":changePasswordForm"
actionListener="#{changePasswordBean.save()
icon="ui-icon-disk" />
</td>
</tr>
</table>
</h:form>
现在的问题是,我不知道如何得到弱势&#34;或者强大的&#34;来自支持bean中的UI的消息。 有人可以帮帮我吗?
我使用的是JSF 2和PrimeFaces 3.4。
答案 0 :(得分:1)
将JavaScript实现转换为Java。
public class PasswordValidator implements Serializable {
/**
* Less than this is weak, more that this is good.
*/
public final static int MEDIUM = 30;
/**
* More than this is strong.
*/
public final static int STRONG = 80;
private String password;
private int score;
public PasswordValidator() {
}
public PasswordValidator(String password) {
setPassword(password);
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
validatePassword();
}
private void validatePassword() {
score = testStrength(password);
}
public int getScore() {
return score;
}
public boolean isWeak() {
return score <= MEDIUM;
}
public boolean isAtLeastGood() {
return score >= MEDIUM;
}
public boolean isStrong() {
return score >= STRONG;
}
public boolean isSecure() {
return score == 100;
}
public static int testStrength(String d) {
if (d == null || d.isEmpty())
return 0;
//var b=0,c=0,a=this;
float b = 0;
int c;
//c=d.match("[0-9]");b+=a.normalize(c?c.length:1/4,1)*25;
c = countMatches(d, "[0-9]"); // asks for at least one number
b += normalize(c != 0 ? 1 : 1 / 4F, 1) * 25;
//c=d.match("[a-zA-Z]");b+=a.normalize(c?c.length:1/2,3)*10;
c = countMatches(d, "[a-zA-Z]"); // matches only latin characters, not other character sets
b += normalize(c != 0 ? 1 : 1 / 2F, 3) * 10;
//c=d.match("[!@#$%^&*?_~.,;=]");b+=a.normalize(c?c.length:1/6,1)*35;
c = countMatches(d, "[!@#$%^&*?_~.,;=]"); // asks for at least on symbol
b += normalize(c != 0 ? 1 : 1 / 6F, 1) * 35;
//c=d.match("[A-Z]");b+=a.normalize(c?c.length:1/6,1)*30;
c = countMatches(d, "[A-Z]"); // asks for at least one capital letter
b += normalize(c != 0 ? 1 : 1 / 6F, 1) * 30;
//b*=d.length/8;
b *= d.length() / 8F;
System.out.println(b);
//return b>100?100:b
return b > 100 ? 100 : (int) b;
}
private static float normalize(float a, float c) {
return a - c <= 0 ? a / c : 1 + 0.5F * (a / (a + c / 4));
}
private static int countMatches(String container, String regex) {
int i = 0;
Matcher m = Pattern.compile(regex).matcher(container);
while (m.find())
i++;
return i;
}
}
PasswordValidator.testStrength("password"); // e.g. 83
// or
PasswordValidator pv = new PasswordValidator(); // or new PasswordValidator("password")
pv.setPassword("password");
pv.getScore(); // e.g. 83
pv.isAtLeastGood(); // e.g. true
pv.isStrong(); // e.g. true
JavaScript / PrimeFaces实施和我的密码强度得分结果。
Password My class PrimeFaces 123456 28 28.125 Ofar-E*Qnmcm_eSPA 100 100 123456789aZ 88 88.22916666666666 2010.11.02 83 83.33333333333334 mississDOGippi 79 79.47916666666666
完美作品!!!
PrimeFaces.widget.Password.prototype.testStrength(PF('password-widget-var').jq.val())
PrimeFaces.widget.Password.prototype.testStrength
和PrimeFaces.widget.Password.prototype.normalize
PrimeFaces.widget.Password.prototype.testStrength
function (d){
var b=0, c=0, a=this;
c=d.match("[0-9]"); b+=a.normalize(c?c.length:1/4,1)*25;
c=d.match("[a-zA-Z]"); b+=a.normalize(c?c.length:1/2,3)*10;
c=d.match("[!@#$%^&*?_~.,;=]"); b+=a.normalize(c?c.length:1/6,1)*35;
c=d.match("[A-Z]"); b+=a.normalize(c?c.length:1/6,1)*30;
b*=d.length/8;
return b>100?100:b
}
PrimeFaces.widget.Password.prototype.normalize
function (a,c){var b=a-c;if(b<=0){return a/c}else{return 1+0.5*(a/(a+c/4))}}
没有理由在Java中使用float类型得分,所以我使用了整数。
(再次)作品完美!!!
因此,您在后端所要做的就是将密码传递给PasswordValidator,您将获得其强度,这与PrimeFaces计算的值相同。要计算密码是否弱,PrimeFaces计算的好或强使用PasswordValidator的相应方法。