当我尝试使用安全性编写标头代码时出现以下错误:
javax.xml.soap.SOAPException:无法找到前缀:wsse的命名空间 at weblogic.xml.saaj.SOAPElementImpl.addChildElement(SOAPElementImpl.java:357) 在pr.com.prt.eppaSapInt.ws.SecurityHeader.doWithMessage(SecurityHeader.java:48)
这是我的代码:
public class SecurityHeader implements WebServiceMessageCallback{
@Override
public void doWithMessage(WebServiceMessage wsMessage) throws IOException, TransformerException {
SOAPMessage soapMessage = ((SaajSoapMessage)wsMessage).getSaajMessage();
SOAPHeader header;
SOAPHeaderElement security;
SOAPHeaderElement usertoken;
SOAPElement username;
SOAPElement password;
try {
header = soapMessage.getSOAPHeader();
//header.addNamespaceDeclaration("wsse", "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd");
//header.addNamespaceDeclaration("wsu", "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd");
security = header.addHeaderElement(new QName("http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd", "Security", "wsse"));
usertoken = header.addHeaderElement(new QName("http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd", "UsernameToken", "wsu"));
username = usertoken.addChildElement("Username", "wsse");
password = usertoken.addChildElement("Password", "wsse");
password.setAttribute("Type","http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-username-token-profile-1.0#PasswordText");
username.setTextContent("eppa2SAP_app");
password.setTextContent("eppa2SAP_app123");
security.addChildElement(username);
security.addChildElement(password);
security.addChildElement(usertoken);
JAXBContext context = JAXBContext.newInstance();
Marshaller marshaller = context.createMarshaller();
marshaller.marshal(null, ((SoapHeader) soapMessage).getResult());
} catch (JAXBException e) {
throw new IOException("error while marshalling authentication JAXB.");
} catch (MarshallingException e) {
throw new IOException("error while marshalling authentication exception.");
} catch (SOAPException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
答案 0 :(得分:1)
我认为问题是QNames
不好玩。请尝试使用header.addChildElement(localName, prefix, uri);
代替Security
和UsernameToken
。