0

我很难理解为什么左侧的代码生成的肥皂请求不起作用,但是如果我将它调整到右侧的内容,那么它会起作用吗?

在此处输入图像描述 既然我知道需要做什么才能使其工作,我该如何解决它?

我添加jaxws-maven-plugin到我的 java 项目中:

     <build>
        <plugins>
            <plugin>
                <groupId>org.codehaus.mojo</groupId>
                <artifactId>jaxws-maven-plugin</artifactId>
                <version>2.5</version>
                <configuration>
                   <sourceDestDir>src/main/java</sourceDestDir>
                   <wsdlDirectory>src/main/resources/wsdl</wsdlDirectory>
                   <wsdlFiles>
                       <wsdlFile>Flattened_Integrator7.0.wsdl</wsdlFile>
                   </wsdlFiles>
                   <keep>true</keep>
                </configuration>
                <executions>
                    <execution>
                        <goals>
                            <goal>wsimport</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>

注意上图中没有prefix wsse,它不起作用。

必须是那个词。它存在于wsdl文件中。 在此处输入图像描述 有谁知道如何:

  1. 我可以强制namespace prefixhttp://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsdwsse
  2. soap envelope强制代码在节中而不是在Security节中生成命名空间
4

1 回答 1

0

因此,我必须手动将前缀/命名空间添加到信封,并将所有子前缀重命名为wsse.

这是我的做法:

@Component
public class RequestClient {

    private static final String WSSE_PREFIX = "wsse";
    private static final String WSSE_NAMESPACE = "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd";
    private static final String NS2_PREFIX = "ns2";
    private static final String NS2_NAMESPACE = "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd";

    private buildSoaprequest(){
        ...
        SOAPEnvelope soapEnvelope = soapMessage.getSOAPPart().getEnvelope();
        soapEnvelope.addNamespaceDeclaration(WSSE_PREFIX, WSSE_NAMESPACE);
        soapEnvelope.addNamespaceDeclaration(NS2_PREFIX, NS2_NAMESPACE);
        SOAPHeader soapHeader = soapMessage.getSOAPHeader();
        removeUndesiredBodyNamespaceEntries(soapHeader.getChildElements());
        soapHeader.setPrefix(WSSE_PREFIX);
        addDesiredBodyNamespaceEntries(soapHeader.getChildElements());              
        soapMessage.saveChanges();
        ...
    }

    private void addDesiredBodyNamespaceEntries(Iterator childElements) {
        while (childElements.hasNext()) {
          final Object childElementNode = childElements.next();
          if (childElementNode instanceof SOAPElement) {
            SOAPElement soapElement = (SOAPElement) childElementNode;
            soapElement.setPrefix(WSSE_PREFIX); 
            addDesiredBodyNamespaceEntries(soapElement.getChildElements());
          }
        }
      }

    private void removeUndesiredBodyNamespaceEntries(Iterator childElements) {
        while (childElements.hasNext()) {
          final Object childElementNode = childElements.next();
          if (childElementNode instanceof SOAPElement) {
            SOAPElement soapElement = (SOAPElement) childElementNode;

            //remove any prefix/namespace entries added by JAX-WS in the body element
            //it cannot be null, so it will leave wsse
            for (String prefix : getNamespacePrefixList(soapElement.getNamespacePrefixes())) {
              if (prefix != null) {
                soapElement.removeNamespaceDeclaration(prefix);
              }
            }
            // recursively remove prefix/namespace entries in child elements
            removeUndesiredBodyNamespaceEntries(soapElement.getChildElements());
          }
        }
      }

     private Set<String> getNamespacePrefixList(Iterator namespacePrefixIter) {
        Set<String> namespacePrefixesSet = new HashSet<>();
        while (namespacePrefixIter.hasNext()) {
          namespacePrefixesSet.add((String) namespacePrefixIter.next());
        }
        return namespacePrefixesSet;
      }
于 2019-05-31T12:56:38.393 回答