Skip to content

Commit 48d5e90

Browse files
garyrussellartembilan
authored andcommitted
Configurable Key/Trust Store Algorithm
https://stackoverflow.com/questions/55630983/spring-amqp-compatibility-with-ibm-jvm/55633883#55633883 **cherry-pick to 2.1.x** (cherry picked from commit e3d37e8)
1 parent c71ed1e commit 48d5e90

File tree

1 file changed

+45
-16
lines changed

1 file changed

+45
-16
lines changed

spring-rabbit/src/main/java/org/springframework/amqp/rabbit/connection/RabbitConnectionFactoryBean.java

Lines changed: 45 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -39,9 +39,6 @@
3939
import javax.net.ssl.TrustManager;
4040
import javax.net.ssl.TrustManagerFactory;
4141

42-
import org.apache.commons.logging.Log;
43-
import org.apache.commons.logging.LogFactory;
44-
4542
import org.springframework.amqp.rabbit.support.RabbitExceptionTranslator;
4643
import org.springframework.beans.factory.config.AbstractFactoryBean;
4744
import org.springframework.core.io.Resource;
@@ -57,15 +54,17 @@
5754
import com.rabbitmq.client.impl.nio.NioParams;
5855

5956
/**
60-
* Factory bean to create a RabbitMQ ConnectionFactory, delegating most
61-
* setter methods and optionally enabling SSL, with or without
62-
* certificate validation. When {@link #setSslPropertiesLocation(Resource) sslPropertiesLocation}
63-
* is not null, the default implementation loads a {@code PKCS12} keystore and a
64-
* {@code JKS} truststore using the supplied properties and intializes {@code SunX509} key
65-
* and trust manager factories. These are then used to initialize an {@link SSLContext}
66-
* using the {@link #setSslAlgorithm(String) sslAlgorithm} (default TLSv1.1).
57+
* Factory bean to create a RabbitMQ ConnectionFactory, delegating most setter methods and
58+
* optionally enabling SSL, with or without certificate validation. When
59+
* {@link #setSslPropertiesLocation(Resource) sslPropertiesLocation} is not null, the
60+
* default implementation loads a {@code PKCS12} keystore and a {@code JKS} truststore
61+
* using the supplied properties and intializes key and trust manager factories, using
62+
* algorithm {@code SunX509} by default. These are then used to initialize an
63+
* {@link SSLContext} using the {@link #setSslAlgorithm(String) sslAlgorithm} (default
64+
* TLSv1.1).
6765
* <p>
68-
* Override {@link #createSSLContext()} to create and/or perform further modification of the context.
66+
* Override {@link #createSSLContext()} to create and/or perform further modification of
67+
* the context.
6968
* <p>
7069
* Override {@link #setUpSSL()} to take complete control over setting up SSL.
7170
*
@@ -80,7 +79,7 @@
8079
*/
8180
public class RabbitConnectionFactoryBean extends AbstractFactoryBean<ConnectionFactory> {
8281

83-
private final Log logger = LogFactory.getLog(getClass());
82+
private static final String SUN_X509 = "SunX509";
8483

8584
private static final String KEY_STORE = "keyStore";
8685

@@ -136,6 +135,10 @@ public class RabbitConnectionFactoryBean extends AbstractFactoryBean<ConnectionF
136135

137136
private boolean enableHostnameVerification = true;
138137

138+
private String keyStoreAlgorithm = SUN_X509;
139+
140+
private String trustStoreAlgorithm = SUN_X509;
141+
139142
public RabbitConnectionFactoryBean() {
140143
this.connectionFactory.setAutomaticRecoveryEnabled(false);
141144
}
@@ -629,6 +632,32 @@ public void setEnableHostnameVerification(boolean enable) {
629632
this.enableHostnameVerification = enable;
630633
}
631634

635+
protected String getKeyStoreAlgorithm() {
636+
return this.keyStoreAlgorithm;
637+
}
638+
639+
/**
640+
* Set the algorithm used when creating the key store, default {@code SunX509}.
641+
* @param keyStoreAlgorithm the algorithm.
642+
* @since 2.1.6
643+
*/
644+
public void setKeyStoreAlgorithm(String keyStoreAlgorithm) {
645+
this.keyStoreAlgorithm = keyStoreAlgorithm;
646+
}
647+
648+
protected String getTrustStoreAlgorithm() {
649+
return this.trustStoreAlgorithm;
650+
}
651+
652+
/**
653+
* Set the algorithm used when creating the trust store, default {@code SunX509}.
654+
* @param trustStoreAlgorithm the algorithm.
655+
* @since 2.1.6
656+
*/
657+
public void setTrustStoreAlgorithm(String trustStoreAlgorithm) {
658+
this.trustStoreAlgorithm = trustStoreAlgorithm;
659+
}
660+
632661
@Override
633662
public void afterPropertiesSet() {
634663
try {
@@ -703,7 +732,7 @@ private void setupBasicSSL() throws NoSuchAlgorithmException, KeyManagementExcep
703732
}
704733

705734
@Nullable
706-
private KeyManager[] configureKeyManagers() throws KeyStoreException, IOException, NoSuchAlgorithmException,
735+
protected KeyManager[] configureKeyManagers() throws KeyStoreException, IOException, NoSuchAlgorithmException,
707736
CertificateException, UnrecoverableKeyException {
708737
String keyStoreName = getKeyStore();
709738
String keyStorePassword = getKeyStorePassphrase();
@@ -718,15 +747,15 @@ private KeyManager[] configureKeyManagers() throws KeyStoreException, IOExceptio
718747
: this.resolver.getResource(keyStoreName);
719748
KeyStore ks = KeyStore.getInstance(storeType);
720749
ks.load(resource.getInputStream(), keyPassphrase);
721-
KeyManagerFactory kmf = KeyManagerFactory.getInstance("SunX509");
750+
KeyManagerFactory kmf = KeyManagerFactory.getInstance(this.keyStoreAlgorithm);
722751
kmf.init(ks, keyPassphrase);
723752
keyManagers = kmf.getKeyManagers();
724753
}
725754
return keyManagers;
726755
}
727756

728757
@Nullable
729-
private TrustManager[] configureTrustManagers()
758+
protected TrustManager[] configureTrustManagers()
730759
throws KeyStoreException, IOException, NoSuchAlgorithmException, CertificateException {
731760
String trustStoreName = getTrustStore();
732761
String trustStorePassword = getTrustStorePassphrase();
@@ -741,7 +770,7 @@ private TrustManager[] configureTrustManagers()
741770
: this.resolver.getResource(trustStoreName);
742771
KeyStore tks = KeyStore.getInstance(storeType);
743772
tks.load(resource.getInputStream(), trustPassphrase);
744-
TrustManagerFactory tmf = TrustManagerFactory.getInstance("SunX509");
773+
TrustManagerFactory tmf = TrustManagerFactory.getInstance(this.trustStoreAlgorithm);
745774
tmf.init(tks);
746775
trustManagers = tmf.getTrustManagers();
747776
}

0 commit comments

Comments
 (0)