Java 使用Spring WS-Security添加签名时出错
我在应用程序上使用SpringWS-Security时遇到问题。目标是在我的XMLSOAP请求中包含签名、UsernameToken和加密。但我有一个错误:Java 使用Spring WS-Security添加签名时出错,java,spring,soap,spring-ws,Java,Spring,Soap,Spring Ws,我在应用程序上使用SpringWS-Security时遇到问题。目标是在我的XMLSOAP请求中包含签名、UsernameToken和加密。但我有一个错误: 12/01/16 14:41:41 - [main] ERROR org.springframework.ws.soap.security.wss4j.Wss4jSecurityInterceptor - Could not secure response: Error during encryption: ; nested exc
12/01/16 14:41:41 - [main] ERROR org.springframework.ws.soap.security.wss4j.Wss4jSecurityInterceptor - Could not secure response: Error during encryption: ; nested exception is org.apache.ws.security.WSSecurityException: Error during encryption:
org.springframework.ws.soap.security.wss4j.Wss4jSecuritySecurementException: Error during encryption: ; nested exception is org.apache.ws.security.WSSecurityException: Error during encryption:
at org.springframework.ws.soap.security.wss4j.Wss4jSecurityInterceptor.secureMessage(Wss4jSecurityInterceptor.java:523)
at org.springframework.ws.soap.security.AbstractWsSecurityInterceptor.handleRequest(AbstractWsSecurityInterceptor.java:203)
at org.springframework.ws.client.core.WebServiceTemplate.doSendAndReceive(WebServiceTemplate.java:580)
at org.springframework.ws.client.core.WebServiceTemplate.sendAndReceive(WebServiceTemplate.java:539)
at org.springframework.ws.client.core.WebServiceTemplate.marshalSendAndReceive(WebServiceTemplate.java:386)
at org.springframework.ws.client.core.WebServiceTemplate.marshalSendAndReceive(WebServiceTemplate.java:380)
at net.virtela.ws.pricing.client.VerizonPricingApiClient.submitOrderWithSpring(VerizonPricingApiClient.java:79)
at net.virtela.rest.api.test.NcopQueryRestApiTest.testVerizon(NcopQueryRestApiTest.java:465)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:47)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:44)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:26)
at org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:27)
at org.junit.rules.TestWatcher$1.evaluate(TestWatcher.java:55)
at org.junit.rules.RunRules.evaluate(RunRules.java:20)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:271)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:70)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:50)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:238)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:63)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:236)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:53)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:229)
at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:26)
at org.junit.runners.ParentRunner.run(ParentRunner.java:309)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:86)
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:459)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:678)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:382)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:192)
Caused by: org.apache.ws.security.WSSecurityException: Error during encryption:
at org.apache.ws.security.action.EncryptionAction.execute(EncryptionAction.java:93)
at org.apache.ws.security.handler.WSHandler.doSenderAction(WSHandler.java:202)
at org.springframework.ws.soap.security.wss4j.Wss4jHandler.doSenderAction(Wss4jHandler.java:120)
at org.springframework.ws.soap.security.wss4j.Wss4jSecurityInterceptor.secureMessage(Wss4jSecurityInterceptor.java:520)
... 35 more
Caused by: org.apache.ws.security.WSSecurityException: Cannot encrypt data
at org.apache.ws.security.message.WSSecEncrypt.encryptElement(WSSecEncrypt.java:493)
at org.apache.ws.security.message.WSSecEncrypt.doEncryption(WSSecEncrypt.java:406)
at org.apache.ws.security.message.WSSecEncrypt.encryptForRef(WSSecEncrypt.java:289)
at org.apache.ws.security.message.WSSecEncrypt.build(WSSecEncrypt.java:246)
at org.apache.ws.security.action.EncryptionAction.execute(EncryptionAction.java:91)
... 38 more
Caused by: org.apache.xml.security.c14n.CanonicalizationException: Element ns3:serviceRequest has a relative namespace: ns3="e2eiServiceRequest"
at org.apache.xml.security.c14n.implementations.Canonicalizer20010315.handleAttributesSubtree(Canonicalizer20010315.java:227)
at org.apache.xml.security.c14n.implementations.CanonicalizerBase.canonicalizeSubTree(CanonicalizerBase.java:295)
at org.apache.xml.security.c14n.implementations.CanonicalizerBase.engineCanonicalizeSubTree(CanonicalizerBase.java:203)
at org.apache.xml.security.c14n.implementations.CanonicalizerBase.engineCanonicalizeSubTree(CanonicalizerBase.java:121)
at org.apache.xml.security.c14n.Canonicalizer.canonicalizeSubtree(Canonicalizer.java:284)
at org.apache.xml.security.encryption.AbstractSerializer.serializeToByteArray(AbstractSerializer.java:123)
at org.apache.xml.security.encryption.XMLCipher.encryptData(XMLCipher.java:1047)
at org.apache.xml.security.encryption.XMLCipher.encryptData(XMLCipher.java:1027)
at org.apache.xml.security.encryption.XMLCipher.encryptElementContent(XMLCipher.java:798)
at org.apache.xml.security.encryption.XMLCipher.doFinal(XMLCipher.java:930)
at org.apache.ws.security.message.WSSecEncrypt.encryptElement(WSSecEncrypt.java:490)
... 42 more
顺便说一下,这是我使用的应用程序上下文:
<?xml version="1.0" encoding="UTF-8"?>
http://www.springframework.org/schema/beans/spring-beans.xsd“>
net.sample.ws.pricing.model.telco.ServiceRequest
net.sample.ws.pricing.model.telco.ServiceResponse
主要错误是:org.apache.xml.security.c14n.CanonicalizationException:元素ns3:serviceRequest具有相对命名空间:ns3=“e2eiServiceRequest”
有人能帮我解决这个问题吗
谢谢
<bean id="wsSecurityInterceptor" class="org.springframework.ws.soap.security.wss4j.Wss4jSecurityInterceptor">
<property name="securementActions" value="Encrypt Signature UsernameToken" />
<!-- UsernameToken -->
<property name="securementUsername" value="user1234" />
<property name="securementPassword" value="abc1234" />
<property name="securementPasswordType" value="PasswordText" />
<property name="securementUsernameTokenElements" value="Nonce Created" />
<!-- Signature -->
<property name="securementSignatureKeyIdentifier" value="X509KeyIdentifier" />
<property name="securementSignatureUser" value="1" />
<property name="securementSignatureCrypto" ref="cryptoFactory"/>
<!-- Encrypt -->
<property name="securementEncryptionUser" value="1"/>
<property name="securementEncryptionCrypto" ref="cryptoFactory"/>
</bean>
<bean id="cryptoFactory" class="org.springframework.ws.soap.security.wss4j.support.CryptoFactoryBean">
<property name="keyStorePassword" value="abc1234" />
<property name="keyStoreLocation" value="classpath:/certificate/telco.sample.net.jks" />
</bean>
<!-- Configuring Client -->
<bean id="messageFactory" class="org.springframework.ws.soap.saaj.SaajSoapMessageFactory"/>
<bean id="marshaller" class="org.springframework.oxm.jaxb.Jaxb2Marshaller">
<property name="classesToBeBound">
<list>
<value>net.sample.ws.pricing.model.telco.ServiceRequest</value>
<value>net.sample.ws.pricing.model.telco.ServiceResponse</value>
</list>
</property>
</bean>
<bean id="webServiceTemplate" class="org.springframework.ws.client.core.WebServiceTemplate">
<constructor-arg ref="messageFactory"/>
<property name="defaultUri" value="https://wsgateway.verizon.com:443/VRDOrderingService_v2.0r1"/>
<property name="marshaller" ref="marshaller" />
<property name="unmarshaller" ref="marshaller"/>
<property name="interceptors">
<list>
<ref bean="wsSecurityInterceptor"/>
</list>
</property>
</bean>