【问题标题】:Customize SOAP header in Java在 Java 中自定义 SOAP 标头
【发布时间】:2015-05-22 12:24:47
【问题描述】:

我对 SOAP 很陌生,我想学习如何自定义 SOAP 标头。更具体地说,我正在尝试将出站消息 SOAP 标头配置为符合预期格式。标头将用于身份验证。

这是我目前所拥有的。

我已经设置了一种方法来添加安全死者,我试图按照规范格式化标题。

private void addSecurityHeader(SOAPMessageContext messageContext) throws SOAPException {

public static final String WSSE_NS = "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd";
public static final String WSSE_SECURITY_NAME = "Security";
public static final String WSSE_NS_PREFIX = "wsse";
public static final String SOAPENV_NS_PREFIX = "soapenv";

SOAPEnvelope envelope = messageContext.getMessage().getSOAPPart().getEnvelope();
SOAPHeader header = messageContext.getMessage().getSOAPPart().getEnvelope().getHeader();
SOAPBody body = messageContext.getMessage().getSOAPPart().getEnvelope().getBody();

// changing prefix to soapenv
envelope.setPrefix(SOAPENV_NS_PREFIX);
header.setPrefix(SOAPENV_NS_PREFIX);
body.setPrefix(SOAPENV_NS_PREFIX);

// adding security Element
Name securityName = soapFactory.createName(WSSE_SECURITY_NAME, WSSE_NS_PREFIX, WSSE_NS);
SOAPHeaderElement securityElement = header.addHeaderElement(securityName);

当我在 Eclipse 控制台中打印出消息时,Security 元素的格式如下:

<wsse:Security xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:wsse="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd" SOAP-ENV:mustUnderstand="1">

但这是安全格式的所需格式:

<wsse:Security soapenv:mustUnderstand="1" xmlns:wsse="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd">

总结一下我需要解决的问题:

1) 我需要将 SOAP-ENV 更改为 soapenv。

SOAP-ENV:mustUnderstand="1"

应该是

soapenv:mustUnderstand="1"

2) 我需要删除

xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/"

因为这个元素不需要它。

任何如何完成它的提示将不胜感激。

【问题讨论】:

  • 天哪,什么样的服务器会关心特定的命名空间前缀?

标签: java web-services soap soapheader


【解决方案1】:

我最近通过以下方式解决了这个问题:-

  1. 从模板 XML 文件创建 SOAP 消息

    BufferedReader rd  = new BufferedReader(new FileReader(new File("D:\\TestGetOppuService.xml")));
        StringBuffer fileContent = new StringBuffer();
        String line = null;
        while ((line = rd.readLine()) != null)
        {
            if(line.indexOf("Current_TimeStamp")>0)
            {
                line = line.replaceAll("Current_TimeStamp", createTime);
            }
            if(line.indexOf("Expire_TimeStamp")>0)
            {
                line = line.replaceAll("Expire_TimeStamp", expiresTime);
            }
            if(line.indexOf("NONCE_STRING")>0)
            {
                line = line.replaceAll("NONCE_STRING", getNonceString(createTime));
            }
            fileContent.append(line + '\n');
        }
    
  2. 发送时间戳时要小心。客户端和服务器时钟应同步,因此请注意客户端和服务器计算机的时区

  3. Nonce 字符串应正确编码。我得到了帮助:-
    Java Webservice Client UsernameToken equivalent to PHP

  4. 模板 XML 文件如下所示:-

    <soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:typ="http://xmlns.oracle.com/apps/sales/opptyMgmt/opportunities/opportunityService/types/">
    <soapenv:Header>
      <wsse:Security soapenv:mustUnderstand="1" xmlns:wsse="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd" xmlns:wsu="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd">
         <wsu:Timestamp wsu:Id="TS-fasfwffsafsaf-asffsaf">
            <wsu:Created>Current_TimeStamp</wsu:Created>
            <wsu:Expires>Expire_TimeStamp</wsu:Expires>
         </wsu:Timestamp>
         <wsse:UsernameToken wsu:Id="UsernameToken-asfsafsaf-78787080affaf-saf">
            <wsse:Username>XXXXX</wsse:Username>
            <wsse:Password Type="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-username-token-profile-1.0#PasswordText">XXXXXXXXXXX</wsse:Password>
            <wsse:Nonce EncodingType="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-soap-message-security-1.0#Base64Binary">NONCE_STRING</wsse:Nonce>
            <wsu:Created>Current_TimeStamp</wsu:Created>
         </wsse:UsernameToken>
      </wsse:Security>
    </soapenv:Header>
    <soapenv:Body>
      -----------Content------------
    </soapenv:Body>
    </soapenv:Envelope>
    

【讨论】:

    猜你喜欢
    • 2012-05-17
    • 2011-01-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-02-25
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多