在已生成的 SOAP 信封的安全标头中插入用户名令牌会给我两个标头!

发布于 2024-08-11 04:34:11 字数 2261 浏览 1 评论 0原文

我使用 WSS4J 在已形成的 SOAP 请求信封的标头中添加用户名令牌。

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>

这是我的代码(字符串、请求是上面的请求):

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

这是我的结果(请注意,插入的标头命名空间不正确,并且存在两个标头):

<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>

我做错了什么?

I'm using WSS4J to add a Username token in the header of an already formed SOAP request envelope.

Here is what the SOAP request looks like:

<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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

GRAY°灰色天空 2024-08-18 04:34:11

我做错了什么?

当您构建初始 XML 时,您需要确保 DocumentBuilderFactory 是命名空间感知的。 WSSecurity 尝试通过soap 命名空间查找soap 标头,但它不可用。添加以下行应该可以修复它:

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

What am I doing wrong?

When you are building the initial XML, you need to make sure that the DocumentBuilderFactory is namespace-aware. WSSecurity is trying to find the soap header by the soap namespace but it isn't available. Adding the following line should fix it:

DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
factory.setNamespaceAware(true);
...
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文