|
16 | 16 |
|
17 | 17 | package org.springframework.ws.soap.security.wss4j.support;
|
18 | 18 |
|
| 19 | +import java.io.File; |
| 20 | +import java.io.IOException; |
19 | 21 | import java.util.Properties;
|
20 | 22 |
|
21 | 23 | import org.apache.ws.security.components.crypto.Crypto;
|
22 | 24 | import org.apache.ws.security.components.crypto.CryptoFactory;
|
| 25 | +import org.apache.ws.security.components.crypto.Merlin; |
23 | 26 |
|
24 | 27 | import org.springframework.beans.factory.BeanClassLoaderAware;
|
25 | 28 | import org.springframework.beans.factory.FactoryBean;
|
26 | 29 | import org.springframework.beans.factory.InitializingBean;
|
| 30 | +import org.springframework.core.io.Resource; |
27 | 31 | import org.springframework.util.Assert;
|
28 | 32 |
|
29 | 33 | /**
|
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}. |
31 | 36 | * <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. |
34 | 39 | *
|
35 | 40 | * @author Tareq Abed Rabbo
|
36 | 41 | * @author Arjen Poutsma
|
|
39 | 44 | */
|
40 | 45 | public class CryptoFactoryBean implements FactoryBean, BeanClassLoaderAware, InitializingBean {
|
41 | 46 |
|
42 |
| - private Properties configuration; |
| 47 | + private Properties configuration = new Properties(); |
43 | 48 |
|
44 | 49 | private ClassLoader classLoader;
|
45 | 50 |
|
46 | 51 | private Crypto crypto;
|
47 | 52 |
|
| 53 | + private static final String CRYPTO_PROVIDER_PROPERTY = "org.apache.ws.security.crypto.provider"; |
| 54 | + |
48 | 55 | /**
|
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 |
50 | 58 | *
|
51 | 59 | * @see org.apache.ws.security.components.crypto.CryptoFactory#getInstance(java.util.Properties)
|
52 | 60 | */
|
53 | 61 | 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); |
55 | 150 | }
|
56 | 151 |
|
57 | 152 | public void setBeanClassLoader(ClassLoader classLoader) {
|
58 | 153 | this.classLoader = classLoader;
|
59 | 154 | }
|
60 | 155 |
|
61 | 156 | 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 | + } |
64 | 160 | this.crypto = CryptoFactory.getInstance(configuration, classLoader);
|
65 | 161 | }
|
66 | 162 |
|
|
0 commit comments