简体   繁体   English

在已经生成的SOAP信封的安全标头中插入Username令牌给我两个标头!

[英]Inserting Username token in security header of already generated SOAP envelope gives me two headers!

I'm using WSS4J to add a Username token in the header of an already formed SOAP request envelope. 我正在使用WSS4J在已形成的SOAP请求信封的标头中添加用户名令牌。

Here is what the SOAP request looks like: SOAP请求如下所示:

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsd="http://sample03.samples.rampart.apache.org/xsd">   
   <soapenv:Header/>   
   <soapenv:Body>      
      <xsd:echo>         
         <xsd:param0>hurro kitty</xsd:param0>      
      </xsd:echo>   
   </soapenv:Body></soapenv:Envelope>

This is my code (the String, request, is the request above): 这是我的代码(字符串,请求是上面的请求):

DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
InputSource inStream = new InputSource();
inStream.setCharacterStream(new StringReader(request));
Document document = builder.parse(inStream);

WSSecUsernameToken usernametoken = new WSSecUsernameToken();
usernametoken.setPasswordType(WSConstants.PASSWORD_TEXT);
usernametoken.setUserInfo(username, password);

WSSecHeader secHeader = new WSSecHeader("", false);
secHeader.insertSecurityHeader(document);
usernametoken.build(document, secHeader);

This is my result (notice the header that was inserted is not namespaced correctly, as well as there being two headers): 这是我的结果(请注意,插入的标题没有正确命名空间,并且有两个标题):

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsd="http://sample03.samples.rampart.apache.org/xsd">   
   <Header>      
      <wsse:Security soapenv:mustUnderstand="1" xmlns:wsse="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd">         
         <wsse:UsernameToken wsu:Id="UsernameToken-2765109" xmlns:wsu="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd">            
            <wsse:Username>bob</wsse:Username>            
            <wsse:Password Type="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-username-token-profile-1.0#PasswordText">bobPW</wsse:Password>         
         </wsse:UsernameToken>      
      </wsse:Security>   
   </Header>   
   <soapenv:Header/>   
   <soapenv:Body>      
      <xsd:echo>         
         <xsd:param0>hurro kitty</xsd:param0>      
      </xsd:echo>   
   </soapenv:Body></soapenv:Envelope>

What am I doing wrong? 我究竟做错了什么?

What am I doing wrong? 我究竟做错了什么?

When you are building the initial XML, you need to make sure that the DocumentBuilderFactory is namespace-aware. 在构建初始XML时,需要确保DocumentBuilderFactory支持名称空间。 WSSecurity is trying to find the soap header by the soap namespace but it isn't available. WSSecurity尝试通过soap名称空间查找soap头,但是它不可用。 Adding the following line should fix it: 添加以下行即可解决此问题:

DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
factory.setNamespaceAware(true);
...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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