Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- private static void ONVIFAuthenticatingByWSUsernameToken(final String userName, final String password)
- {
- // A random number value generated with a client uniquely is set
- byte[] nonce = new byte[16];
- new Random().nextBytes(nonce);
- // Nonce to base64
- String nonceBase64 = Base64.encodeToString(nonce, false);
- // Get the current UTC time properly formatted.
- final Date currentTime = new Date();
- final SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'hh:mm:ss.SSSZ");
- sdf.setTimeZone(TimeZone.getTimeZone("UTC"));
- String dt = sdf.format(currentTime);
- byte[] operand = new byte[nonce.length + dt.getBytes().length + password.getBytes().length];
- System.arraycopy(nonce, 0, operand, 0, nonce.length);
- System.arraycopy(dt.getBytes(), 0, operand, nonce.length, dt.getBytes().length);
- System.arraycopy(password.getBytes(), 0, operand, nonce.length + dt.getBytes().length, password.getBytes().length);
- String hashBase64 = Base64.encodeToString(DigestUtils.sha(operand), false);
- String soapHeader =
- "<soap:Header>\n" +
- " <Security soap:mustUnderstand=\"1\" xmlns=\"http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd\">\n" +
- " <UsernameToken>\n" +
- " <Username>" + userName + "</Username>\n" +
- " <wsse:Password Type=\"http://docs.oasis-open.org/wss/2004/01/oasis-200401-wssusername-token-profile-1.0#PasswordDigest\">" + hashBase64 + "</wsse:Password>\n" +
- " <wsse:Nonce >" + nonceBase64 + "</wsse:Nonce>\n" +
- " <Created>" + dt + "</Created>\n" +
- " </UsernameToken>\n" +
- " </Security>\n" +
- "</soap:Header>";
- System.out.println(soapHeader);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement