簡體   English   中英

在Java中自定義SOAP標頭

[英]Customize SOAP header in Java

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

因為在此元素中不需要。

任何技巧如何做到這一點將不勝感激。

我最近通過以下方式解決了該問題:-

  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等同於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> 

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM