Using WebServiceTemplate with a keystore

Mark Pope picture Mark Pope · Mar 10, 2010 · Viewed 19.4k times · Source

Is it possible to configure a WebServiceTemplate with a java keystore?

edit
I'm looking for a way to configure the location of the keystore in the spring config

Answer

Suken Shah picture Suken Shah · Jun 1, 2016

I am posting this answer after six years but to be honest there isn't a single post where a complete and concise solution is provided. All you need is spring-ws-core (2.1.4.RELEASE +) and spring-we-security (2.2.4.RELEASE +) dependencies. The next step is to configure custom keystore and truststore as beans and then inject them to web service template in spring configuration.

<bean id="myKeyStore" class="org.springframework.ws.soap.security.support.KeyStoreFactoryBean">
	<property name="location" value="file:/tmp/config/my-keystore.jks"/>
	<property name="password" value="password"/>
</bean>

<bean id="myTrustStore" class="org.springframework.ws.soap.security.support.KeyStoreFactoryBean">
	<property name="location" value="file:/tmp/config/my-truststore.jks"/>
	<property name="password" value="different_password"/>
</bean>

<bean id="template" class="org.springframework.ws.client.core.WebServiceTemplate">
    <property name="messageSender">
        <bean class="org.springframework.ws.transport.http.HttpsUrlConnectionMessageSender">
            <property name="trustManagers">
		<bean class="org.springframework.ws.soap.security.support.TrustManagersFactoryBean">
			<property name="keyStore" ref="mytrustStore" />
		</bean>
	    </property>
	    <property name="keyManagers">
		<bean class="org.springframework.ws.soap.security.support.KeyManagersFactoryBean">
			<property name="keyStore" ref="myKeyStore" />
			<property name="password" value="password" />
		</bean>
	   </property>
        </bean>
    </property>
</bean>

In summery there is no need to write any code, the use case can be easily achieved using spring config.