Java中SOAP消息中的数字签名XML问题

时间:2015-09-24 21:38:46

标签: java xml soap

我有以下问题:

我正在制作一个应用程序,将已经数字签名的XML证书发送给官方政府。 web服务。

当我生成SOAP消息时,我将整个文件复制到一个字符串(我使用了几种技术来执行此操作:Guava的File.toString,Files.readAllBytes(Paths.get(path),并尝试使用所有文件)相关编码(UTF-8和ISO-8859-1,因为该文件是西班牙语)。我使用记录器在发送之前存储肥皂消息的内容。

问题是webservice给了我一个“无效的签名”答案,但是当我从日志中复制soap消息并通过SoapUI发送它时,webservice接受签名的证书。

以下是实现魔术的代码:

public class SOAPClientSAAJ {

public static void SendMessage(String file, String rscpath) throws Exception {
    // Create SOAP Connection
    SOAPConnectionFactory soapConnectionFactory = SOAPConnectionFactory.newInstance();
    SOAPConnection soapConnection = soapConnectionFactory.createConnection();
    GetCurrentTimeStamp cts = new GetCurrentTimeStamp();
    Logger logger = Logger.getLogger("MyLog");  
    FileHandler fh;  

    String logfile = rscpath+cts.GetCurrentTimeStamp()+".log";
    try {  

        // This block configure the logger with handler and formatter  
        fh = new FileHandler(logfile);  
        logger.addHandler(fh);
        SimpleFormatter formatter = new SimpleFormatter();  
        fh.setFormatter(formatter);  

    } catch (SecurityException e) {  
        e.printStackTrace();  
    } catch (IOException e) {  
        e.printStackTrace();  
    }  

    // Send SOAP Message to SOAP Server
    String url = "http://172.25.1.220:8080/SGPRTWebServices/RecepcionCrt";
    SOAPMessage soapResponse = null;
    SOAPMessage soapmsg = null;
    logger.info(file.substring(file.lastIndexOf(File.separator)));
    soapmsg = createSOAPRequest(file);
    ByteArrayOutputStream out = new ByteArrayOutputStream();

    soapmsg.writeTo(out);
    String msg = new String(out.toByteArray());
    logger.info(msg);
    out = null;
    msg = null;

    try{
    soapResponse = soapConnection.call(soapmsg, url);

    SOAPPart sp = soapResponse.getSOAPPart();
    SOAPEnvelope se = sp.getEnvelope();
    SOAPBody sb = se.getBody();
    ByteArrayOutputStream out2 = new ByteArrayOutputStream();
    soapResponse.writeTo(out2);
    msg = new String(out2.toByteArray());
    logger.info(msg);

    }catch (Exception ex) {
        logger.info(ex.toString());
    }
    soapConnection.close();
}

private static SOAPMessage createSOAPRequest(String confFilePath) throws Exception {
    MessageFactory messageFactory = MessageFactory.newInstance();
    SOAPMessage soapMessage = messageFactory.createMessage();
    SOAPPart soapPart = soapMessage.getSOAPPart();

    String serverURI = "http://recepcionCrt.ws.sgprt.mtt.cl/";

    Path pat =Paths.get(confFilePath);
    Charset charset = StandardCharsets.ISO_8859_1;

    String texto = "<![CDATA[";
    File fl = pat.toFile();
    String content =  Files.toString(fl, charset);
    texto = texto + content +"]]>";

    SOAPEnvelope envelope = soapPart.getEnvelope();
    envelope.setEncodingStyle("UTF-8");
    SOAPBody soapBody = envelope.getBody();
    soapBody.addNamespaceDeclaration("ns2", serverURI);
    SOAPElement soapBodyElem = soapBody.addChildElement ("recepcionCRT","ns2");
    SOAPElement soapBodyElem1 = soapBodyElem.addChildElement ("arg0");

    soapBodyElem1.addTextNode(texto);
    MimeHeaders headers = soapMessage.getMimeHeaders();
    headers.addHeader("SOAPAction", serverURI  + "recepcionCRT");
    soapMessage.saveChanges();

    return soapMessage;
}
}

此处:can you check some files喜欢wsdl,有效证书和有效的肥皂信息。

1 个答案:

答案 0 :(得分:0)

事实证明,问题出在沟通过程中。我不得不使用jdk中的importws工具来加载接口和clases以与提供者通信。完成后,邮件已正确收到,签名已经过验证。