您的位置:首页 > 编程语言 > Java开发

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

2012-06-26 14:53 239 查看
转载自: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;


/* 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. */
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: