Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Introduced TrustManagersFactoryBean for easy configuration of TrustManager instances in Spring XML. Issue: SWS-731
- Loading branch information
Arjen Poutsma
committed
Mar 19, 2014
1 parent
0c8850b
commit 9f4e293
Showing
4 changed files
with
203 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
101 changes: 101 additions & 0 deletions
101
.../src/main/java/org/springframework/ws/soap/security/support/TrustManagersFactoryBean.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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(); | ||
} | ||
} |
48 changes: 48 additions & 0 deletions
48
...rc/test/java/org/springframework/ws/soap/security/support/KeyManagersFactoryBeanTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); | ||
} | ||
|
||
} |
47 changes: 47 additions & 0 deletions
47
.../test/java/org/springframework/ws/soap/security/support/TrustManagersFactoryBeanTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); | ||
} | ||
|
||
} |