簡體   English   中英

如何從備用Bean獲取Primefaces Password組件中的強度指示器

[英]How to get strength indicator in Primefaces Password component from backing 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>

現在的問題是,我不知道如何從支持bean的UI中獲取“弱”或“強”消息。 有人可以幫助我嗎?

我正在使用JSF 2和PrimeFaces 3.4。

行為

將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密碼組件將由31個字母(無大寫字母,數字或符號)組成的密碼視為強密碼,這不是true
  • 要測試PrimeFaces組件,請執行PrimeFaces.widget.Password.prototype.testStrength(PF('password-widget-var').jq.val())
  • 要獲取javascript函數的代碼,請執行PrimeFaces.widget.Password.prototype.testStrengthPrimeFaces.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("[AZ]"); 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=ac;if(b<=0){return a/c}else{return 1+0.5*(a/(a+c/4))}} 
  • 沒有理由在Java中使用浮點類型進行評分,因此我使用了整數。

  • 兩種實現都只接受拉丁字符集

(再次)完美!!!

因此,您在后端要做的就是將密碼傳遞給PasswordValidator,您將獲得它的強度,該強度將與PrimeFaces計算的強度相同。 要計算由PrimeFaces計算的密碼是弱密碼,好密碼還是強密碼,請使用PasswordValidator的相應方法。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM