在 Java 应用程序中保存用户设置的最佳方法是什么?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3784657/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-14 04:57:52  来源:igfitidea点击:

What is the best way to save user settings in java application?

java

提问by Feras Odeh

What is the best way to save user settings in java desktop app securely? For example If I want to save an Ftp account settings what is the best way to do that?

在 Java 桌面应用程序中安全保存用户设置的最佳方法是什么?例如,如果我想保存 Ftp 帐户设置,最好的方法是什么?

Thanks

谢谢

采纳答案by Giorgos Dimtsas

The Preferences APIis a nice way to store user and system preferences. If you want to store passwords, you'll have to encrypt them. Here is a nice article that can get you started.

选项API是存储用户和系统首选项的好方法。如果要存储密码,则必须对其进行加密。这是一篇很好的文章,可以帮助您入门。

Encrypted Preferences in Java

Java 中的加密首选项

回答by Cheok Yan Cheng

I usually store in user data directory, with sub directories of application name followed by application version.

我通常存储在用户数据目录中,应用程序名称的子目录后跟应用程序版本。

public static String getUserDataDirectory() {
    return System.getProperty("user.home") + File.separator + ".jstock" + File.separator + getApplicationVersionString() + File.separator;
}

I had been using the following code for 3 years. This method works quite well either in Windows, Linux or Mac.

我已经使用以下代码 3 年了。这种方法在 Windows、Linux 或 Mac 中都非常有效。

Please note that, in Windows, never store it in Program Files, as UAC in Windows Vista or newer may give you a lot of trouble.

请注意,在 Windows 中,切勿将其存储在 中Program Files,因为 Windows Vista 或更新版本中的 UAC 可能会给您带来很多麻烦。

Remember put a dot in-front of your application name, so that it will become a hidden folder in Linux.

记住在你的应用程序名称前面加一个点,这样它在 Linux 中就会变成一个隐藏文件夹。

Good thing by using this methology is that, you are not limited your self in storing primitive value only. Instead, you may save the entire object state to the disk by using xstream

使用这种方法的好处是,您不仅限于存储原始值。相反,您可以使用xstream将整个对象状态保存到磁盘

For example :

例如 :

public static boolean toXML(Object object, File file) {
    XStream xStream = new XStream();
    OutputStream outputStream = null;
    Writer writer = null;

    try {
        outputStream = new FileOutputStream(file);
        writer = new OutputStreamWriter(outputStream, Charset.forName("UTF-8"));
        xStream.toXML(object, writer);
    }
    catch (Exception exp) {
        log.error(null, exp);
        return false;
    }
    finally {
        close(writer);
        close(outputStream);
    }

    return true;
} 

回答by djna

Storing a single password securelyis quite difficult. Suppose you encrypt the password using some secret key. Then when your applciation starts again it needs that secret key, where does it get that from?

安全地存储单个密码非常困难。假设您使用某个密钥加密密码。然后,当您的应用程序再次启动时,它需要该密钥,它从哪里获得?

If it asks the user then he might as well just enter the ftp password which you stored in the first place. If it reads the secret key from somewhere then you need to secure the secret key, and we're back where we started.

如果它询问用户,那么他不妨输入您最初存储的 ftp 密码。如果它从某个地方读取密钥,那么您需要保护密钥,然后我们又回到了开始的地方。

If you are keeping several passwords then asking the user for a single password to some "vault" may be much friendlier, but you then get into all the hassle of dealing with expired passwords.

如果您保留了多个密码,那么向用户询问某个“保险库”的单个密码可能会友好得多,但是您会遇到处理过期密码的所有麻烦。

There are products available to deal with this wort of stuff, if you have a serious need then you probably need to investigate them.

有可用的产品来处理这种麦汁,如果您有严重的需求,那么您可能需要调查它们。