Skip to content

Commit 671683b

Browse files
committed
Working on SWS-207
1 parent 53fea29 commit 671683b

File tree

4 files changed

+125
-20
lines changed

4 files changed

+125
-20
lines changed

security/src/main/java/org/springframework/ws/soap/security/wss4j/support/CryptoFactoryBean.java

Lines changed: 104 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -16,21 +16,26 @@
1616

1717
package org.springframework.ws.soap.security.wss4j.support;
1818

19+
import java.io.File;
20+
import java.io.IOException;
1921
import java.util.Properties;
2022

2123
import org.apache.ws.security.components.crypto.Crypto;
2224
import org.apache.ws.security.components.crypto.CryptoFactory;
25+
import org.apache.ws.security.components.crypto.Merlin;
2326

2427
import org.springframework.beans.factory.BeanClassLoaderAware;
2528
import org.springframework.beans.factory.FactoryBean;
2629
import org.springframework.beans.factory.InitializingBean;
30+
import org.springframework.core.io.Resource;
2731
import org.springframework.util.Assert;
2832

2933
/**
30-
* Spring factory bean for a WSS4J {@link Crypto}.
34+
* Spring factory bean for a WSS4J {@link Crypto}. Allows for strong-typed property configuration, or configuration
35+
* through {@link Properties}.
3136
* <p/>
32-
* Requires the {@link #setConfiguration(java.util.Properties) configuration} property to be set. This configuration
33-
* should have the <code>org.apache.ws.security.crypto.provider</code> property defined.
37+
* Requires either individual properties, or the {@link #setConfiguration(java.util.Properties) configuration} property
38+
* to be set.
3439
*
3540
* @author Tareq Abed Rabbo
3641
* @author Arjen Poutsma
@@ -39,28 +44,119 @@
3944
*/
4045
public class CryptoFactoryBean implements FactoryBean, BeanClassLoaderAware, InitializingBean {
4146

42-
private Properties configuration;
47+
private Properties configuration = new Properties();
4348

4449
private ClassLoader classLoader;
4550

4651
private Crypto crypto;
4752

53+
private static final String CRYPTO_PROVIDER_PROPERTY = "org.apache.ws.security.crypto.provider";
54+
4855
/**
49-
* Sets the configuration of the Crypto.
56+
* Sets the configuration of the Crypto. Setting this property overrides all previously set configuration, through
57+
* the type-safe properties
5058
*
5159
* @see org.apache.ws.security.components.crypto.CryptoFactory#getInstance(java.util.Properties)
5260
*/
5361
public void setConfiguration(Properties properties) {
54-
this.configuration = properties;
62+
Assert.notNull(properties, "'properties' must not be null");
63+
this.configuration.putAll(properties);
64+
}
65+
66+
/**
67+
* Sets the {@link org.apache.ws.security.components.crypto.Crypto} provider name. Defaults to {@link
68+
* org.apache.ws.security.components.crypto.Merlin}.
69+
* <p/>
70+
* This property maps to the WSS4J <code>org.apache.ws.security.crypto.provider</code> property.
71+
*
72+
* @param cryptoProviderClass the crypto provider class
73+
*/
74+
public void setCryptoProvider(Class cryptoProviderClass) {
75+
this.configuration.setProperty(CRYPTO_PROVIDER_PROPERTY, cryptoProviderClass.getName());
76+
}
77+
78+
/**
79+
* Sets the location of the key store to be loaded in the {@link org.apache.ws.security.components.crypto.Crypto}
80+
* instance.
81+
* <p/>
82+
* This property maps to the WSS4J <code>org.apache.ws.security.crypto.merlin.file</code> property.
83+
*
84+
* @param location the key store location
85+
* @throws java.io.IOException when the resource cannot be openened
86+
*/
87+
public void setKeyStoreLocation(Resource location) throws IOException {
88+
File keystoreFile = location.getFile();
89+
this.configuration.setProperty("org.apache.ws.security.crypto.merlin.file", keystoreFile.getAbsolutePath());
90+
}
91+
92+
/**
93+
* Sets the key store provider.
94+
* <p/>
95+
* This property maps to the WSS4J <code>org.apache.ws.security.crypto.merlin.keystore.provider</code> property.
96+
*
97+
* @param provider the key store provider
98+
*/
99+
public void setKeyStoreProvider(String provider) {
100+
this.configuration.setProperty("org.apache.ws.security.crypto.merlin.keystore.provider", provider);
101+
}
102+
103+
/**
104+
* Sets the key store password. Defaults to <code>security</code>.
105+
* <p/>
106+
* This property maps to the WSS4J <code>org.apache.ws.security.crypto.merlin.keystore.password</code> property.
107+
*
108+
* @param password the key store password
109+
*/
110+
public void setKeyStorePassword(String password) {
111+
this.configuration.setProperty("org.apache.ws.security.crypto.merlin.keystore.password", password);
112+
}
113+
114+
/**
115+
* Sets the key store type. Defaults to {@link java.security.KeyStore#getDefaultType()}.
116+
* <p/>
117+
* This property maps to the WSS4J <code>org.apache.ws.security.crypto.merlin.keystore.type</code> property.
118+
*
119+
* @param type the key store type
120+
*/
121+
public void setKeyStoreType(String type) {
122+
this.configuration.setProperty("org.apache.ws.security.crypto.merlin.keystore.type", type);
123+
}
124+
125+
/**
126+
* Sets the trust store password. Defaults to <code>changeit</code>.
127+
* <p/>
128+
* WSS4J crypto uses the standard J2SE trust store, i.e. <code>$JAVA_HOME/lib/security/cacerts</code>.
129+
* <p/>
130+
* <p/>
131+
* This property maps to the WSS4J <code>org.apache.ws.security.crypto.merlin.cacerts.password</code> property.
132+
*
133+
* @param password the trust store password
134+
*/
135+
public void setTrustStorePassword(String password) {
136+
this.configuration.setProperty("org.apache.ws.security.crypto.merlin.cacerts.password", password);
137+
}
138+
139+
/**
140+
* Sets the alias name of the default certificate which has been specified as a property. This should be the
141+
* certificate that is used for signature and encryption. This alias corresponds to the certificate that should be
142+
* used whenever KeyInfo is not present in a signed or an encrypted message.
143+
* <p/>
144+
* This property maps to the WSS4J <code>org.apache.ws.security.crypto.merlin.keystore.alias</code> property.
145+
*
146+
* @param defaultX509Alias alias name of the default X509 certificate
147+
*/
148+
public void setDefaultX509Alias(String defaultX509Alias) {
149+
this.configuration.setProperty("org.apache.ws.security.crypto.merlin.keystore.alias", defaultX509Alias);
55150
}
56151

57152
public void setBeanClassLoader(ClassLoader classLoader) {
58153
this.classLoader = classLoader;
59154
}
60155

61156
public void afterPropertiesSet() throws Exception {
62-
Assert.notNull(configuration, "'configuration' is required");
63-
157+
if (!configuration.containsKey(CRYPTO_PROVIDER_PROPERTY)) {
158+
configuration.setProperty(CRYPTO_PROVIDER_PROPERTY, Merlin.class.getName());
159+
}
64160
this.crypto = CryptoFactory.getInstance(configuration, classLoader);
65161
}
66162

security/src/test/java/org/springframework/ws/soap/security/wss4j/support/CryptoFactoryBeanTest.java

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,9 @@
1919
import java.util.Properties;
2020

2121
import junit.framework.TestCase;
22-
import org.apache.ws.security.components.crypto.Crypto;
22+
import org.apache.ws.security.components.crypto.Merlin;
2323

2424
import org.springframework.core.io.ClassPathResource;
25-
import org.springframework.core.io.support.PropertiesLoaderUtils;
2625
import org.springframework.util.ClassUtils;
2726

2827
public class CryptoFactoryBeanTest extends TestCase {
@@ -33,15 +32,31 @@ protected void setUp() throws Exception {
3332
factoryBean = new CryptoFactoryBean();
3433
}
3534

36-
public void testMerlin() throws Exception {
37-
Properties configuration =
38-
PropertiesLoaderUtils.loadProperties(new ClassPathResource("merlin.properties", getClass()));
35+
public void testSetConfiguration() throws Exception {
36+
Properties configuration = new Properties();
37+
configuration.setProperty("org.apache.ws.security.crypto.provider",
38+
"org.apache.ws.security.components.crypto.Merlin");
39+
configuration.setProperty("org.apache.ws.security.crypto.merlin.keystore.type", "jceks");
40+
configuration.setProperty("org.apache.ws.security.crypto.merlin.keystore.password", "123456");
41+
configuration.setProperty("org.apache.ws.security.crypto.merlin.file", "private.jks");
42+
3943
factoryBean.setConfiguration(configuration);
4044
factoryBean.setBeanClassLoader(ClassUtils.getDefaultClassLoader());
4145
factoryBean.afterPropertiesSet();
4246

4347
Object result = factoryBean.getObject();
4448
assertNotNull("No result", result);
45-
assertTrue("Not a crypto instance", result instanceof Crypto);
49+
assertTrue("Not a Merlin instance", result instanceof Merlin);
50+
}
51+
52+
public void testProperties() throws Exception {
53+
factoryBean.setKeyStoreType("jceks");
54+
factoryBean.setKeyStorePassword("123456");
55+
factoryBean.setKeyStoreLocation(new ClassPathResource("private.jks"));
56+
factoryBean.setBeanClassLoader(ClassUtils.getDefaultClassLoader());
57+
factoryBean.afterPropertiesSet();
58+
Object result = factoryBean.getObject();
59+
assertNotNull("No result", result);
60+
assertTrue("Not a Merlin instance", result instanceof Merlin);
4661
}
4762
}

security/src/test/resources/org/springframework/ws/soap/security/wss4j/support/merlin.properties

Lines changed: 0 additions & 6 deletions
This file was deleted.

0 commit comments

Comments
 (0)