Skip to content

Commit 9f4e293

Browse files
author
Arjen Poutsma
committed
Introduce TrustManagersFactoryBean
Introduced TrustManagersFactoryBean for easy configuration of TrustManager instances in Spring XML. Issue: SWS-731
1 parent 0c8850b commit 9f4e293

File tree

4 files changed

+203
-5
lines changed

4 files changed

+203
-5
lines changed

spring-ws-security/src/main/java/org/springframework/ws/soap/security/support/KeyManagersFactoryBean.java

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@
3737
*/
3838
public class KeyManagersFactoryBean implements FactoryBean<KeyManager[]>, InitializingBean {
3939

40-
private KeyManagerFactory keyManagerFactory;
40+
private KeyManager[] keyManagers;
4141

4242
private KeyStore keyStore;
4343

@@ -58,14 +58,14 @@ public void setPassword(String password) {
5858
}
5959

6060
/**
61-
* Sets the provider of the key store to use. If this is not set, the default is used.
61+
* Sets the provider of the key manager to use. If this is not set, the default is used.
6262
*/
6363
public void setProvider(String provider) {
6464
this.provider = provider;
6565
}
6666

6767
/**
68-
* Sets the algorithm of the <code>KeyManager</code> to use. If this is not set, the default is used.
68+
* Sets the algorithm of the {@code KeyManager} to use. If this is not set, the default is used.
6969
*
7070
* @see KeyManagerFactory#getDefaultAlgorithm()
7171
*/
@@ -83,7 +83,7 @@ public void setKeyStore(KeyStore keyStore) {
8383
}
8484

8585
public KeyManager[] getObject() throws Exception {
86-
return keyManagerFactory.getKeyManagers();
86+
return keyManagers;
8787
}
8888

8989
public Class<?> getObjectType() {
@@ -98,10 +98,12 @@ public void afterPropertiesSet() throws Exception {
9898
String algorithm =
9999
StringUtils.hasLength(this.algorithm) ? this.algorithm : KeyManagerFactory.getDefaultAlgorithm();
100100

101-
keyManagerFactory =
101+
KeyManagerFactory keyManagerFactory =
102102
StringUtils.hasLength(this.provider) ? KeyManagerFactory.getInstance(algorithm, this.provider) :
103103
KeyManagerFactory.getInstance(algorithm);
104104

105105
keyManagerFactory.init(keyStore, password);
106+
107+
this.keyManagers = keyManagerFactory.getKeyManagers();
106108
}
107109
}
Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
/*
2+
* Copyright 2005-2014 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.ws.soap.security.support;
18+
19+
import java.security.KeyStore;
20+
import javax.net.ssl.TrustManager;
21+
import javax.net.ssl.TrustManagerFactory;
22+
23+
import org.springframework.beans.factory.FactoryBean;
24+
import org.springframework.beans.factory.InitializingBean;
25+
import org.springframework.util.StringUtils;
26+
27+
/**
28+
* Spring factory bean for an array of {@link TrustManager}s.
29+
* <p/>
30+
* Uses the {@link TrustManagerFactory} to create the {@code TrustManager}s.
31+
*
32+
* @author Arjen Poutsma
33+
* @see TrustManager
34+
* @see TrustManagerFactory
35+
* @since 2.2
36+
*/
37+
public class TrustManagersFactoryBean
38+
implements FactoryBean<TrustManager[]>, InitializingBean {
39+
40+
private TrustManager[] trustManagers;
41+
42+
private KeyStore keyStore;
43+
44+
private String algorithm;
45+
46+
private String provider;
47+
48+
/**
49+
* Sets the provider of the trust manager to use. If this is not set, the default is
50+
* used.
51+
*/
52+
public void setProvider(String provider) {
53+
this.provider = provider;
54+
}
55+
56+
/**
57+
* Sets the algorithm of the {@code TrustManager} to use. If this is not set, the
58+
* default is used.
59+
* @see TrustManagerFactory#getDefaultAlgorithm()
60+
*/
61+
public void setAlgorithm(String algorithm) {
62+
this.algorithm = algorithm;
63+
}
64+
65+
/**
66+
* Sets the source of certificate authorities and related trust material.
67+
* @see TrustManagerFactory#init(KeyStore)
68+
*/
69+
public void setKeyStore(KeyStore keyStore) {
70+
this.keyStore = keyStore;
71+
}
72+
73+
@Override
74+
public TrustManager[] getObject() throws Exception {
75+
return trustManagers;
76+
}
77+
78+
@Override
79+
public Class<?> getObjectType() {
80+
return TrustManager[].class;
81+
}
82+
83+
@Override
84+
public boolean isSingleton() {
85+
return true;
86+
}
87+
88+
@Override
89+
public void afterPropertiesSet() throws Exception {
90+
String algorithm = StringUtils.hasLength(this.algorithm) ? this.algorithm :
91+
TrustManagerFactory.getDefaultAlgorithm();
92+
93+
TrustManagerFactory trustManagerFactory = StringUtils.hasLength(this.provider) ?
94+
TrustManagerFactory.getInstance(algorithm, this.provider) :
95+
TrustManagerFactory.getInstance(algorithm);
96+
97+
trustManagerFactory.init(keyStore);
98+
99+
this.trustManagers = trustManagerFactory.getTrustManagers();
100+
}
101+
}
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
/*
2+
* Copyright 2005-2014 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.ws.soap.security.support;
18+
19+
import javax.net.ssl.KeyManager;
20+
import javax.net.ssl.TrustManager;
21+
22+
import static org.junit.Assert.assertEquals;
23+
import static org.junit.Assert.assertFalse;
24+
import static org.junit.Assert.assertNotNull;
25+
import org.junit.Test;
26+
27+
public class KeyManagersFactoryBeanTest {
28+
29+
@Test
30+
public void defaults() throws Exception {
31+
KeyManagersFactoryBean factoryBean = new KeyManagersFactoryBean();
32+
factoryBean.afterPropertiesSet();
33+
KeyManager[] keyManagers = factoryBean.getObject();
34+
assertNotNull(keyManagers);
35+
assertEquals(1, keyManagers.length);
36+
}
37+
38+
@Test
39+
public void algorithm() throws Exception {
40+
KeyManagersFactoryBean factoryBean = new KeyManagersFactoryBean();
41+
factoryBean.setAlgorithm("PKIX");
42+
factoryBean.afterPropertiesSet();
43+
KeyManager[] keyManagers = factoryBean.getObject();
44+
assertNotNull(keyManagers);
45+
assertEquals(1, keyManagers.length);
46+
}
47+
48+
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
/*
2+
* Copyright 2005-2014 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.ws.soap.security.support;
18+
19+
import javax.net.ssl.TrustManager;
20+
21+
import static org.junit.Assert.assertEquals;
22+
import static org.junit.Assert.assertFalse;
23+
import static org.junit.Assert.assertNotNull;
24+
import org.junit.Test;
25+
26+
public class TrustManagersFactoryBeanTest {
27+
28+
@Test
29+
public void defaults() throws Exception {
30+
TrustManagersFactoryBean factoryBean = new TrustManagersFactoryBean();
31+
factoryBean.afterPropertiesSet();
32+
TrustManager[] trustManagers = factoryBean.getObject();
33+
assertNotNull(trustManagers);
34+
assertEquals(1, trustManagers.length);
35+
}
36+
37+
@Test
38+
public void algorithm() throws Exception {
39+
TrustManagersFactoryBean factoryBean = new TrustManagersFactoryBean();
40+
factoryBean.setAlgorithm("PKIX");
41+
factoryBean.afterPropertiesSet();
42+
TrustManager[] trustManagers = factoryBean.getObject();
43+
assertNotNull(trustManagers);
44+
assertEquals(1, trustManagers.length);
45+
}
46+
47+
}

0 commit comments

Comments
 (0)