إدراج رمزية اسم المستخدم في رأس الأمان من مغلف الصابون الذي تم إنشاؤه بالفعل يعطيني رؤساء!

StackOverflow https://stackoverflow.com/questions/1724598

سؤال

أنا أستخدم WSS4J لإضافة رمز اسم مستخدم في رأس مظروف طلب الصابون الذي تم تشكيله بالفعل.

هنا هو ما يشبه طلب الصابون:

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

ما الخطأ الذي افعله؟

هل كانت مفيدة؟

المحلول

ما الخطأ الذي افعله؟

عندما تقوم ببناء XML الأولي، تحتاج إلى التأكد من أن مستندات المستندات هي تدرك مساحة الاسم. يحاول WSSecurity العثور على رأس الصابون بواسطة مساحة اسم الصابون ولكنه غير متوفر. يجب إضافة السطر التالي إصلاحه:

DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
factory.setNamespaceAware(true);
...
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top