From 9f4e2937c8d49b37449b60baa590748eb84454eb Mon Sep 17 00:00:00 2001 From: Arjen Poutsma Date: Wed, 19 Mar 2014 12:34:27 +0100 Subject: [PATCH] Introduce TrustManagersFactoryBean Introduced TrustManagersFactoryBean for easy configuration of TrustManager instances in Spring XML. Issue: SWS-731 --- .../support/KeyManagersFactoryBean.java | 12 ++- .../support/TrustManagersFactoryBean.java | 101 ++++++++++++++++++ .../support/KeyManagersFactoryBeanTest.java | 48 +++++++++ .../support/TrustManagersFactoryBeanTest.java | 47 ++++++++ 4 files changed, 203 insertions(+), 5 deletions(-) create mode 100644 spring-ws-security/src/main/java/org/springframework/ws/soap/security/support/TrustManagersFactoryBean.java create mode 100644 spring-ws-security/src/test/java/org/springframework/ws/soap/security/support/KeyManagersFactoryBeanTest.java create mode 100644 spring-ws-security/src/test/java/org/springframework/ws/soap/security/support/TrustManagersFactoryBeanTest.java diff --git a/spring-ws-security/src/main/java/org/springframework/ws/soap/security/support/KeyManagersFactoryBean.java b/spring-ws-security/src/main/java/org/springframework/ws/soap/security/support/KeyManagersFactoryBean.java index 33ae521a..fbd7b6ca 100644 --- a/spring-ws-security/src/main/java/org/springframework/ws/soap/security/support/KeyManagersFactoryBean.java +++ b/spring-ws-security/src/main/java/org/springframework/ws/soap/security/support/KeyManagersFactoryBean.java @@ -37,7 +37,7 @@ import org.springframework.util.StringUtils; */ public class KeyManagersFactoryBean implements FactoryBean, InitializingBean { - private KeyManagerFactory keyManagerFactory; + private KeyManager[] keyManagers; private KeyStore keyStore; @@ -58,14 +58,14 @@ public class KeyManagersFactoryBean implements FactoryBean, Initia } /** - * Sets the provider of the key store to use. If this is not set, the default is used. + * Sets the provider of the key manager to use. If this is not set, the default is used. */ public void setProvider(String provider) { this.provider = provider; } /** - * Sets the algorithm of the KeyManager to use. If this is not set, the default is used. + * Sets the algorithm of the {@code KeyManager} to use. If this is not set, the default is used. * * @see KeyManagerFactory#getDefaultAlgorithm() */ @@ -83,7 +83,7 @@ public class KeyManagersFactoryBean implements FactoryBean, Initia } public KeyManager[] getObject() throws Exception { - return keyManagerFactory.getKeyManagers(); + return keyManagers; } public Class getObjectType() { @@ -98,10 +98,12 @@ public class KeyManagersFactoryBean implements FactoryBean, Initia String algorithm = StringUtils.hasLength(this.algorithm) ? this.algorithm : KeyManagerFactory.getDefaultAlgorithm(); - keyManagerFactory = + KeyManagerFactory keyManagerFactory = StringUtils.hasLength(this.provider) ? KeyManagerFactory.getInstance(algorithm, this.provider) : KeyManagerFactory.getInstance(algorithm); keyManagerFactory.init(keyStore, password); + + this.keyManagers = keyManagerFactory.getKeyManagers(); } } diff --git a/spring-ws-security/src/main/java/org/springframework/ws/soap/security/support/TrustManagersFactoryBean.java b/spring-ws-security/src/main/java/org/springframework/ws/soap/security/support/TrustManagersFactoryBean.java new file mode 100644 index 00000000..2a1ca27d --- /dev/null +++ b/spring-ws-security/src/main/java/org/springframework/ws/soap/security/support/TrustManagersFactoryBean.java @@ -0,0 +1,101 @@ +/* + * Copyright 2005-2014 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.springframework.ws.soap.security.support; + +import java.security.KeyStore; +import javax.net.ssl.TrustManager; +import javax.net.ssl.TrustManagerFactory; + +import org.springframework.beans.factory.FactoryBean; +import org.springframework.beans.factory.InitializingBean; +import org.springframework.util.StringUtils; + +/** + * Spring factory bean for an array of {@link TrustManager}s. + *

+ * Uses the {@link TrustManagerFactory} to create the {@code TrustManager}s. + * + * @author Arjen Poutsma + * @see TrustManager + * @see TrustManagerFactory + * @since 2.2 + */ +public class TrustManagersFactoryBean + implements FactoryBean, InitializingBean { + + private TrustManager[] trustManagers; + + private KeyStore keyStore; + + private String algorithm; + + private String provider; + + /** + * Sets the provider of the trust manager to use. If this is not set, the default is + * used. + */ + public void setProvider(String provider) { + this.provider = provider; + } + + /** + * Sets the algorithm of the {@code TrustManager} to use. If this is not set, the + * default is used. + * @see TrustManagerFactory#getDefaultAlgorithm() + */ + public void setAlgorithm(String algorithm) { + this.algorithm = algorithm; + } + + /** + * Sets the source of certificate authorities and related trust material. + * @see TrustManagerFactory#init(KeyStore) + */ + public void setKeyStore(KeyStore keyStore) { + this.keyStore = keyStore; + } + + @Override + public TrustManager[] getObject() throws Exception { + return trustManagers; + } + + @Override + public Class getObjectType() { + return TrustManager[].class; + } + + @Override + public boolean isSingleton() { + return true; + } + + @Override + public void afterPropertiesSet() throws Exception { + String algorithm = StringUtils.hasLength(this.algorithm) ? this.algorithm : + TrustManagerFactory.getDefaultAlgorithm(); + + TrustManagerFactory trustManagerFactory = StringUtils.hasLength(this.provider) ? + TrustManagerFactory.getInstance(algorithm, this.provider) : + TrustManagerFactory.getInstance(algorithm); + + trustManagerFactory.init(keyStore); + + this.trustManagers = trustManagerFactory.getTrustManagers(); + } +} diff --git a/spring-ws-security/src/test/java/org/springframework/ws/soap/security/support/KeyManagersFactoryBeanTest.java b/spring-ws-security/src/test/java/org/springframework/ws/soap/security/support/KeyManagersFactoryBeanTest.java new file mode 100644 index 00000000..8bf0d9c2 --- /dev/null +++ b/spring-ws-security/src/test/java/org/springframework/ws/soap/security/support/KeyManagersFactoryBeanTest.java @@ -0,0 +1,48 @@ +/* + * Copyright 2005-2014 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.springframework.ws.soap.security.support; + +import javax.net.ssl.KeyManager; +import javax.net.ssl.TrustManager; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertNotNull; +import org.junit.Test; + +public class KeyManagersFactoryBeanTest { + + @Test + public void defaults() throws Exception { + KeyManagersFactoryBean factoryBean = new KeyManagersFactoryBean(); + factoryBean.afterPropertiesSet(); + KeyManager[] keyManagers = factoryBean.getObject(); + assertNotNull(keyManagers); + assertEquals(1, keyManagers.length); + } + + @Test + public void algorithm() throws Exception { + KeyManagersFactoryBean factoryBean = new KeyManagersFactoryBean(); + factoryBean.setAlgorithm("PKIX"); + factoryBean.afterPropertiesSet(); + KeyManager[] keyManagers = factoryBean.getObject(); + assertNotNull(keyManagers); + assertEquals(1, keyManagers.length); + } + +} diff --git a/spring-ws-security/src/test/java/org/springframework/ws/soap/security/support/TrustManagersFactoryBeanTest.java b/spring-ws-security/src/test/java/org/springframework/ws/soap/security/support/TrustManagersFactoryBeanTest.java new file mode 100644 index 00000000..e2570a0a --- /dev/null +++ b/spring-ws-security/src/test/java/org/springframework/ws/soap/security/support/TrustManagersFactoryBeanTest.java @@ -0,0 +1,47 @@ +/* + * Copyright 2005-2014 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.springframework.ws.soap.security.support; + +import javax.net.ssl.TrustManager; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertNotNull; +import org.junit.Test; + +public class TrustManagersFactoryBeanTest { + + @Test + public void defaults() throws Exception { + TrustManagersFactoryBean factoryBean = new TrustManagersFactoryBean(); + factoryBean.afterPropertiesSet(); + TrustManager[] trustManagers = factoryBean.getObject(); + assertNotNull(trustManagers); + assertEquals(1, trustManagers.length); + } + + @Test + public void algorithm() throws Exception { + TrustManagersFactoryBean factoryBean = new TrustManagersFactoryBean(); + factoryBean.setAlgorithm("PKIX"); + factoryBean.afterPropertiesSet(); + TrustManager[] trustManagers = factoryBean.getObject(); + assertNotNull(trustManagers); + assertEquals(1, trustManagers.length); + } + +}