Key Stores

For managing Java key stores, I suggest using KeyStore Explorer.

Default

KeyStore keystore();

Returns the default key store using configuration from the following system properties:

  • javax.net.ssl.keyStore

  • javax.net.ssl.keyStorePassword

The key store location supports the following protocols:

  • classpath:

  • http:

  • https:

  • file:

    If no protocol is specified, file is assumed. The default key store type is PKCS12.

Usage Example

KeyStore keystore = keystore();

Default with Type

KeyStore keystore(String type);

Same as above but the key store type can be specified. For instance, valid types include: JKS, PKCS12.

Usage Example

KeyStore keystore = keystore("JKS");

From Location

KeyStore keystore(
    String location, 
    char[] password
);

Loads a key store from the given location.

The location parameter supports the protocols described in the default key store api method.

The key store is opened with the given password.

The key store type is assumed to be the default: PKCS12.

Usage Example

// load key store from classpath
KeyStore keystore = keystore(
    "classpath:keystore.p12", 
    "password".toCharArray()
);
// load key store from file
KeyStore keystore = keystore(
    "file:/etc/myapp/keystore.p12", 
    "password".toCharArray()
);
// load key store from https
KeyStore keystore = keystore(
    "https://acme.com/sec/keystore.p12", 
    "password".toCharArray()
);

From Location with Type

KeyStore keystore(
    String location, 
    char[] password, 
    String type
);

Same as in from location but allows you to specify a key store type. For instance, valid types include: JKS, PKCS12.

Usage Examples

// use JKS as the key store type
KeyStore keystore = keystore(
    "classpath:keystore.jks", 
    "password".toCharArray(), 
    "JKS"
);
// use PKCS12 as the key store type
KeyStore keystore = keystore(
    "classpath:keystore.p12", 
    "password".toCharArray(), 
    "PKCS12"
);

Last updated