Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do you store a username/password in the Mac Keychain using Java?

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!

like image 342
John Watson Avatar asked Jul 28 '11 05:07

John Watson


People also ask

Where are keychain passwords stored on Mac?

In the Keychain Access app on your Mac, if you don't see a list of keychains, choose Window > Keychain Viewer or press Command-1. Select the keychain that you want to view. To see more information about an item, either double-click it or select it and click the Info button in the toolbar.

Are keychain passwords stored on Mac?

When you are using Outlook or Mac mail client software on an Apple Macs, your passwords are stored in your local Mac keychain under Applications folder. Passwords are stored in the local Mac computer in Keychain 1. Go to Application, then Utilities, then Keychain.


1 Answers

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)

like image 151
Frobbit Avatar answered Nov 12 '22 06:11

Frobbit