Skip to content

Instantly share code, notes, and snippets.

@ithayer
Created March 27, 2012 19:31
Show Gist options
  • Save ithayer/2219525 to your computer and use it in GitHub Desktop.
Save ithayer/2219525 to your computer and use it in GitHub Desktop.
Simple JAX-WS UsernameToken security generation with Clojure (SOAP, AXIS, WSDL)
(defn create-security-header
"Given an optional environment (:stage, :prod), creates the authentication."
[credentials]
(let [soap-factory (SOAPFactory/newInstance)
security (.. soap-factory (createElement (QName. security-namespace "Security")))
user-token (.. soap-factory (createElement (QName. security-namespace "UsernameToken")))
username (.. soap-factory (createElement (QName. security-namespace "Username")))
password (.. soap-factory (createElement (QName. security-namespace "Password")))]
(.. username (addTextNode (get credentials :username)))
(.. password (addTextNode (get credentials :password)))
(.. user-token (addChildElement username))
(.. user-token (addChildElement password))
(.. security (addChildElement user-token))
(Headers/create security)))
then you need to call "setOutboundHeaders" on the soap service object with the returned headers. That generates this:
<S:Header>
<Security xmlns="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd">
<UsernameToken xmlns="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd">
<Username xmlns="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd">...
</Username>
<Password xmlns="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd">...
</Password>
</UsernameToken>
</Security>
</S:Header>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment