简体   繁体   English

在Java中自定义SOAP标头

[英]Customize SOAP header in Java

I am pretty new to SOAP and I would like to learn how to customize SOAP header. 我是SOAP的新手,我想学习如何自定义SOAP标头。 More specifically, I am trying to configure my outbound message SOAP header to be compliant with the expected format. 更具体地说,我尝试将出站消息SOAP标头配置为符合预期格式。 The header is going to be used for authentication purposes. 标头将用于身份验证目的。

This is what I have so far. 到目前为止,这就是我所拥有的。

I have set up a method to add the security deader where I am trying to format the header as per specification. 我已经设置了一种方法来添加安全终止符,在该方法中,我尝试按照规范设置标头的格式。

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);

When I print out the message in Eclipse console, the Security element is in the following format: 当我在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">

But this is the desired format of the Security format: 但这是安全格式的理想格式:

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

To summarize the issues that I need to address: 总结一下我需要解决的问题:

1) I need to change the SOAP-ENV to soapenv. 1)我需要将SOAP-ENV更改为soapenv。

SOAP-ENV:mustUnderstand="1" SOAP-ENV:mustUnderstand属性= “1”

should be 应该

soapenv:mustUnderstand="1" soapenv:mustUnderstand属性= “1”

2) I need to remove 2)我需要删除

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

as it's not needed in this element. 因为在此元素中不需要。

Any tips how to accomplish it would be greatly appreciated. 任何技巧如何做到这一点将不胜感激。

I recently solved the issue in following way:- 我最近通过以下方式解决了该问题:-

  1. Created SOAP Message from a template XML file 从模板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. Be careful in sending TimeStamp. 发送时间戳时要小心。 Client and Server clocks should be in Sync so be careful about the client and server machine's timezone 客户端和服务器时钟应保持同步,因此请注意客户端和服务器计算机的时区

  3. Nonce String should be encoded properly. Nonce字符串应正确编码。 I took help from:- 我从以下方面寻求帮助:-
    Java Webservice Client UsernameToken equivalent to PHP Java Webservice Client UsernameToken等同于PHP

  4. Here's how template XML file looks like:- 模板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