如何在java中屏蔽一个值?

问题描述 投票:0回答:6

我有一个pinNumber值。如何掩盖它(即)说我的pinNumber是否为1234并用四个星号符号掩盖它而不是显示数字。此屏蔽值将使用jsp显示在网页中。

java masking
6个回答
2
投票

在代码中的某个时刻,您将数字转换为字符串以供显示。此时,您可以简单地调用String的replaceAll方法将0-9中的所有字符替换为*字符:

s.replaceAll("[0-9]", "*")

好吧 - 这就是你直接用Java做的。在JSP中,如果您在输入中选择“密码”类型,则会像其他海报所示那样照顾您。


2
投票
<input type="PASSWORD" name="password">

如果要在标签上显示密码,只需显示5或6个星号字符,因为您不希望通过使用密码长度标记该标签的长度来提供线索。


0
投票

你创建这样的形式

<form action="something" method="post">
pinNumber:<input type="password" name="pass"/>
<input type="submit" value="OK"/>
</form>

那么它会变成*


0
投票

此代码可能对您有所帮助。

    class Password {
        final String password; // the string to mask

      Password(String password) { this.password = password; } // needs null protection

         // allow this to be equal to any string
        // reconsider this approach if adding it to a map or something?

      public boolean equals(Object o) {
            return password.equals(o);
        }
        // we don't need anything special that the string doesnt

      public int hashCode() { return password.hashCode(); }

        // send stars if anyone asks to see the string - consider sending just
        // "******" instead of the length, that way you don't reveal the password's length
        // which might be protected information

      public String toString() {
            StringBuilder sb = new StringBuilder();
            for(int i = 0; < password.length(); i++) 
                sb.append("*");
            return sb.toString();
        }
    }

0
投票

我在我的许多应用程序中使用以下代码,它工作得很好。

public class Test {
    public static void main(String[] args) {
        String number = "1234";
        StringBuffer maskValue = new StringBuffer();
        if(number != null && number.length() >0){
            for (int i = 0; i < number.length(); i++) {
                maskValue.append("*");
            }
        }
        System.out.println("Masked Value of 1234 is "+maskValue);
    }
}
Ans - Masked Value of 1234 is ****

0
投票

两种方式(这将掩盖所有字符,而不仅仅是数字):

private String mask(String value) {
    StringBuilder sb = new StringBuilder();
    for(int i = 0; i < value.length(); sb.append("*"), i++); 
    return sb.toString();
}

要么:

private String mask(String value) {
    return value.replaceAll(".", "*");
}

我用第二个

© www.soinside.com 2019 - 2024. All rights reserved.