【问题标题】:How to mask a value in java?如何在java中屏蔽一个值?
【发布时间】:2021-08-03 14:02:09
【问题描述】:

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

【问题讨论】:

  • 可能是 ?
  • 你将如何在 jsp 中显示你的密码?为什么你想在你的 java 类中屏蔽你的密码,而不是在屏幕上渲染它时?

标签: java masking


【解决方案1】:

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

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

嗯——这就是你在 Java 中直接做的方式。在 JSP 中,如果您在输入中选择“密码”类型,它会为您处理好,正如其他海报所示。

【讨论】:

    【解决方案2】:
    <input type="PASSWORD" name="password">
    

    如果您要在标签上显示密码,只需显示 5 或 6 个星号字符,因为您不想通过将标签的长度与密码的长度相匹配来提供线索。

    【讨论】:

      【解决方案3】:

      你创建这样的表单

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

      然后它会变成 *

      【讨论】:

        【解决方案4】:

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

            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();
                }
            }
        

        【讨论】:

          【解决方案5】:

          我在我的许多应用程序中使用以下代码,它运行良好。

          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 ****
          

          【讨论】:

            【解决方案6】:

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

            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(".", "*");
            }
            

            我用第二个

            【讨论】:

              【解决方案7】:

              使用@Mask 注释。

              @Mask(prefixNoMaskLen = 3, maskStr = "*", suffixNoMaskLen = 3) 私有字符串手机号码;

              这会将手机号码从 987654321 屏蔽到 987****321

              【讨论】:

                猜你喜欢
                • 1970-01-01
                • 2012-11-26
                • 2012-01-14
                • 1970-01-01
                • 1970-01-01
                • 2017-01-30
                • 1970-01-01
                • 1970-01-01
                • 2017-10-05
                相关资源
                最近更新 更多