搜索热词
我试图以下面的格式在Java Axis2客户端中生成安全头.
以下是我目前的代码:
SOAPHeaderElement wsseSecurity = new SOAPHeaderElement(
new PrefixedQName("http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd","Security","wsse"));
wsseSecurity.setMustUnderstand(true);
wsseSecurity.setActor(null);
SOAPElement sub =wsseSecurity.addChildElement("UsernameToken");
sub.setAttribute("wsu:Id","UsernameToken-2");
SOAPElement userElement = sub.addChildElement("Username");
userElement.addTextNode("xxx");
SOAPElement pwdElement = sub.addChildElement("Password");
pwdElement.setAttribute("Type","wsse:PasswordText");
pwdElement.addTextNode("xxx");
stub.setHeader(wsseSecurity);
最佳答案
这可能会帮到你.看一看:
private static final String cNODE_USRTOKEN = "UsernameToken";
private static final String cNODE_USERNAME = "Username";
private static final String cNODE_PASSWORD = "Password";
private String iUsername;
private String iPassword;
private static final Logger cTRACE = Logger.getLogger(UsernameTokenHandler.class.getName());
public Element getToken()
{
Element assertion = null;
SOAPElement soapElement = null;
try
{
SOAPMessage soapMessage = MessageFactory.newInstance().createMessage();
SOAPPart sOAPPart = soapMessage.getSOAPPart();
SOAPEnvelope envelope = sOAPPart.getEnvelope();
SOAPHeader header = envelope.getHeader();
if (header==null)
{
// no header yet,create one
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");
Name tUserTokenElementName = envelope.createName(cNODE_USRTOKEN,"http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd");
SOAPElement tUserTokenElement = security.addChildElement(tUserTokenElementName);
tUserTokenElement.removeNamespaceDeclaration("wsse");
tUserTokenElement.addNamespaceDeclaration("wsu","http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd");
// user name child
Name tUsernameElementName = envelope.createName(cNODE_USERNAME,"http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd");
SOAPElement tUsernameElement = tUserTokenElement.addChildElement(tUsernameElementName);
tUsernameElement.removeNamespaceDeclaration("wsse");
tUsernameElement.addTextNode(iUsername);
// password child
Name tPasswordElementName = envelope.createName(cNODE_PASSWORD,"http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd");
SOAPElement tPasswordElement = tUserTokenElement.addChildElement(tPasswordElementName);
tPasswordElement.removeNamespaceDeclaration("wsse");
tPasswordElement.addTextNode(iPassword);
tPasswordElement.setAttribute("Type","http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-username-token-profile-1.0#PasswordText");
assertion = (Element) security;
//printAssertion(assertion);
//MessageElement samlElement = new MessageElement(assertion);
//SAMLHandler.setAssertion((SOAPElement) samlElement);
//soapElement = (SOAPElement) samlElement;
}
catch (Exception e)
{
if (cTRACE.isLoggable(Level.SEVERE))
{
cTRACE.logp(Level.SEVERE,UsernameTokenHandler.class.getName(),"handleMessage","Unable to add WSS credentials",e);
}
// stop processing
return assertion;
}
// continue processing
return assertion;
}
相关文章
总结
以上是编程之家为你收集整理的java – 添加安全标头全部内容,希望文章能够帮你解决java – 添加安全标头所遇到的程序开发问题。
如果觉得编程之家网站内容还不错,欢迎将编程之家网站推荐给程序员好友。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您喜欢交流学习经验,点击链接加入交流1群:1065694478(已满)交流2群:163560250