javax.xml.soap.SOAPException :unable to find namespace for prefix: wsse HEADER Security

标签 java soap wsdl wsse

当我尝试使用安全性对 header 进行编码时出现以下错误:

javax.xml.soap.SOAPException: unable to find namespace for prefix: wsse at weblogic.xml.saaj.SOAPElementImpl.addChildElement(SOAPElementImpl.java:357) at 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();
    }       
}

最佳答案

我认为问题在于QNames 表现不佳。尝试使用 header.addChildElement(localName, prefix, uri); 来代替 SecurityUsernameToken

关于javax.xml.soap.SOAPException :unable to find namespace for prefix: wsse HEADER Security,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43129565/

相关文章:

java - 在 Frege 中导入 Java 库

php - 安装 SOAP 后没有 soap.so PHP 模块文件

sockets - HTTP/SOAP 数据包大小

c# - WCF 在调用期间等待 _TransparantProxyStub_CrossContext 函数时使 CPU 最大化

java - 从 Java 调用 Objective-C 代码的最简单方法是什么?

java - Apache CXF 附件安全

java - 如何使用子类中的 Action 监听器从父类(super class)中的 JPanel 中删除所有元素?

c - 有 gSOAP 的轻量级替代品吗?

soap - xmlschema 序列中无界元素的顺序

wcf - WCF托管:可以访问svc文件,但不能转到wsdl链接