我使用的是WSO2
EI ver 6.6.0
,并且我开发了一个负责对SOAP信封进行签名的中介器。
我正确使用的代码在JavaSE上运行,我已经测试过了。但当我编译它并将其部署在WSO2 EI
上时,它就不起作用了。
代码是从代码的这一部分抛出的。
private static Document sign(Document doc, String Reference) throws Exception {
DOMSignContext dsc = null;
String providerName = System.getProperty("jsr105Provider", "org.jcp.xml.dsig.internal.dom.XMLDSigRI");
Provider provider = (Provider) Class.forName(providerName).newInstance();
// Below line throw exception!
XMLSignatureFactory fac = XMLSignatureFactory.getInstance("DOM", provider);
.
.
.
return doc;
}
错误
java.lang.ClassCastException: class org.jcp.xml.dsig.internal.dom.DOMXMLSignatureFactory cannot be cast to class javax.xml.crypto.dsig.XMLSignatureFactory (org.jcp.xml.dsig.internal.dom.DOMXMLSignatureFactory is in module java.xml.crypto of loader 'platform'; javax.xml.crypto.dsig.XMLSignatureFactory is in unnamed module of loader org.eclipse.osgi.internal.loader.EquinoxClassLoader @657e3cda)
at javax.xml.crypto.dsig.XMLSignatureFactory.findInstance(XMLSignatureFactory.java:202)
at javax.xml.crypto.dsig.XMLSignatureFactory.getInstance(XMLSignatureFactory.java:250)
at x.signer.util.Utils.sign(Utils.java:246)
at x.signer.util.Utils.insertSignSoap(Utils.java:233)
at x.signer.util.Utils.insertSignSayad(Utils.java:197)
at x.signer.Signer.mediate(Signer.java:37)
at org.apache.synapse.mediators.ext.ClassMediator.mediate(ClassMediator.java:94)
at org.apache.synapse.mediators.AbstractListMediator.mediate(AbstractListMediator.java:109)
at org.apache.synapse.mediators.AbstractListMediator.mediate(AbstractListMediator.java:71)
at org.apache.synapse.mediators.base.SequenceMediator.mediate(SequenceMediator.java:158)
at org.apache.synapse.rest.Resource.process(Resource.java:331)
at org.apache.synapse.rest.API.process(API.java:441)
at org.apache.synapse.rest.RESTRequestHandler.apiProcess(RESTRequestHandler.java:135)
at org.apache.synapse.rest.RESTRequestHandler.dispatchToAPI(RESTRequestHandler.java:113)
at org.apache.synapse.rest.RESTRequestHandler.process(RESTRequestHandler.java:71)
at org.apache.synapse.core.axis2.Axis2SynapseEnvironment.injectMessage(Axis2SynapseEnvironment.java:327)
at org.apache.synapse.core.axis2.SynapseMessageReceiver.receive(SynapseMessageReceiver.java:98)
at org.apache.axis2.engine.AxisEngine.receive(AxisEngine.java:180)
at org.apache.synapse.transport.passthru.ServerWorker.processNonEntityEnclosingRESTHandler(ServerWorker.java:368)
at org.apache.synapse.transport.passthru.ServerWorker.processEntityEnclosingRequest(ServerWorker.java:427)
at org.apache.synapse.transport.passthru.ServerWorker.run(ServerWorker.java:182)
at org.apache.axis2.transport.base.threads.NativeWorkerPool$1.run(NativeWorkerPool.java:172)
at java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1128)
at java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:628)
at java.base/java.lang.Thread.run(Thread.java:834)
输入示例
<S:Envelope xmlns:S="http://schemas.xmlsoap.org/soap/envelope/">
<S:Body>
<InquiryCode xmlns="http://tempuri.org/">
<RequestCode>12345</RequestCode>
</InquiryCode>
</S:Body>
</S:Envelope>
我搜索了很多类似的问题和Stackoverflow问题,但大多数都没有答案,或者回答的问题对我来说不起作用,比如
https://groups.google.com/g/fusefabric/c/1no0A5akFag
当XMLSignatureFactory在WSO2-AM中介中使用时,如何修复ClassCastException?
将所需的javax.xml.crypto类jar捆绑在类中介器中。
要做到这一点,必须将类中介器创建为OSGI捆绑包,而不是jar文件。如果您的类中介器项目pom.xml文件中有以下内容;
<packaging>jar</packaging>
将此更改为;
<packaging>bundle</packaging>
然后,将javax.xml.crypto类jar绑定到类中介器。要做到这一点,请将以下内容添加到mavenbundle-plugin下的类中介pom.xml文件中,如下所示。
<plugin>
<groupId>org.apache.felix</groupId>
<artifactId>maven-bundle-plugin</artifactId>
<version>3.5.0</version>
<extensions>true</extensions>
<configuration>
<instructions>
-----------
-----------------------
<Import-Package>
*; resolution:=optional
</Import-Package>
<Embed-Dependency>
javax.xml.crypto.dsig
</Embed-Dependency>
<Embed-Transitive>true</Embed-Transitive>
</instructions>
</configuration>
</plugin>
现在构建类中介器,并添加创建的jar文件/dropins文件夹。请确保从/lib和/dropins文件夹中都删除了以前的类中介器jar。
添加类中介器后重新启动服务器。