与文件匹配的输入无法正常工作-Java
**此方法在正确时实际返回true。现在的问题与LoginModule(JAAS特性)有关 在下面的Java方法中,我从文件“file.txt”中读取行。在阅读时,我输出每一行(这很好),然后检查每一行以匹配由用户名密码组合组成的字符串“creds”。我试着用“creds”,确切的字符串进行测试,正如您在下面看到的,使用三种不同类型的匹配方法。不起作用,返回的布尔值“result”始终为false。以前有没有人遇到过类似的问题,或者能够帮助我与文件匹配的输入无法正常工作-Java,java,file-io,Java,File Io,**此方法在正确时实际返回true。现在的问题与LoginModule(JAAS特性)有关 在下面的Java方法中,我从文件“file.txt”中读取行。在阅读时,我输出每一行(这很好),然后检查每一行以匹配由用户名密码组合组成的字符串“creds”。我试着用“creds”,确切的字符串进行测试,正如您在下面看到的,使用三种不同类型的匹配方法。不起作用,返回的布尔值“result”始终为false。以前有没有人遇到过类似的问题,或者能够帮助我 // Now perform val
// 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
问题是LoginModule的配置不正确。我有一个浮动布尔值,需要在匹配方法上声明为True。因为这是错误的,我的登录失败了。显示,了解您的环境和正确的类配置 下面是我指的Java LoginModule:
您可以发布输入文件的内容吗?
line==creds
仅当两个对象引用相同时才起作用。要测试两个字符串的相等性,您需要使用.equals()
或.equalsIgnoreCase()
(您已经使用了)。我刚刚测试了它,它对我有效。你确定你的信条完全符合要求吗?我看到您打印用户名/密码,但在cred变量中使用username、password。您在文件中使用的是哪一个?@DustinF您所说的打印只是我打印出来的一个字符串。我参考的是System.out.println(line);我这样做是为了让我知道文件正在被逐行读取,正如我所说的,这可以很好地检查文件以及username
和password
值中的额外空间。