Skip to content

Commit

Permalink
redis6
Browse files Browse the repository at this point in the history
  • Loading branch information
chenby committed Jan 8, 2020
1 parent 5d5b30d commit f6892d6
Showing 1 changed file with 25 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import static javax.net.ssl.TrustManagerFactory.getDefaultAlgorithm;

import java.io.FileInputStream;
import java.io.InputStream;
import java.security.KeyStore;
import java.security.cert.X509Certificate;

Expand All @@ -45,6 +46,20 @@ public class RedisSslContextFactory implements SslContextFactory {
private String trustStorePath;
private String trustStoreType;
private String trustStorePassword;

/*
* if loader == null use absolute file path to load keystore and truststore
* else use classpath to load keystore and truststore. by default use file path.
*/
private final ClassLoader loader;

public RedisSslContextFactory() {
this(null);
}

public RedisSslContextFactory(ClassLoader loader) {
this.loader = loader;
}

public String getProtocol() {
return protocol;
Expand Down Expand Up @@ -119,7 +134,7 @@ public SSLContext create() {

if (keyStorePath != null) {
KeyStore ks = KeyStore.getInstance(requireNonNull(keyStoreType));
try(FileInputStream in = new FileInputStream(keyStorePath)) {
try(InputStream in = getInputStream(loader, keyStorePath)) {
ks.load(in, requireNonNull(keyStorePassword).toCharArray());
KeyManagerFactory kmf = KeyManagerFactory.getInstance(getDefaultAlgorithm());
kmf.init(ks, requireNonNull(keyStorePassword).toCharArray());
Expand All @@ -129,7 +144,7 @@ public SSLContext create() {

if (trustStorePath != null) {
KeyStore ks = KeyStore.getInstance(requireNonNull(trustStoreType));
try(FileInputStream in = new FileInputStream(keyStorePath)) {
try(InputStream in = getInputStream(loader, keyStorePath)) {
ks.load(in, requireNonNull(trustStorePassword).toCharArray());
TrustManagerFactory tmf = TrustManagerFactory.getInstance(getDefaultAlgorithm());
tmf.init(ks);
Expand All @@ -143,6 +158,14 @@ public SSLContext create() {
throw new UnsupportedOperationException(e);
}
}

protected InputStream getInputStream(ClassLoader loader, String path) throws Exception {
if (loader == null) {
return new FileInputStream(path);
} else {
return loader.getResourceAsStream(path);
}
}

private static class TrustAllManager implements X509TrustManager {

Expand Down

0 comments on commit f6892d6

Please sign in to comment.