java 从肥皂头中删除 mustUnderstand 属性

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7473145/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 20:08:17  来源:igfitidea点击:

removing mustUnderstand attribute from soap headers

javasoapaxis

提问by cacert

How to remove mustunderstand attribute from soap header in axis client.even i dont set it especially, when i set soap header info mustundertand and actor attributes are automatically added to soap message.Does anybody know how to remove them ? I am using Axis2 1.4 version's wsdl2java to create my ws client.

如何从轴客户端的soap头中删除mustunderstand属性。即使我没有特别设置它,当我设置soap头信息mustundertand和actor属性会自动添加到soap消息中。有人知道如何删除它们吗?我正在使用 Axis2 1.4 版本的 wsdl2java 来创建我的 ws 客户端。

回答by Reinhard

If you want to disable the must understand check in the AXIS client you have to add the following line to your code:

如果您想在 AXIS 客户端中禁用必须了解的检查,您必须将以下行添加到您的代码中:

_call.setProperty(Call.CHECK_MUST_UNDERSTAND, new Boolean(false));

then the MustUnderstandCheckerof the AXIS Client is never invoked.

那么永远不会调用 AXIS 客户端的MustUnderstandChecker

回答by Javarome

None of those solutions worked for me, as:

这些解决方案都不适合我,因为:

Looking at the answer to "Adding ws-security to wsdl2java generated classes" helped me to write a solution that worked for me:

查看“将 ws-security 添加到 wsdl2java 生成的类”的答案帮助我编写了一个对我有用的解决方案:

void addSecurityHeader(Stub stub, final String username, final String password) {
  QName headerName = new QName("http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd", "Security");  // Or any other namespace that fits in your case
  AtomicReference<SOAPHeaderElement> header 
    = new AtomicReference<SOAPHeaderElement>
        (new SOAPHeaderElement(headerName) {                       
           {  
             SOAPElement utElem = addChildElement("UsernameToken");
             utElem.addChildElement("Username").setValue(username);
             utElem.addChildElement("Password").setValue(password);
           }
           @Override
           public void setAttribute(String namespace, String localName, String value) {
             if (!Constants.ATTR_MUST_UNDERSTAND.equals(localName)) {  // Or any other attribute name you'd want to avoid
               super.setAttribute(namespace, localName, value);
             }
           }
        });
  SOAPHeaderElement soapHeaderElement = header.get();
  soapHeaderElement.setActor(null);      // No intermediate actors are involved.
  stub.setHeader(soapHeaderElement);  // Finally, attach the header to the stub
}

回答by Daniel Pawlik

In my case it worked manually remove the attribute from the SOAPHeader

在我的情况下,它可以手动从SOAPHeader 中删除该属性

SOAPHeader header = env.getHeader();
OMChildrenQNameIterator childrenWithName = (OMChildrenQNameIterator) header.getChildrenWithName(omElementauthentication.getQName());
    while (childrenWithName.hasNext()) {
        org.apache.axiom.om.OMElement omElement = (org.apache.axiom.om.OMElement) childrenWithName.next();
        QName mustAnderstandQName = omElement.resolveQName("soapenv:mustUnderstand");
        if (mustAnderstandQName == null) {
            continue;
        }
        OMAttribute mustAnderstandAttribute = omElement.getAttribute(mustAnderstandQName);
            if (mustAnderstandAttribute == null) {
                continue;
            }
        omElement.removeAttribute(mustAnderstandAttribute);
    }

回答by tazarov

I was recently struggling with similar situation and by doing some google-ing I managed to find the following solution.

我最近在类似的情况下苦苦挣扎,通过做一些谷歌搜索,我设法找到了以下解决方案。

Having used Axis2 you would've probably generated a MyWSStub file that contains the calls to your service. Create an wrapper class (better not touch the auto-generated stub files) that extends your stub e.g. MyWSStubWrapper:

使用 Axis2 后,您可能会生成一个 MyWSStub 文件,其中包含对您的服务的调用。创建一个扩展您的存根的包装类(最好不要接触自动生成的存根文件),例如 MyWSStubWrapper:

public class MyWSStubWrapper extends MyWSStub {

/**
 * @throws AxisFault
 */
public MyWSStubWrapper() throws AxisFault {
}

/**
 * @param configurationContext
 * @throws AxisFault
 */
public MyWSStubWrapper(ConfigurationContext configurationContext) throws AxisFault {
    super(configurationContext);
}

/**
 * @param targetEndpoint
 * @throws AxisFault
 */
public MyWSStubWrapper(String targetEndpoint) throws AxisFault {
    super(targetEndpoint);
}

/**
 * @param configurationContext
 * @param targetEndpoint
 * @throws AxisFault
 */
public MyWSStubWrapper(ConfigurationContext configurationContext, String targetEndpoint) throws AxisFault {
    super(configurationContext, targetEndpoint);
}

/**
 * @param configurationContext
 * @param targetEndpoint
 * @param useSeparateListener
 * @throws AxisFault
 */
public MyWSStubWrapper(ConfigurationContext configurationContext, String targetEndpoint, boolean useSeparateListener) throws AxisFault {
    super(configurationContext, targetEndpoint, useSeparateListener);
}

@Override
protected void addHeader(OMElement omElementToadd, SOAPEnvelope envelop, boolean mustUnderstand) {
    SOAPHeaderBlock soapHeaderBlock = envelop.getHeader().addHeaderBlock(omElementToadd.getLocalName(), omElementToadd.getNamespace());
    OMNode omNode = null;

    // add child elements
    for (Iterator iter = omElementToadd.getChildren(); iter.hasNext();) {
        omNode = (OMNode) iter.next();
        iter.remove();
        soapHeaderBlock.addChild(omNode);
    }

    OMAttribute omatribute = null;
    // add attributes
    for (Iterator iter = omElementToadd.getAllAttributes(); iter.hasNext();) {
        omatribute = (OMAttribute) iter.next();
        soapHeaderBlock.addAttribute(omatribute);
    }
}

}

}

Bear in mind that the above code will completely remove the soapenv:mustUnderstand="0|1" from your headers if you wish to added you need to call soapHeaderBlock.setMustUnderstand(true|false); somewhere in your code.

请记住,如果您想添加,上面的代码将从您的标题中完全删除 soapenv:mustUnderstand="0|1" ,您需要调用 soapHeaderBlock.setMustUnderstand(true|false); 在您的代码中的某处。

回答by Simeon G

1) Are you using the Axis SOAPHeaderElement and if so, are you setting the mustUnderstand setter to false?

1) 您是否在使用 Axis SOAPHeaderElement,如果是,您是否将 mustUnderstand 设置器设置为 false?

2) Since you're generating your client with wsdl2java, it's quite possible that the WSDL (or more accurately, the schema) contains the mustUnderstand attribute on an element referenced in your SOAP binding. So when wsdlToJava generates the client code, those attributes will naturally be added. See herefor a description of the mustUnderstand attribute. If modifying the WSDL is out of the question, and you must remove this attribute from the header, then I suppose you can try to do it with a handler

2) 由于您使用 wsdl2java 生成客户端,WSDL(或更准确地说,模式)很可能包含 SOAP 绑定中引用的元素上的 mustUnderstand 属性。所以当 wsdlToJava 生成客户端代码时,这些属性自然会被添加。有关mustUnderstand 属性的说明,请参见此处。如果修改 WSDL 是不可能的,并且您必须从标题中删除此属性,那么我想您可以尝试使用处理程序来完成

3) Not advisable, but if you really MUST remove this attribute then I suppose you can add a client side handler that alters the header: http://ws.apache.org/axis/java/apiDocs/org/apache/axis/handlers/package-summary.html

3)不可取,但如果你真的必须删除这个属性,那么我想你可以添加一个改变标题的客户端处理程序:http: //ws.apache.org/axis/java/apiDocs/org/apache/axis/处理程序/package-summary.html

回答by A C S

i am using axis 1.4 client with ws security

我正在使用具有 ws 安全性的轴 1.4 客户端

in my case as Reinhard said this worked

就我而言,正如莱因哈德所说,这有效

MyService service = new MyServiceLocator(); 
MyServicePortType port = service.getMyServiceHttpsSoap11Endpoint();

((Stub) port)._setProperty(Call.CHECK_MUST_UNDERSTAND, Boolean.FALSE);