JPasswordField 返回一些转换为字符串类型的哈希码 [英] JPasswordField returning some hash code converted into string type
问题描述
我的程序在初始化程序之前从用户那里获取用户名和密码认证,所以我创建了一个按钮登录,我将 ActionListener 关联到它,如下所示
My program takes user name and password authentication from user before initialising the program, so i created a button login to which i associated ActionListener as show below
login.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent event){
if(txtUserName.getText().equals("Suraj") && (txtPwd.getPassword().toString()).equals("s123")){
dispose();
TimeFrame tFrame = new TimeFrame(userName);
tFrame.setVisible(true);
tFrame.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
tFrame.setLayout(new GridLayout());
} else {
JOptionPane.showMessageDialog(null,"User name or password don't match","Acces Denied", JOptionPane.ERROR_MESSAGE);
}
现在出现的问题是即使我输入正确的密码,程序也会显示错误信息圣人
Now the problem that occurs is even if i enter correct password, program displays an error message
推荐答案
getPassword()
返回一个 char[]
.它上面的 toString()
不会像您假设的那样将内容作为字符串返回.
getPassword()
returns a char[]
. The toString()
on it does not return the contents as a string as you assume.
试试new String(txtPwd.getPassword()).equals("s123")
.
然而,它是 char[]
而不是字符串是有原因的.尝试在 javadoc 中查找它的安全方面.
However, there is a reason it is a char[]
and not a String. Try looking up the security aspect of it in the javadoc.
这篇关于JPasswordField 返回一些转换为字符串类型的哈希码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!