Note that there are some explanatory texts on larger screens.

plurals
  1. POJava Webservice Client UsernameToken equivalent to PHP
    primarykey
    data
    text
    <p>I have a webservice build in PHP that uses UsernameToken as authentification mechanism. I have PHP client side code that can access this web service. Now I need to do this in Java. Maybe you can help me!</p> <p>This service can be accessed using the following php code:</p> <pre><code>$password="super_secure_pass"; $timestamp=gmdate('Y-m-d\TH:i:s\Z'); $nonce=mt_rand(); $passdigest=base64_encode(pack('H*',sha1(pack('H*',$nonce).pack('a*',$timestamp).pack('a*',$password)))); $nonce=base64_encode(pack('H*',$nonce)) </code></pre> <p>These values get parsed into this Soap header.</p> <pre><code>&lt;wsse:Security SOAP-ENV:mustUnderstand="0" xmlns:wsse="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd"&gt; &lt;wsse:UsernameToken&gt; &lt;wsse:Username&gt;'.$username.'&lt;/wsse:Username&gt; &lt;wsse:Password Type="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-username-token-profile-1.0#PasswordDigest"&gt;'.$passdigest.'&lt;/wsse:Password&gt; &lt;wsse:Nonce&gt;'.$nonce.'&lt;/wsse:Nonce&gt; &lt;wsu:Created xmlns:wsu="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd"&gt;'.$timestamp.'&lt;/wsu:Created&gt; &lt;/wsse:UsernameToken&gt; &lt;/wsse:Security&gt; </code></pre> <p>With this code I can access the web service without any problems. Now I need to do this in Java. </p> <p>I have created the neccesary files, implemented a handler to add a soap header with the UsernameToken. But when I try to access the WS I always get a "Not Authorized" error. I think that I'm missing something while creating the passdigest or nonce entry.</p> <p>Here is how I calculate them:</p> <pre><code> Random generator = new Random(); String nonceString = String.valueOf(generator.nextInt(999999999)); String createTime=localToGmtTimestamp();//Returns a date with format (SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'")) String pass="super_secure_pass"; String tmp = AeSimpleSHA1.SHA1(nonce + createTime + pass); encodedPass = Base64.encodeBytes(tmp.getBytes()); </code></pre> <p>These values will be used while creating the soap header:</p> <pre><code> SOAPEnvelope envelope = smc.getMessage().getSOAPPart().getEnvelope(); SOAPHeader header = envelope.addHeader(); SOAPElement security = header.addChildElement("Security", "wsse", "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd"); SOAPElement usernameToken = security.addChildElement("UsernameToken", "wsse"); SOAPElement username = usernameToken.addChildElement("Username", "wsse"); username.addTextNode(user); SOAPElement password = usernameToken.addChildElement("Password", "wsse"); password.setAttribute("Type", "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-username-token-profile-1.0#PasswordDigest"); password.addTextNode(encodedPass); SOAPElement nonce = usernameToken.addChildElement("Nonce", "wsse"); nonce.addTextNode(Base64.encodeBytes(nonceString.getBytes())); SOAPElement created = usernameToken.addChildElement("Created", "wsu","http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd"); created.addTextNode(creatTime); </code></pre> <p>This is what the resulting soap header looks like:</p> <pre><code> &lt;S:Header&gt; &lt;wsse:Security xmlns:wsse="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd"&gt; &lt;wsse:UsernameToken&gt; &lt;wsse:Username&gt;myusername&lt;/wsse:Username&gt; &lt;wsse:Password Type="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-username-token-profile-1.0#PasswordDigest"&gt;ZDM4MjkwNzNlNTc3MjNmMTY4MjgyYWQ1ZjllN2JlZmJmNGY2NDE4MA==&lt;/wsse:Password&gt; &lt;wsse:Nonce&gt;NTU5NzA2Mjkw&lt;/wsse:Nonce&gt; &lt;wsu:Created xmlns:wsu="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd"&gt;2011-07-24T11:41:55Z&lt;/wsu:Created&gt; &lt;/wsse:UsernameToken&gt; &lt;/wsse:Security&gt; &lt;/S:Header&gt; </code></pre> <p>Does anybody see what I'm doing wrong ? </p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload