简体   繁体   中英

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

I have the following error when I tried to code the header using security:

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)

This is my code:

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();
    }       
}

I think the issue is that QNames don't play nice. Try using header.addChildElement(localName, prefix, uri); for Security and UsernameToken instead.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM