首页 文章

在Java中自定义SOAP标头

提问于
浏览
0

我是SOAP的新手,我想学习如何自定义SOAP头 . 更具体地说,我试图配置我的出站消息SOAP标头以符合预期的格式 . 标头将用于身份验证 .

这就是我到目前为止所拥有的 .

我已经设置了一个方法来添加安全deader,我试图按照规范格式化标头 .

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/”

因为在这个元素中不需要它 .

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

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');
        }
    
    • 发送TimeStamp时要小心 . 客户端和服务器时钟应该处于同步状态,因此请注意客户端和服务器计算机的时区

    • 应该正确编码Nonce字符串 . 我接受了以下方面的帮助:
      Java Webservice Client UsernameToken equivalent to PHP

    • 以下是模板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>
    

相关问题