Created
August 24, 2020 15:45
-
-
Save brunopk/e530f45116a22545ff97b25c19c308fe to your computer and use it in GitHub Desktop.
WS Security for SOAP WS client with Spring Boot
This file contains hidden or 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
package com.bruno.ws; | |
import org.apache.wss4j.dom.WSConstants; | |
import org.apache.wss4j.dom.handler.WSHandlerConstants; | |
import org.springframework.beans.factory.annotation.Autowired; | |
import org.springframework.beans.factory.annotation.Qualifier; | |
import org.springframework.beans.factory.annotation.Value; | |
import org.springframework.context.annotation.Bean; | |
import org.springframework.context.annotation.Configuration; | |
import org.springframework.context.annotation.PropertySource; | |
import org.springframework.core.env.Environment; | |
import org.springframework.oxm.jaxb.Jaxb2Marshaller; | |
import org.springframework.ws.client.core.WebServiceTemplate; | |
import org.springframework.ws.client.support.interceptor.ClientInterceptor; | |
import org.springframework.ws.soap.security.wss4j2.Wss4jSecurityInterceptor; | |
import org.springframework.ws.transport.http.HttpUrlConnectionMessageSender; | |
/** | |
* @author bruno.piaggio | |
*/ | |
@Configuration | |
@PropertySource("classpath:application.properties") | |
public class WsClientConfig extends HttpUrlConnectionMessageSender { | |
@Autowired | |
private Environment env; | |
@Bean | |
public Wss4jSecurityInterceptor securityInterceptor() { | |
Wss4jSecurityInterceptor security = new Wss4jSecurityInterceptor(); | |
String Username = env.getProperty("ws.wss.username"); | |
String authPassword = env.getProperty("ws.wss.password"); | |
// Adds "Timestamp" and "UsernameToken" sections in SOAP header (esto no se si está bien) | |
security.setSecurementActions(WSHandlerConstants.TIMESTAMP + " " + WSHandlerConstants.USERNAME_TOKEN); | |
// Set values for "UsernameToken" sections in SOAP header | |
security.setSecurementPasswordType(WSConstants.PW_TEXT); | |
security.setSecurementUsername(authUsername); | |
security.setSecurementPassword(authPassword); | |
return security; | |
} | |
@Bean | |
public Jaxb2Marshaller marshaller(){ | |
Jaxb2Marshaller jaxb2Marshaller = new Jaxb2Marshaller(); | |
jaxb2Marshaller.setContextPath("com.external.ws"); | |
return jaxb2Marshaller; | |
} | |
@Bean | |
public WsClient wsClient(@Qualifier("marshaller") Jaxb2Marshaller jaxb2Marshaller) { | |
String defaultUri = env.getProperty("ws.uri"); | |
WsClient client = new WsClient(); | |
WebServiceTemplate template = client.getWebServiceTemplate(); | |
template.setMessageSender(new WsFuncClientConfig()); | |
template.setInterceptors(new ClientInterceptor[] {securityInterceptor()}); | |
client.setDefaultUri(defaultUri); | |
client.setMarshaller(jaxb2Marshaller); | |
client.setUnmarshaller(jaxb2Marshaller); | |
return client; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment