[英]Is it possible to load in a KeyStore using JFileChooser?
嗨,我正在開發一個聊天程序,它將在Java中使用密鑰庫。 目前,您可以通過終端中的命令行參數來加載密鑰庫。 該程序將按以下方式啟動java Chat /keystore TEST.keystore
主要方法
public static void main(String[] args) {
int arg_length = args.length;
switch (arg_length) {
case 1:
if (args[0].equalsIgnoreCase("/keystore")) {
keystore = args[1];
File test = new File(keystore);
if (!test.exists()) {
displayHelpInformation();
}
} else {
displayHelpInformation();
}
break;
}
Chat my_server = new Chat(port);
}
密鑰庫聲明為字符串,如下所示,我從給定的輸入流中加載密鑰庫。
private SSLServerSocketFactory getSSLServerSocketFactory() {
SSLContext ctx = null;
try {
KeyManagerFactory kmf;
TrustManagerFactory tmf;
KeyStore ks;
char[] password = "password".toCharArray();
// Returns a SSLContext object that implements the specified secure
// socket protocol.
ctx = SSLContext.getInstance("TLS");
kmf = KeyManagerFactory.getInstance(KeyManagerFactory
.getDefaultAlgorithm());
tmf = TrustManagerFactory.getInstance(TrustManagerFactory
.getDefaultAlgorithm());
// returns keystore object of type Java KeyStore
ks = KeyStore.getInstance("JKS");
ks.load(new FileInputStream(keystore), password);
kmf.init(ks, password);
tmf.init(ks);
ctx.init(kmf.getKeyManagers(), tmf.getTrustManagers(), null);
} catch (Exception e) {
e.printStackTrace();
}
return ctx.getServerSocketFactory();
}
我正在嘗試做的是允許用戶在主窗口中使用JFileChooser而不是使用命令行參數來選擇密鑰庫嗎? 這可能嗎? 我擔心密鑰庫有密碼。
您可以使用JFileChooser做任何您想做的事...最近我一直在使用它,雖然它並不完美,但是很有用。
我的示例將如下所示:
public boolean openFile() {
JFileChooser jfc = new JFileChooser();
// OPTIONAL : if you would like to make the choices only *.keystore
FileFilter ff = new ExtensionFileFilter("KeyStore Files", new String[] { "keystore" });
jfc.setFileFilter(ff);
// END OF OPTIONAL
jfc.setCurrentDirectory(new File(".")); // base folder
jfc.setDialogTitle("Choose a KeyStore");
int jfcReturnValue = jfc.showOpenDialog(parentFrame);
if (jfcReturnValue == JFileChooser.APPROVE_OPTION) {
// sourceFile here would be the *.keystore file selected
File sourceFile = jfc.getSelectedFile();
} else {
System.out.println("Openning a file cancelled.");
return false;
}
return true;
}
如果希望該方法返回File,則可以將返回類型更改為File,但是我通常只發送true或false作為成功或失敗的通知。
至於密碼部分。 如果您使用ks.load(new FileInputStream(keystore), password);
如果成功,則將您從JFileChooser獲得的文件傳遞給它也將起作用。
選修課:
class ExtensionFileFilter extends FileFilter {
String description;
String extensions[];
public ExtensionFileFilter(String description, String extension) {
this(description, new String[] { extension });
}
public ExtensionFileFilter(String description, String extensions[]) {
if (description == null) {
this.description = extensions[0];
} else {
this.description = description;
}
this.extensions = (String[]) extensions.clone();
toLower(this.extensions);
}
private void toLower(String array[]) {
for (int i = 0, n = array.length; i < n; i++) {
array[i] = array[i].toLowerCase();
}
}
public String getDescription() {
return description;
}
public boolean accept(File file) {
if (file.isDirectory()) {
return true;
} else {
String path = file.getAbsolutePath().toLowerCase();
for (int i = 0, n = extensions.length; i < n; i++) {
String extension = extensions[i];
if ((path.endsWith(extension) && (path.charAt(path.length() - extension.length() - 1)) == '.')) {
return true;
}
}
}
return false;
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.