JPasswordField 返回一些转换为字符串类型的哈希码 [英] JPasswordField returning some hash code converted into string type

查看:38
本文介绍了JPasswordField 返回一些转换为字符串类型的哈希码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的程序在初始化程序之前从用户那里获取用户名和密码认证,所以我创建了一个按钮登录,我将 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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆