Document doc = SOAPUtil.toSOAPPart(SOAPUtil.SAMPLE_SOAP_MSG);
WSSecHeader secHeader = new WSSecHeader();
secHeader.insertSecurityHeader(doc);
KerberosSecurity bst = new KerberosSecurity(doc);
CallbackHandler callbackHandler = new CallbackHandler() {
@Override
public void handle(Callback[] callbacks) throws IOException, UnsupportedCallbackException {
if (callbacks[0] instanceof PasswordCallback) {
PasswordCallback passwordCallback = (PasswordCallback)callbacks[0];
if (passwordCallback.getPrompt().contains("alice")) {
passwordCallback.setPassword("alice".toCharArray());
} else if (passwordCallback.getPrompt().contains("bob")) {
passwordCallback.setPassword("bob".toCharArray());
}
}
}
};
bst.retrieveServiceTicket("alice", callbackHandler, "bob@service.ws.apache.org");
bst.setID("Id-" + bst.hashCode());
WSSecEncrypt builder = new WSSecEncrypt();
builder.setSymmetricEncAlgorithm(WSConstants.AES_128);
SecretKey secretKey = bst.getSecretKey();
builder.setSymmetricKey(secretKey);
builder.setEncryptSymmKey(false);
builder.setCustomReferenceValue(WSConstants.WSS_KRB_KI_VALUE_TYPE);
byte[] digestBytes = WSSecurityUtil.generateDigest(bst.getToken());
builder.setEncKeyId(Base64.encode(digestBytes));
Document encryptedDoc = builder.build(doc, null, secHeader);
WSSecurityUtil.prependChildElement(secHeader.getSecurityHeader(), bst.getElement());
if (LOG.isDebugEnabled()) {
String outputString =
XMLUtils.PrettyDocumentToString(encryptedDoc);
LOG.debug(outputString);