Coldfusion WSS4J java soap签名证书在首次签约后消失了

时间:2014-09-23 20:33:50

标签: java web-services soap coldfusion wss4j

我几个月来一直试图解决这个问题。我通过升级到最新版本的WSS4J来解决我的java对象的一些问题,并且我的所有测试用例都使用Coldfusion JRE在Eclipse内部进行测试。当我导出jar并将其添加到Coldfusion中的类路径并重置Coldfusion时,该对象将在第一次签署SOAP请求并在此之后开始抛出错误。它将始终签署第一个请求。我也尝试过JavaLoader,每次尝试从jar加载它,但这也不起作用。

以下是Coldfusion代码:

<cfset Soap = "<?xml version='1.0' encoding='UTF-8'?><soap:Envelope xmlns:soap='http://schemas.xmlsoap.org/soap/envelope/' xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'> <soap:Body> <test1></test1> </soap:Body> </soap:Envelope>">

<cfloop from="1" to="5" index="i">
    <cfset Signer = CreateObject("java","com.soapsigner.SoapSigner").init(Soap)>
    <cfdump var="#Signer.getLastError()#">
    <cfdump var="#Signer.getXML()#">
    <br  /><br /><br />
</cfloop>

<cfabort> 

以下是使用WSS4J 2.0.1

的Java代码
package com.soapsigner;

import java.io.PrintWriter;
import java.io.StringReader;
import java.io.StringWriter;
import java.util.Properties;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;

import org.apache.log4j.ConsoleAppender;
import org.apache.log4j.Level;
import org.apache.log4j.Logger;
import org.apache.log4j.PatternLayout;
import org.apache.wss4j.common.crypto.Crypto;
import org.apache.wss4j.common.crypto.Merlin;
import org.apache.wss4j.dom.WSConstants;
import org.apache.wss4j.dom.message.WSSecHeader;
import org.apache.wss4j.dom.message.WSSecSignature;
import org.apache.wss4j.dom.message.WSSecTimestamp;
import org.w3c.dom.Document;
import org.xml.sax.InputSource;

public class SoapSigner {
    private Crypto crypto;
    private WSSecSignature sig;
    private WSSecTimestamp time;
    private WSSecHeader header; 
    private String alias;
    private String password;
    private String keyFile;
    private String keyFileType;
    private Document signedDoc;
    private String lastError;
    {
        Logger rootLogger = Logger.getRootLogger();
        rootLogger.setLevel(Level.INFO);
        rootLogger.addAppender(new ConsoleAppender(
                   new PatternLayout("%-6r [%p] %c - %m%n")));
    }
    static { org.apache.xml.security.Init.init(); }

    //constructor
    public SoapSigner(String XML){
        try {           
            alias = "myalias";
            password = "mypassword";
            keyFile = "/keystore/keystore.pkcs";
            keyFileType = "pkcs12";
            sig = new WSSecSignature(); 
            time = new WSSecTimestamp();
            header = new WSSecHeader();
            signedDoc = null;
            lastError = "";         

            Merlin merlin = new Merlin(getCryptoProperties(), null, null);
            crypto = merlin;            

            signDocument(xmlToDoc(XML));
            /*testing
            System.out.println(getXML());
            System.out.println(getLastError());
            */
        } catch (Exception e) {
            setLastError(e);
        }
    }

    //properties
    public Properties getCryptoProperties(){
        Properties cryptoProperties = new Properties();
        cryptoProperties.setProperty("org.apache.ws.security.crypto.merlin.keystore.alias", alias);       
        cryptoProperties.setProperty("org.apache.ws.security.crypto.merlin.keystore.password", password);
        cryptoProperties.setProperty("org.apache.ws.security.crypto.merlin.keystore.type", keyFileType);
        cryptoProperties.setProperty("org.apache.ws.security.crypto.merlin.keystore.file", keyFile);        
        return cryptoProperties;        
    }

    //sign the document
    public void signDocument(Document doc){             
        try {   
            header.setMustUnderstand(true);
            sig.setSignatureAlgorithm(WSConstants.C14N_EXCL_OMIT_COMMENTS);
            sig.setSignatureAlgorithm(WSConstants.RSA);
            sig.setUserInfo(alias, password);
            sig.setKeyIdentifierType(WSConstants.BST_DIRECT_REFERENCE); 
            header.insertSecurityHeader(doc);
            sig.build(doc, crypto, header);
            time.build(doc, header);
            signedDoc = doc;
        } catch (Exception e) {         
            setLastError(e);
        }           
    }

    //get the signed document
    public Document getDocument(){      
        return signedDoc;       
    }

    //get the signed xml
    public String getXML(){
        return getStringFromDoc(getDocument()).trim();
    }

    //get last error
    public String getLastError(){
        return lastError;
    }

    //set last error
    private void setLastError(Throwable e){
        StringWriter sw = new StringWriter();
        e.printStackTrace(new PrintWriter(sw));
        lastError += "     NEXT ERROR     "+sw.toString();
        e.printStackTrace();
    }   

    //document to string
    public String getStringFromDoc(Document doc){
        try
        {
           DOMSource domSource = new DOMSource(doc);
           StringWriter writer = new StringWriter();
           StreamResult result = new StreamResult(writer);
           TransformerFactory tf = TransformerFactory.newInstance();
           Transformer transformer = tf.newTransformer();
           transformer.transform(domSource, result);
           writer.flush();
           return writer.toString();
        }
        catch(Exception e)
        {
           setLastError(e);
           return null;
        }
    }

    //string to document
    public Document xmlToDoc(String XML){   
        try {
            DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
            dbf.setNamespaceAware(true);
            DocumentBuilder db;     
            db = dbf.newDocumentBuilder();      
            InputSource is = new InputSource();
            is.setCharacterStream(new StringReader(XML));
            Document doc = db.parse(is);
        return doc;
        } catch (Exception e) {
            setLastError(e);
            return null;
        }       
    }

    //main
    public static void main(String[] args){
        /*testing, these test cases work
        String XML1 = "<?xml version='1.0' encoding='UTF-8'?><soap:Envelope xmlns:soap='http://schemas.xmlsoap.org/soap/envelope/' xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'> <soap:Body> <test1></test1> </soap:Body> </soap:Envelope>";  
        String XML2 = "<?xml version='1.0' encoding='UTF-8'?><soap:Envelope xmlns:soap='http://schemas.xmlsoap.org/soap/envelope/' xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'> <soap:Body> <test2></test2> </soap:Body> </soap:Envelope>";  
        new SoapSigner(XML1);       
        new SoapSigner(XML2);      
        */
    }
}

我在第二个循环中遇到的错误:

org.apache.wss4j.common.ext.WSSecurityException: No message with ID "noUserCertsFound" found in resource bundle "org/apache/xml/security/resource/xmlsecurity" at
org.apache.wss4j.dom.message.WSSecSignature.getSigningCerts(WSSecSignature.java:823) at
org.apache.wss4j.dom.message.WSSecSignature.prepare(WSSecSignature.java:167) at 
org.apache.wss4j.dom.message.WSSecSignature.build(WSSecSignature.java:375) at    
com.soapsigner.SoapSigner.signDocument(SoapSigner.java:91) at com.soapsigner.SoapSigner.<init>(SoapSigner.java:63) at 
sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) at 
sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source) at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source) at
java.lang.reflect.Constructor.newInstance(Unknown Source) at 
coldfusion.runtime.java.JavaProxy.CreateObject(JavaProxy.java:166) at 
coldfusion.runtime.java.JavaProxy.invoke(JavaProxy.java:80) at
coldfusion.runtime.CfJspPage._invoke(CfJspPage.java:2483) at 
cfindex2ecfm836201788.runPage(C:\ColdFusion11\cfusion\wwwroot\testsite\index.cfm:4) at
coldfusion.runtime.CfJspPage.invoke(CfJspPage.java:246) at    
coldfusion.tagext.lang.IncludeTag.handlePageInvoke(IncludeTag.java:734) at
coldfusion.tagext.lang.IncludeTag.doStartTag(IncludeTag.java:570) at 
coldfusion.filter.CfincludeFilter.invoke(CfincludeFilter.java:65) at
coldfusion.filter.IpFilter.invoke(IpFilter.java:45) at 
coldfusion.filter.ApplicationFilter.invoke(ApplicationFilter.java:487) at 
coldfusion.filter.RequestMonitorFilter.invoke(RequestMonitorFilter.java:42) at
coldfusion.filter.MonitoringFilter.invoke(MonitoringFilter.java:40) at
coldfusion.filter.PathFilter.invoke(PathFilter.java:141) at 
coldfusion.filter.LicenseFilter.invoke(LicenseFilter.java:30) at
coldfusion.filter.ExceptionFilter.invoke(ExceptionFilter.java:94) at
coldfusion.filter.ClientScopePersistenceFilter.invoke
(ClientScopePersistenceFilter.java:28) at
coldfusion.filter.BrowserFilter.invoke(BrowserFilter.java:38) at 
coldfusion.filter.NoCacheFilter.invoke(NoCacheFilter.java:58) at
coldfusion.filter.GlobalsFilter.invoke(GlobalsFilter.java:38) at 
coldfusion.filter.DatasourceFilter.invoke(DatasourceFilter.java:22) at
coldfusion.filter.CachingFilter.invoke(CachingFilter.java:62) at 
coldfusion.CfmServlet.service(CfmServlet.java:219) at 
coldfusion.bootstrap.BootstrapServlet.service(BootstrapServlet.java:89) at   
org.apache.catalina.core.ApplicationFilterChain.internalDoFilter
(ApplicationFilterChain.java:303) at    
org.apache.catalina.core.ApplicationFilterChain.doFilter
(ApplicationFilterChain.java:208) at   
coldfusion.monitor.event.MonitoringServletFilter.doFilter
(MonitoringServletFilter.java:42) at 
coldfusion.bootstrap.BootstrapFilter.doFilter(BootstrapFilter.java:46) at 
org.apache.catalina.core.ApplicationFilterChain.internalDoFilter
(ApplicationFilterChain.java:241) at
org.apache.catalina.core.ApplicationFilterChain.doFilter
(ApplicationFilterChain.java:208) at    
coldfusion.inspect.weinre.MobileDeviceDomInspectionFilter.doFilter
(MobileDeviceDomInspectionFilter.java:121) at   
coldfusion.bootstrap.BootstrapFilter.doFilter(BootstrapFilter.java:46) at 
org.apache.catalina.core.ApplicationFilterChain.internalDoFilter
(ApplicationFilterChain.java:241) at 
org.apache.catalina.core.ApplicationFilterChain.doFilter
(ApplicationFilterChain.java:208) at 
org.apache.catalina.core.StandardWrapperValve.invoke
(StandardWrapperValve.java:220) at 
org.apache.catalina.core.StandardContextValve.invoke
(StandardContextValve.java:122) at 
org.apache.catalina.authenticator.AuthenticatorBase.invoke
(AuthenticatorBase.java:501) at 
org.apache.catalina.core.StandardHostValve.invoke
(StandardHostValve.java:170) at 
org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:98) at
org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:116) at
org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:422) at 
org.apache.coyote.ajp.AjpProcessor.process(AjpProcessor.java:198) at 
org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process
(AbstractProtocol.java:607) at 
org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:313) at 
java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source) at 
java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source) at 
java.lang.Thread.run(Unknown Source)

根据我加载罐子的方式,我也得到了这个错误:

org.apache.wss4j.common.ext.WSSecurityException: No certificates for user 1 were found for signature
    at org.apache.wss4j.dom.message.WSSecSignature.getSigningCerts(WSSecSignature.java:823)
    at org.apache.wss4j.dom.message.WSSecSignature.prepare(WSSecSignature.java:167)
    at org.apache.wss4j.dom.message.WSSecSignature.build(WSSecSignature.java:375)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    at java.lang.reflect.Method.invoke(Unknown Source)
    at coldfusion.runtime.StructBean.invoke(StructBean.java:508)
    at coldfusion.runtime.CfJspPage._invoke(CfJspPage.java:2553)
    at cfindex2ecfm836201788.runPage(C:\ColdFusion11\cfusion\wwwroot\testsite\index.cfm:36)
    at coldfusion.runtime.CfJspPage.invoke(CfJspPage.java:246)
    at coldfusion.tagext.lang.IncludeTag.handlePageInvoke(IncludeTag.java:734)
    at coldfusion.tagext.lang.IncludeTag.doStartTag(IncludeTag.java:570)
    at coldfusion.filter.CfincludeFilter.invoke(CfincludeFilter.java:65)
    at coldfusion.filter.IpFilter.invoke(IpFilter.java:45)
    at coldfusion.filter.ApplicationFilter.invoke(ApplicationFilter.java:487)
    at coldfusion.filter.RequestMonitorFilter.invoke(RequestMonitorFilter.java:42)
    at coldfusion.filter.MonitoringFilter.invoke(MonitoringFilter.java:40)
    at coldfusion.filter.PathFilter.invoke(PathFilter.java:141)
    at coldfusion.filter.LicenseFilter.invoke(LicenseFilter.java:30)
    at coldfusion.filter.ExceptionFilter.invoke(ExceptionFilter.java:94)
    at coldfusion.filter.ClientScopePersistenceFilter.invoke(ClientScopePersistenceFilter.java:28)
    at coldfusion.filter.BrowserFilter.invoke(BrowserFilter.java:38)
    at coldfusion.filter.NoCacheFilter.invoke(NoCacheFilter.java:58)
    at coldfusion.filter.GlobalsFilter.invoke(GlobalsFilter.java:38)
    at coldfusion.filter.DatasourceFilter.invoke(DatasourceFilter.java:22)
    at coldfusion.filter.CachingFilter.invoke(CachingFilter.java:62)
    at coldfusion.CfmServlet.service(CfmServlet.java:219)
    at coldfusion.bootstrap.BootstrapServlet.service(BootstrapServlet.java:89)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:303)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
    at coldfusion.monitor.event.MonitoringServletFilter.doFilter(MonitoringServletFilter.java:42)
    at coldfusion.bootstrap.BootstrapFilter.doFilter(BootstrapFilter.java:46)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:241)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
    at coldfusion.inspect.weinre.MobileDeviceDomInspectionFilter.doFilter(MobileDeviceDomInspectionFilter.java:121)
    at coldfusion.bootstrap.BootstrapFilter.doFilter(BootstrapFilter.java:46)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:241)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:220)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:122)
    at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:501)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:170)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:98)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:116)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:422)
    at org.apache.coyote.ajp.AjpProcessor.process(AjpProcessor.java:198)
    at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:607)
    at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:313)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
    at java.lang.Thread.run(Unknown Source)

更改org.apache.xml.security.Init后,这些是第一个循环后的错误:

org.apache.wss4j.common.ext.WSSecurityException: No certificates for user 1 were found for signature at
org.apache.wss4j.dom.message.WSSecSignature.getSigningCerts(WSSecSignature.java:823) at
org.apache.wss4j.dom.message.WSSecSignature.prepare(WSSecSignature.java:167) at
org.apache.wss4j.dom.message.WSSecSignature.build(WSSecSignature.java:375) at
com.soapsigner.SoapSigner.signDocument(SoapSigner.java:93) at com.soapsigner.SoapSigner.<init>(SoapSigner.java:65) at
 sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) at
sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source) at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source) at
java.lang.reflect.Constructor.newInstance(Unknown Source) at
coldfusion.runtime.java.JavaProxy.CreateObject(JavaProxy.java:166) at
coldfusion.runtime.java.JavaProxy.invoke(JavaProxy.java:80) at 
coldfusion.runtime.CfJspPage._invoke(CfJspPage.java:2483) at
cfindex2ecfm836201788.runPage(C:\ColdFusion11\cfusion\wwwroot\testsite\index.cfm:60) at coldfusion.runtime.CfJspPage.invoke(CfJspPage.java:246) at
coldfusion.tagext.lang.IncludeTag.handlePageInvoke(IncludeTag.java:734) at
coldfusion.tagext.lang.IncludeTag.doStartTag(IncludeTag.java:570) at coldfusion.filter.CfincludeFilter.invoke(CfincludeFilter.java:65) at
 coldfusion.filter.IpFilter.invoke(IpFilter.java:45) at
 coldfusion.filter.ApplicationFilter.invoke(ApplicationFilter.java:487) at
 coldfusion.filter.RequestMonitorFilter.invoke(RequestMonitorFilter.java:42) at
 coldfusion.filter.MonitoringFilter.invoke(MonitoringFilter.java:40) at
 coldfusion.filter.PathFilter.invoke(PathFilter.java:141) at
 coldfusion.filter.LicenseFilter.invoke(LicenseFilter.java:30) at 
 coldfusion.filter.ExceptionFilter.invoke(ExceptionFilter.java:94) at
    coldfusion.filter.ClientScopePersistenceFilter.invoke(ClientScopePersistenceFilter.java:28) at 
coldfusion.filter.BrowserFilter.invoke(BrowserFilter.java:38) at
coldfusion.filter.NoCacheFilter.invoke(NoCacheFilter.java:58) at
coldfusion.filter.GlobalsFilter.invoke(GlobalsFilter.java:38) at 
coldfusion.filter.DatasourceFilter.invoke(DatasourceFilter.java:22) at
coldfusion.filter.CachingFilter.invoke(CachingFilter.java:62) at 
coldfusion.CfmServlet.service(CfmServlet.java:219) at 
coldfusion.bootstrap.BootstrapServlet.service(BootstrapServlet.java:89) at        
org.apache.catalina.core.ApplicationFilterChain.internalDoFilter
(ApplicationFilterChain.java:303) at     
org.apache.catalina.core.ApplicationFilterChain.doFilter
(ApplicationFilterChain.java:208) at 
coldfusion.monitor.event.MonitoringServletFilter.doFilter
(MonitoringServletFilter.java:42) at 
coldfusion.bootstrap.BootstrapFilter.doFilter(BootstrapFilter.java:46) at    
org.apache.catalina.core.ApplicationFilterChain.internalDoFilter
(ApplicationFilterChain.java:241) at    
org.apache.catalina.core.ApplicationFilterChain.doFilter
(ApplicationFilterChain.java:208) at 
coldfusion.inspect.weinre.MobileDeviceDomInspectionFilter.doFilter
(MobileDeviceDomInspectionFilter.java:121) at 
coldfusion.bootstrap.BootstrapFilter.doFilter
(BootstrapFilter.java:46) at 
org.apache.catalina.core.ApplicationFilterChain.internalDoFilter
ApplicationFilterChain.java:241) at 
org.apache.catalina.core.ApplicationFilterChain.doFilter
(ApplicationFilterChain.java:208) at 
org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:220) at 
org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:122) at
 org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:501) at
 org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:170) at 
 org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:98) at
 org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:116) at
 org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:422) at 
 org.apache.coyote.ajp.AjpProcessor.process(AjpProcessor.java:198) at 
org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:607) at 
 org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:313)
 at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source) at
 java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source) at
 java.lang.Thread.run(Unknown Source)

1 个答案:

答案 0 :(得分:0)

而不是:

static {org.apache.xml.security.Init.init(); }

尝试:

static {org.apache.wss4j.dom.WSSConfig.init(); }