xml - 如何使用 Axis2 和 Rampart 在 SOAP 请求中添加 MessageID

标签 xml web-services soap axis2 rampart

我正在尝试构建一个使用 Axis2rampat 1.6 使用外部服务器应用程序的客户端应用程序。

检查 SOAP 请求时,一切似乎都很好,因为 SOAP 按预期进行了加密和签名。以下是用于该目的的 policy.xml 文件:

<wsp:Policy wsu:Id="MyPolicy"
    xmlns:wsu="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd"
    xmlns:wsp="http://schemas.xmlsoap.org/ws/2004/09/policy"
    xmlns:sp="http://schemas.xmlsoap.org/ws/2005/07/securitypolicy"
    xmlns:wsam="http://schemas.xmlsoap.org/ws/2004/08/addressing" 
    xmlns:wst="http://docs.oasis-open.org/wss/oasis-wss-saml-token-profile-1.0#SAMLAssertionID">
<wsp:ExactlyOne>
    <wsp:All>
        <sp:SignedSupportingTokens xmlns:sp="http://docs.oasis-open.org/ws-sx/ws-securitypolicy/200702">
            <wsp:Policy>
                <sp:UsernameToken sp:IncludeToken="http://docs.oasis-open.org/ws-sx/ws-securitypolicy/200702/IncludeToken/AlwaysToRecipient" />
            </wsp:Policy>
        </sp:SignedSupportingTokens>
        <sp:SymmetricBinding>
            <wsp:Policy>
                <sp:ProtectionToken>
                    <wsp:Policy>
                        <sp:X509Token sp:IncludeToken="http://schemas.xmlsoap.org/ws/2005/07/securitypolicy/IncludeToken/Never">
                            <wsp:Policy>
                                <sp:RequireIssuerSerialReference/>
                                <sp:WssX509V3Token10/>
                            </wsp:Policy>
                        </sp:X509Token>
                    </wsp:Policy>
                </sp:ProtectionToken>

                <sp:AlgorithmSuite>
                    <wsp:Policy>
                        <sp:Basic128/>
                    </wsp:Policy>
                </sp:AlgorithmSuite>
                <sp:IncludeTimestamp/>
                <sp:Layout>
                    <wsp:Policy>
                        <sp:Strict/>
                    </wsp:Policy>
                </sp:Layout>
                <sp:OnlySignEntireHeadersAndBody/>
            </wsp:Policy>
        </sp:SymmetricBinding>
        <sp:EncryptedParts>
            <sp:Body />
        </sp:EncryptedParts>
        <sp:SignedParts>
            <sp:Body/>
            <sp:Header Namespace="http://www.w3.org/2005/08/addressing" Name="ReplyTo"/>
            <sp:Header Namespace="http://www.w3.org/2005/08/addressing" Name="To"/>
            <sp:Header Namespace="http://www.w3.org/2005/08/addressing" Name="From"/>
            <sp:Header Namespace="http://docs.oasis-open.org/ws-rx/wsrm/200702" Name="AckRequested"/>
            <sp:Header Namespace="http://docs.oasis-open.org/ws-rx/wsrm/200702" Name="CreateSequence"/>
            <sp:Header Namespace="http://docs.oasis-open.org/ws-rx/wsrm/200702" Name="Sequence"/>
            <sp:Header Namespace="http://www.w3.org/2005/08/addressing" Name="MessageID"/>
            <sp:Header Namespace="http://www.w3.org/2005/08/addressing" Name="FaultTo"/>
            <sp:Header Namespace="http://docs.oasis-open.org/ws-rx/wsrm/200702" Name="SequenceAcknowledgement"/>
            <sp:Header Namespace="http://www.w3.org/2005/08/addressing" Name="Action"/>
            <sp:Header Namespace="http://www.w3.org/2005/08/addressing" Name="RelatesTo"/>
        </sp:SignedParts>
        <sp:Wss11>
            <wsp:Policy>
                <sp:MustSupportRefEncryptedKey/>
                <sp:MustSupportRefIssuerSerial/>
                <sp:MustSupportRefThumbprint/>
            </wsp:Policy>
        </sp:Wss11>
        <ramp:RampartConfig xmlns:ramp="http://ws.apache.org/rampart/policy">
            <ramp:userCertAlias>mySignAlias</ramp:userCertAlias>
            <ramp:encryptionUser>myEncryptAlias</ramp:encryptionUser>
            <ramp:user>myUser</ramp:user>
            <ramp:passwordCallbackClass>myPackage.PasswordCallBackHandler</ramp:passwordCallbackClass>
            <ramp:encryptionCypto>
                <ramp:crypto provider="org.apache.ws.security.components.crypto.Merlin">
                    <ramp:property name="org.apache.ws.security.crypto.merlin.keystore.type">JKS</ramp:property>
                    <ramp:property name="org.apache.ws.security.crypto.merlin.file">[path goes here]/clientTrustStore.jks</ramp:property>
                    <ramp:property name="org.apache.ws.security.crypto.merlin.keystore.password">changeit</ramp:property>
                </ramp:crypto>
            </ramp:encryptionCypto>

            <ramp:signatureCrypto>
                <ramp:crypto provider="org.apache.ws.security.components.crypto.Merlin">
                    <ramp:property name="org.apache.ws.security.crypto.merlin.keystore.type">JKS</ramp:property>
                    <ramp:property name="org.apache.ws.security.crypto.merlin.file">[path goes here]/clientKeyStore.jks</ramp:property>
                    <ramp:property name="org.apache.ws.security.crypto.merlin.keystore.password">changeit</ramp:property>
                </ramp:crypto>
            </ramp:signatureCrypto>
        </ramp:RampartConfig>

    </wsp:All>
</wsp:ExactlyOne>

但是在调用外部服务器应用程序时出现以下轴错误:
SEVERE: org.apache.axis2.AxisFault: com.sun.xml.wss.XWSSecurityException: Policy verification error:Missing target MessageID for Signature
at org.apache.axis2.util.Utils.getInboundFaultFromMessageContext(Utils.java:531)
at org.apache.axis2.description.OutInAxisOperationClient.handleResponse(OutInAxisOperation.java:375)
at org.apache.axis2.description.OutInAxisOperationClient.send(OutInAxisOperation.java:421)
at org.apache.axis2.description.OutInAxisOperationClient.executeImpl(OutInAxisOperation.java:229)
at org.apache.axis2.client.OperationClient.execute(OperationClient.java:165)

该错误是自描述性的,SOAP 请求在 SOAP header 中缺少 MessageID 标记。我厌倦了找出如何添加提到的标签但没有运气;我发现了如何在此链接中添加自定义 SOAP header adding authentication header to client stub axis2

但我想避免这种情况,因为我使用 PasswordCallBackHandler 添加身份验证属性。

有没有办法在 SOAP header 中自动添加 MessageID ?此外,还有类似的标签,如 ActionReplyToToFrom 如何将它们添加到 SOAP header ?

-- 更新:

从使用 Axis2 工具自动生成的 java 文件 - 请参阅 Apache Axis2 User's Guide - Creating Clients 以了解 stub 文件是如何生成的 - 我转到自动生成的 Java 类 ServiceStub 中的 API 方法。创建了一个消息上下文来发送 SOAP 请求,如下所示:
 // create a message context
_messageContext = new org.apache.axis2.context.MessageContext();

我使用对象 _messageContext 设置消息 ID 如下:
_messageContext.setMessageID("TEST_123456");

此外,我在 Debug模式下运行应用程序,我可以看到 TEST_123456 已作为 MessageID 添加到 SOAP 请求中,并且服务器在其响应中使用相同的 MessageID。但我仍然收到此有线错误 Policy verification error:Missing target MessageID for Signature
我很困惑服务器如何说他缺少 MessageID 尽管它出现在仅来自服务器的 SOAP 请求和响应中!

这与自动签名过程有什么关系吗?但即便如此,MessageID 已经添加到策略文件的 SignedParts 中,还有什么可以解决这个问题?

最佳答案

您可以在 Stub::ServiceClient::Options 中添加所有属性,例如 Message、Action、To 和其他属性。 .

ServiceClient client = serviceStub._getServiceClient();
Options options = client.getOptions();
options.setAction(action);
options.setTo(new EndpointReference(referenceURL));
options.setMessageId(UUID.randomUUID().toString());
    
Sample Reference

关于xml - 如何使用 Axis2 和 Rampart 在 SOAP 请求中添加 MessageID,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32198924/

相关文章:

vb.net - SOAP 错误 : The request was aborted: Could not create SSL/TLS secure channel

PHP Magento soap 登录调用返回空 session ID

c# - 如何使用 Swashbuckle 记录查询字符串参数?

java - 在spring xml中定义Camel路由有什么优缺点?

java - Ant和XML配置文件解析

java - IncationTargetException - 使用 httpClient 从 java Soap Web 服务调用 Rest 服务

java - XML 无法在编码上创建 JAXBContext

web-services - 使用 CXF 框架和 CXFNonSpringServlet 为 WebService (WebApplication Server) 配置 SSL 身份验证

c# - 动态导航 2009 RE : SoapException: Standard Item Code 'foo' does not exist

wcf - 如何使用 REST/SOAP 端点为 WCF 4.0 服务发布 WSDL