输入匹配文件无法正常工作 - Java

时间:2014-10-29 01:51:22

标签: java file-io

**此方法实际上在正确时返回true。现在的问题与LoginModule(A JAAS功能)有关。

在下面的Java方法中,我读取了一个文件中的行," file.txt"。在读取I输出每一行时(这很好),然后我检查每一行以匹配一个字符串" creds"它由用户名密码组合组成。我试图使用" creds",精确的字符串进行测试,正如您在下面使用三种不同类型的匹配方法所看到的那样。没有工作和返回布尔"结果"总是假的。有没有人遇到类似的问题或能够帮助我?

        // Now perform validation. For login can either read from a file or a 
        // database. You can also incorporate secure password  handling here. 
        System.out.println("Checking username and password: " + username +"/" + password);

        String creds = username + ',' + password;
        boolean result = false;
        try {

              File file = new File("file.txt");
                FileReader fr = new FileReader(file);
                BufferedReader br = new BufferedReader(fr);
                String line;
                while((line = br.readLine()) != null){
                    System.out.println(line);
                    if(line == creds || line.equals(creds) || line.contains(creds)){
                        result = true;
                    }
                }
                br.close();
                fr.close();
        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }


    return result; // successful login.


    }

方法读取的文件如下所示:

alicebob,thisissecure
tracyadam,mypass1

1 个答案:

答案 0 :(得分:0)

问题是LoginModule的配置不正确。我有一个浮动布尔值,我需要在匹配方法上声明True。由于这是假的,我的登录失败了。去展示,了解你的环境和正确的课程配置。

下面是Java LoginModule我指的是: http://docs.oracle.com/javase/7/docs/api/javax/security/auth/spi/LoginModule.html