【发布时间】:2014-05-03 16:20:03
【问题描述】:
我制作了一个 Java 小程序,要求人们输入密码。输入 pin 码后,它会立即读入一个“bdd.txt”文件,其中存储了所有 pin,然后显示 :) 是否正确和 :( 如果错误。到目前为止是简单的应用程序。
我想做的是将那个“数据库”文件移动到我计算机上的虚拟机中(例如 Ubuntu),然后做同样的事情。这样,它就不再是本地的了,因为该文件将不再位于我的项目的根目录中。
这是我的应用程序的样子:
如您所见,应用启动后,要求用户输入 PIN 码。如果这是一个好的,应用程序就完成了,如果不是,他还有 2 次尝试,直到应用程序停止。
输入 pin 后,我的程序会检查“bdd.txt”是否存在 pin。它扮演数据库角色:
要了解我需要什么,有必要将此程序同化为需要安全的东西。我们不希望引脚数据库与程序(或现实生活中的设备)位于同一位置。所以我们把它放在一个虚拟机上,我们必须在 Eclipse 中的 Windows7 Java 程序和 VMWare Player 的 Ubuntu 上的 bdd.txt 文件之间进行通信。
我的问题是这怎么可能?我如何需要更改我的代码以让我的程序到达我的 VM 上的某些内容?有没有我应该使用的规范技术?我需要先做一些配置吗?
这是我的代码:
import java.io.*;
import java.util.*;
import java.util.concurrent.atomic.AtomicInteger;
import javax.swing.*;
import javax.swing.text.AttributeSet;
import javax.swing.text.BadLocationException;
import javax.swing.text.DocumentFilter;
import javax.swing.text.PlainDocument;
import java.awt.*;
import java.awt.event.*;
public class Main extends JFrame {
private static final long serialVersionUID = 1L;
private JPanel container = new JPanel();
private JPasswordField p1 = new JPasswordField(4);
private JLabel label = new JLabel("Enter Pin: ");
private JButton b = new JButton("OK");
public Main() {
this.setTitle("NEEDS");
this.setSize(300, 500);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setLocationRelativeTo(null);
container.setBackground(Color.white);
container.setLayout(new BorderLayout());
container.add(p1);
JPanel top = new JPanel();
PlainDocument document =(PlainDocument)p1.getDocument();
b.addActionListener(new BoutonListener());
top.add(label);
top.add(p1);
p1.setEchoChar('*');
top.add(b);
document.setDocumentFilter(new DocumentFilter(){
@Override
public void replace(DocumentFilter.FilterBypass fb, int offset, int length, String text, AttributeSet attrs) throws BadLocationException {
String string =fb.getDocument().getText(0, fb.getDocument().getLength())+text;
if(string.length() <= 4)
super.replace(fb, offset, length, text, attrs); //To change body of generated methods, choose Tools | Templates.
}
});
this.setContentPane(top);
this.setVisible(true);
}
class BoutonListener implements ActionListener {
private final AtomicInteger nbTry = new AtomicInteger(0);
ArrayList<Integer> pins = readPinsData(new File("bdd.txt"));
@SuppressWarnings("deprecation")
public void actionPerformed(ActionEvent e) {
if (nbTry.get() > 2) {
JOptionPane.showMessageDialog(null,
"Pin blocked due to 3 wrong tries");
return;
}
final String passEntered=p1.getText().replaceAll("\u00A0", "");
if (passEntered.length() != 4) {
JOptionPane.showMessageDialog(null, "Pin must be 4 digits");
return;
}
//JOptionPane.showMessageDialog(null, "Checking...");
//System.out.println("Checking...");
SwingWorker<Void, Void> worker = new SwingWorker<Void, Void>() {
@Override
protected Void doInBackground() throws Exception {
boolean authenticated = false;
if (pins.contains(Integer.parseInt(passEntered))) {
JOptionPane.showMessageDialog(null, ":)");
authenticated = true;
}
if (!authenticated) {
JOptionPane.showMessageDialog(null, ":(");
nbTry.incrementAndGet();
}
return null;
}
};
worker.execute();
}
}
//Function to read/access my bdd.txt file
static public ArrayList<Integer> readPinsData(File dataFile) {
final ArrayList<Integer> data=new ArrayList<Integer>();
try {
BufferedReader reader = new BufferedReader(new FileReader(dataFile));
String line;
try {
while ((line = reader.readLine()) != null) {
try {
data.add(Integer.parseInt(line));
} catch (NumberFormatException e) {
e.printStackTrace();
System.err.printf("error parsing line '%s'\n", line);
}
}
} finally {
reader.close();
}
} catch (Exception e) {
e.printStackTrace();
System.err.println("error:"+e.getMessage());
}
return data;
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
new Main();
}
});
}
}
有什么想法吗?谢谢,
弗洛伦特。
【问题讨论】:
-
您是否考虑过添加一个应用程序可以看到并且VM 可以读取的共享文件夹?我认为 VirtualBox 支持类似的东西。
-
如果程序可以访问 pin 列表,那么用户也可以。 pin检查应该由用户无权访问的机器执行,并且您的程序通过安全通道连接到该机器。 SSL/TLS 是一种简单的方法,当然还有其他方法。
-
基本上,在您的计算机上运行的 VM 与通过网络连接的房间内的单独计算机之间没有区别。两种情况都可以使用相同的选项,并且需要基本相同的设置。
标签: java linux eclipse web-services virtual