在Groovy中创建WS-security标头

时间:2019-03-14 17:06:53

标签: web-services soap groovy

我必须使用Groovy在SOAP标头中创建UsernameToken部分,但是我找不到如何做到这一点。我看了WSLite,但是我怎么理解这个库不能与WSS头一起使用。我需要这样的东西

<wsse:UsernameToken wsu:Id="UsernameToken-1">
            <wsse:Username>testuser</wsse:Username>
            <wsse:Password Type="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-username-token-profile-1.0#PasswordText">******</wsse:Password>
            <wsse:Nonce EncodingType="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-soap-message-security-1.0#Base64Binary">/KRqhWHKnB66WVTgZ4GBVg==</wsse:Nonce>
            <wsu:Created>2019-02-25T12:50:36.307Z</wsu:Created>
        </wsse:UsernameToken>
    </wsse:Security>
</soapenv:Header>

1 个答案:

答案 0 :(得分:0)

我决定使用spring ws模板

compile "org.springframework.ws:spring-ws-security"    

对我有帮助

def WebServiceTemplate createWsTemplate(String url, String login, String password) {

Wss4jSecurityInterceptor wss4jSecurityInterceptor = new Wss4jSecurityInterceptor()
wss4jSecurityInterceptor.setSecurementActions(WSHandlerConstants.USERNAME_TOKEN)
wss4jSecurityInterceptor.setSecurementUsername(login)
wss4jSecurityInterceptor.setSecurementPassword(password)
wss4jSecurityInterceptor.setSecurementPasswordType("PasswordText")
wss4jSecurityInterceptor.setSecurementUsernameTokenCreated(true)
wss4jSecurityInterceptor.setSecurementUsernameTokenNonce(true)

SaajSoapMessageFactory msgFactory = new SaajSoapMessageFactory(MessageFactory.newInstance())
msgFactory.afterPropertiesSet()
WebServiceTemplate wsTemplate = new WebServiceTemplate(msgFactory)
wsTemplate.setInterceptors(wss4jSecurityInterceptor)
wsTemplate.setDefaultUri(url)
wsTemplate.afterPropertiesSet()
wsTemplate.setMessageSender(sender)

return wsTemplate }