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

これが私の結果です (挿入されたヘッダーが正しく名前空間化されておらず、2 つのヘッダーがあることに注意してください):

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

私は何を間違っていますか?

4

1 に答える 1

3

私は何が間違っているのですか?

初期XMLを作成するときは、DocumentBuilderFactoryが名前空間に対応していることを確認する必要があります。WSSecurityはsoap名前空間によってsoapヘッダーを検索しようとしていますが、使用できません。次の行を追加すると修正されます。

DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
factory.setNamespaceAware(true);
...
于 2009-11-13T15:38:48.533 に答える