zoukankan      html  css  js  c++  java
  • 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. 
    */


  • 相关阅读:
    mysql主从同步图
    VS2010智能提示失效,关键字不智能提示!
    mvc3的SaveChanges()方法无效,数据并没有更新!
    使用uploadify上传插件时遇到 NetworkError: 403 Forbidden http://xxxx/xxxx/ 错误
    Asp.net 调用mysql存储过程参数传中文乱码!
    The entity type XXXInfo is not part of the model for the current context.
    爬取汽车网站汽车数据
    《牛顿和莱布尼兹对最速降落线问题的解法,少为人知》 回复
    《谁能证明:标准波面的光若能汇集于一点,则它们的光程长度必然相等》 回复
    200^199 和 199^200 哪个大 ?
  • 原文地址:https://www.cnblogs.com/java20130723/p/3212270.html
Copyright © 2011-2022 走看看