How to use Axis2 WSDL2Code Plugin to Generate Client with Username Token Poliy

552 Views Asked by At

I am newer to maven but have been using SOAP services and Axis2 for awhile. As mentioned, I am trying to use axis2-wsdl2code-maven-plugin to generate code from a 3rd party wsdl. The code generation is working for the most part. I see the expected classes generated in the generated-sources and I am able to reference the classes in my project.

The problem I am having is around the Security Policy. The error I am receiving, trying to run my project after generating the code is:

org.apache.neethi.builders.converters.ConverterException: com.ctc.wstx.exc.WstxParsingException: Undeclared namespace prefix "t"

Reviewing the ClientStub code generated, I found the following policy is being referenced:

<wsp:Policy wsu:Id="FedAuthSyncPolicy"
    xmlns:wsp="http://www.w3.org/ns/ws-policy"
    xmlns:wsu="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd">
    <wsp:ExactlyOne>
        <wsp:All>
            <sp:TransportBinding
                xmlns:sp="http://docs.oasis-open.org/ws-sx/ws-securitypolicy/200702">
                <wsp:Policy
                    xmlns:wsp="http://www.w3.org/ns/ws-policy">
                    <sp:TransportToken>
                        <wsp:Policy
                            xmlns:wsp="http://www.w3.org/ns/ws-policy">
                            <sp:HttpsToken/>
                        </wsp:Policy>
                    </sp:TransportToken>
                    <sp:AlgorithmSuite>
                        <wsp:Policy
                            xmlns:wsp="http://www.w3.org/ns/ws-policy">
                            <sp:Basic256/>
                        </wsp:Policy>
                    </sp:AlgorithmSuite>
                    <sp:Layout>
                        <wsp:Policy
                            xmlns:wsp="http://www.w3.org/ns/ws-policy">
                            <sp:Lax/>
                        </wsp:Policy>
                    </sp:Layout>
                </wsp:Policy>
            </sp:TransportBinding>
            <sp:SignedSupportingTokens
                xmlns:sp="http://docs.oasis-open.org/ws-sx/ws-securitypolicy/200702">
                <wsp:Policy
                    xmlns:wsp="http://www.w3.org/ns/ws-policy">
                    <sp:IssuedToken sp:IncludeToken="http://docs.oasis-open.org/ws-sx/ws-securitypolicy/200702/IncludeToken/AlwaysToRecipient">
                        <sp:RequestSecurityTokenTemplate>
                            <t:TokenType>http://docs.oasis-open.org/wss/oasis-wss-saml-token-profile-1.1#SAMLV2.0</t:TokenType>
                            <t:KeyType>http://docs.oasis-open.org/ws-sx/ws-trust/200512/Bearer</t:KeyType>
                            <t:CanonicalizationAlgorithm>http://www.w3.org/2001/10/xml-exc-c14n#</t:CanonicalizationAlgorithm>
                            <t:SignWith>http://www.w3.org/2000/09/xmldsig#hmac-sha1</t:SignWith>
                            <t:SignatureAlgorithm>http://www.w3.org/2000/09/xmldsig#rsa-sha1</t:SignatureAlgorithm>
                        </sp:RequestSecurityTokenTemplate>
                        <wsp:Policy>
                            <sp:RequireInternalReference/>
                        </wsp:Policy>
                        <sp:Issuer>
                            <wsaw:Address/>
                            <wsaw:Metadata>
                                <wsx:Metadata>
                                    <wsx:MetadataSection>
                                        <wsx:MetadataReference>
                                            <wsaw:Address/>
                                        </wsx:MetadataReference>
                                    </wsx:MetadataSection>
                                </wsx:Metadata>
                            </wsaw:Metadata>
                        </sp:Issuer>
                    </sp:IssuedToken>
                </wsp:Policy>
            </sp:SignedSupportingTokens>
            <sp:Wss11
                xmlns:sp="http://docs.oasis-open.org/ws-sx/ws-securitypolicy/200702">
                <wsp:Policy
                    xmlns:wsp="http://www.w3.org/ns/ws-policy"/>
                </sp:Wss11>
                <sp:Trust13
                    xmlns:sp="http://docs.oasis-open.org/ws-sx/ws-securitypolicy/200702">
                    <wsp:Policy
                        xmlns:wsp="http://www.w3.org/ns/ws-policy">
                        <sp:MustSupportIssuedTokens/>
                    </wsp:Policy>
                </sp:Trust13>
            </wsp:All>
        </wsp:ExactlyOne>
    </wsp:Policy>

Notice the nodes prefixed with the namespace "t". Reviewing a project I did previously with the same webservice, code was generated from Axis2 via IntelliJ, not through maven. The Stub was generated with the following policy:

<wsp:Policy xmlns:wsp="http://schemas.xmlsoap.org/ws/2004/09/policy" xmlns:wsu="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd" wsu:Id="SyncPolicy">
   <wsp:ExactlyOne>
      <wsp:All>
         <sp:TransportBinding xmlns:sp="http://schemas.xmlsoap.org/ws/2005/07/securitypolicy">
            <wsp:Policy>
               <wsp:ExactlyOne>
                  <wsp:All>
                     <sp:TransportToken>
                        <wsp:Policy>
                           <wsp:ExactlyOne>
                              <sp:HttpsToken />
                           </wsp:ExactlyOne>
                        </wsp:Policy>
                     </sp:TransportToken>
                     <sp:AlgorithmSuite>
                        <wsp:Policy>
                           <sp:Basic256 />
                        </wsp:Policy>
                     </sp:AlgorithmSuite>
                     <sp:Layout>
                        <wsp:Policy>
                           <wsp:ExactlyOne>
                              <sp:Lax />
                           </wsp:ExactlyOne>
                        </wsp:Policy>
                     </sp:Layout>
                  </wsp:All>
               </wsp:ExactlyOne>
            </wsp:Policy>
         </sp:TransportBinding>
         <sp:SignedSupportingTokens xmlns:sp="http://schemas.xmlsoap.org/ws/2005/07/securitypolicy">
            <wsp:Policy>
               <wsp:ExactlyOne>
                  <sp:UsernameToken>
                     <wsp:Policy>
                        <wsp:ExactlyOne>
                           <sp:WssUsernameToken10 />
                        </wsp:ExactlyOne>
                     </wsp:Policy>
                  </sp:UsernameToken>
               </wsp:ExactlyOne>
            </wsp:Policy>
         </sp:SignedSupportingTokens>
      </wsp:All>
      <wsp:All>
         <sp:TransportBinding xmlns:sp="http://schemas.xmlsoap.org/ws/2005/07/securitypolicy">
            <wsp:Policy>
               <wsp:ExactlyOne>
                  <wsp:All>
                     <sp:TransportToken>
                        <wsp:Policy>
                           <wsp:ExactlyOne>
                              <sp:HttpsToken />
                           </wsp:ExactlyOne>
                        </wsp:Policy>
                     </sp:TransportToken>
                     <sp:AlgorithmSuite>
                        <wsp:Policy>
                           <sp:Basic256 />
                        </wsp:Policy>
                     </sp:AlgorithmSuite>
                     <sp:Layout>
                        <wsp:Policy>
                           <wsp:ExactlyOne>
                              <sp:Lax />
                           </wsp:ExactlyOne>
                        </wsp:Policy>
                     </sp:Layout>
                  </wsp:All>
               </wsp:ExactlyOne>
            </wsp:Policy>
         </sp:TransportBinding>
         <sp:SupportingTokens xmlns:sp="http://schemas.xmlsoap.org/ws/2005/07/securitypolicy">
            <wsp:Policy>
               <wsp:ExactlyOne>
                  <sp:UsernameToken>
                     <wsp:Policy>
                        <wsp:ExactlyOne>
                           <sp:WssUsernameToken10 />
                        </wsp:ExactlyOne>
                     </wsp:Policy>
                  </sp:UsernameToken>
               </wsp:ExactlyOne>
            </wsp:Policy>
         </sp:SupportingTokens>
      </wsp:All>
   </wsp:ExactlyOne>
</wsp:Policy>

That policy references the Username Token, which is required for this web service. How can I have the Maven Axis2 WSDL2Code plugin use a Username Token policy instead of the Issued Token policy?

1

There are 1 best solutions below

1
On

I had this exact same problem, perhaps we're using the same web service. My solution involved modifying the generated ...Stub.java file wherever "t:" occurred, with the line from what your file would've contained had you used the older version of the WSDL.