如何使用 Java 在 Mac 钥匙串中存储用户名/密码?
我想使用 Mac KeyChain 为我的 Java Swing 应用程序存储一些用户名/密码组合,以便代表用户连接到外部服务。
我发现一些资料表明您可以通过执行以下操作来访问 java.security.KeyStore 对象:
KeyStore keyStore = KeyStore.getInstance("KeychainStore", "Apple");
keyStore.load(null, null);
但是,我找不到任何有关如何使用生成的 KeyStore 来实际存储/检索用户名和密码的示例。
任何帮助将不胜感激。
谢谢!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您无法从此处“Java KeyStore”获取“Apple Keychain 密码”
。密钥库抽象仅将证书包装在钥匙串中。
之间的差异
查看钥匙串的本机视图(bash cmd):
security dump ~/Library/Keychains/login.keychain
与 Java 的钥匙串视图(bash cmd):
keytool -list -storetype KeychainStore -keystore ~/Library/Keychains/login.keychain
请注意,仅包含证书,并且列表较短。
您将需要为钥匙串使用不同的包装器来访问它的所有功能。
建议:
另请阅读本机文档:钥匙串服务概念 -- 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:
Also read the native docs: Keychain Services Concepts -- OS X Keychain Services Tasks (see figure 1-3)
您可以“使用”
security
命令行工具,该工具可让您从钥匙串中存储和检索密码。它可能会弹出一个弹出窗口,您必须在其中允许访问。而且,这显然只适用于 OSX。可以像这样保存密码:
要检索密码,您可以执行以下操作:
为了更轻松地捕获输出,您可以使用 JProc 库,例如:
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:
To retrieve the password you could then do this:
To make it easier to capture the output you could use the JProc library, e.g. like so:
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 withKeyStore
, but perhaps you'll find what you need there.