如何使用 Java 在 Mac 钥匙串中存储用户名/密码?

发布于 2024-11-27 02:46:35 字数 337 浏览 4 评论 0 原文

我想使用 Mac KeyChain 为我的 Java Swing 应用程序存储一些用户名/密码组合,以便代表用户连接到外部服务。

我发现一些资料表明您可以通过执行以下操作来访问 java.security.KeyStore 对象:

KeyStore keyStore = KeyStore.getInstance("KeychainStore", "Apple");
keyStore.load(null, null);

但是,我找不到任何有关如何使用生成的 KeyStore 来实际存储/检索用户名和密码的示例。

任何帮助将不胜感激。

谢谢!

I'd like to use the Mac KeyChain to store some username/password combinations for my Java Swing application to use to connect to external services on behalf of the user.

I have found a few sources demonstrating that you can get access to a java.security.KeyStore object by doing:

KeyStore keyStore = KeyStore.getInstance("KeychainStore", "Apple");
keyStore.load(null, null);

However, I can't find any examples of how you use the resulting KeyStore to actually store/retrieve usernames and passwords.

Any help would be greatly appreciated.

Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

短叹 2024-12-04 02:46:35

您无法从此处“Java KeyStore”获取“Ap​​ple Keychain 密码”

。密钥库抽象仅将证书包装在钥匙串中。

之间的差异

查看钥匙串的本机视图(bash cmd): security dump ~/Library/Keychains/login.keychain

与 Java 的钥匙串视图(bash cmd):

keytool -list -storetype KeychainStore -keystore ~/Library/Keychains/login.keychain

请注意,仅包含证书,并且列表较短。

您将需要为钥匙串使用不同的包装器来访问它的所有功能。

建议:

  • github.com/conormcd/osx-keychain-java
  • 使用 swig 制作 JNI 包装器

另请阅读本机文档:钥匙串服务概念 -- OS X 钥匙串服务任务(见图 1 -3)

You cannot get there "Apple Keychain passwords" from here "Java KeyStore"

The keystore abstraction only wraps the certs in the keychain.

See the difference between the native view of the keychain (bash cmd):

security dump ~/Library/Keychains/login.keychain

vs Java's view of the keychain (bash cmd):

keytool -list -storetype KeychainStore -keystore ~/Library/Keychains/login.keychain

Notice that only certificates are included and the listing is shorter.

You will need to use a different wrapper for Keychain that can access all of it's features.

Suggestions:

  • github.com/conormcd/osx-keychain-java
  • use swig to make JNI wrapper

Also read the native docs: Keychain Services Concepts -- OS X Keychain Services Tasks (see figure 1-3)

遮云壑 2024-12-04 02:46:35

您可以“使用”security 命令行工具,该工具可让您从钥匙串中存储和检索密码。它可能会弹出一个弹出窗口,您必须在其中允许访问。而且,这显然只适用于 OSX。

可以像这样保存密码:

security add-generic-password -s mywonderfulapp -a $username -w $password

要检索密码,您可以执行以下操作:

security -i find-generic-password -l mywonderfulapp -w

为了更轻松地捕获输出,您可以使用 JProc 库,例如:

String password = ProcBuilder.run(
    "security", 
    "-i", "find-generic-password", 
    "-l", "mywonderfulapp", "-w"
);

You could "shell out" to the security commandline tool, which lets you store and retrieve passwords from the keychain. It might bring up a popup where you have to allow the access. Also, this does obviously then only work on OSX.

A password could be saved like so:

security add-generic-password -s mywonderfulapp -a $username -w $password

To retrieve the password you could then do this:

security -i find-generic-password -l mywonderfulapp -w

To make it easier to capture the output you could use the JProc library, e.g. like so:

String password = ProcBuilder.run(
    "security", 
    "-i", "find-generic-password", 
    "-l", "mywonderfulapp", "-w"
);
极度宠爱 2024-12-04 02:46:35

Mac OS X 的 Java 开发指南 指向 关于 java.security.KeyStore 的参考文档。我不熟悉 KeyStore,但也许您会在那里找到您需要的东西。

The Java Development Guide for Mac OS X points to the reference documentation on java.security.KeyStore. I'm not familiar with KeyStore, but perhaps you'll find what you need there.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文