• Java Doc 建议不要使用 JPasswordField.getText()


    转载自:http://blog.csdn.net/heyuqi100/article/details/1424166

    想得到用户在 Java Swing 中的 JPasswordField 控件中输入的密码内容,应该使用 JPasswordField.getPassword() ,而不是 JPasswordField.getText() 。因为安全的原因,JavaDoc 中就是如此建议的:

    For security reasons, this method is deprecated. Use the getPassword method instead.


    getPassword() 与 getText() 的差别在于 getPassword 返回的是一个 char[] ,getText() 返回一个 String 。

    咋一想,要是有人想查看内存( 包括物理主存与交换文件 )里的内容,String 与 char[] 在安全上是没什么区别的。

    但是两者最大的区别是不可变性。 String 是一个不可变的对象。一旦被分配内存空间,String 里的字符就不可改变了。如此,这个 String 对象还会在你使用完它后,还会内存中停留上一段时间。然而 char[] 就不一样了,你可以在使用完这个字符数组后,把所有的字符改成 '/0'。

    My Code:

    // HPasswordField.java 

    package heyuqi.swing;

    import java.util.Arrays;
    import javax.swing.JPasswordField;


    /**
     *
     * 
    @author heyuqi< yuqi.he@gmail.com>
     
    */

    public class HPasswordField extends JPasswordField ...{

        
    /**
         *
         * 
    @param passwordField
         * 
    @return
         
    */

        
    public boolean validatePassword( JPasswordField passwordField ) ...{

            
    // get the passwords from this two JPasswordFields
            char firstPassword[]  = this.getPassword();
            
    char secondPassword[] = passwordField.getPassword();
           
            
    boolean returnValue = validatePassword( firstPassword, secondPassword );
           
            
    // set all of the chars in the array to 0
            Arrays.fill( firstPassword, '' );
            Arrays.fill( secondPassword, 
    '' );
           
            
    return returnValue;
        }

       
        
    private boolean validatePassword( char firstPasswd[], char secondPasswd[] ) ...{
           
            
    // if the first password's length is different from the second password,
            
    // return false.
            if( firstPasswd.length != secondPasswd.length ) ...{
                
    return false;
            }

            
    // two passwords have the same string length.
            else ...{
                
    forint i = 0; i < firstPasswd.length; ++i ) ...{
                    
    if( firstPasswd[ i ] != secondPasswd[ i ] ) ...{
                        
    return false;
                    }

                }

            }
     // end if
           
            
    // These two inputs are the same string, return true.
            return true;    }

    }


    /* Copyright (C) 1999, 2002 Free Software Foundation, Inc.

    This file is part of GNU Classpath.

    GNU Classpath is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2, or (at your option)
    any later version.

    GNU Classpath is distributed in the hope that it will be useful, but
    WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with GNU Classpath; see the file COPYING.  If not, write to the
    Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
    02111-1307 USA.

    Linking this library statically or dynamically with other modules is
    making a combined work based on this library.  Thus, the terms and
    conditions of the GNU General Public License cover the whole
    combination.

    As a special exception, the copyright holders of this library give you
    permission to link this library with independent modules to produce an
    executable, regardless of the license terms of these independent
    modules, and to copy and distribute the resulting executable under
    terms of your choice, provided that you also meet, for each linked
    independent module, the terms and conditions of the license of that
    module.  An independent module is a module which is not derived from
    or based on this library.  If you modify this library, you may extend
    this exception to your version of the library, but you are not
    obligated to do so.  If you do not wish to do so, delete this
    exception statement from your version. 
    */


  • 相关阅读:
    ios手机和Android手机测试点区别
    业务下的测试总结
    Mac下web自动化环境部署
    Mac下查找python的安装位置以及第三方库
    简易计算器
    递归
    可变参数
    方法的重载
    方法
    三角形
  • 原文地址:https://www.cnblogs.com/java20130723/p/3212270.html
Copyright © 2020-2023  润新知