Introduce TrustManagersFactoryBean
Introduced TrustManagersFactoryBean for easy configuration of TrustManager instances in Spring XML. Issue: SWS-731
This commit is contained in:
@@ -37,7 +37,7 @@ import org.springframework.util.StringUtils;
|
||||
*/
|
||||
public class KeyManagersFactoryBean implements FactoryBean<KeyManager[]>, InitializingBean {
|
||||
|
||||
private KeyManagerFactory keyManagerFactory;
|
||||
private KeyManager[] keyManagers;
|
||||
|
||||
private KeyStore keyStore;
|
||||
|
||||
@@ -58,14 +58,14 @@ public class KeyManagersFactoryBean implements FactoryBean<KeyManager[]>, 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 <code>KeyManager</code> 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<KeyManager[]>, Initia
|
||||
}
|
||||
|
||||
public KeyManager[] getObject() throws Exception {
|
||||
return keyManagerFactory.getKeyManagers();
|
||||
return keyManagers;
|
||||
}
|
||||
|
||||
public Class<?> getObjectType() {
|
||||
@@ -98,10 +98,12 @@ public class KeyManagersFactoryBean implements FactoryBean<KeyManager[]>, 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();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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.
|
||||
* <p/>
|
||||
* 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<TrustManager[]>, 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();
|
||||
}
|
||||
}
|
||||
@@ -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);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -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);
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user