我正在尝试在Oracle Web服务器上部署我的jsp站点,但是当我使用端口调用我的Web服务时,我收到以下错误: 严重:SAAJ0120:当一个标题已存在时无法添加标题
这是我的HeaderHandler的代码
public boolean handleMessage(SOAPMessageContext smc) {
Boolean outboundProperty = (Boolean) smc.get(MessageContext.MESSAGE_OUTBOUND_PROPERTY);
if (outboundProperty.booleanValue()) {
SOAPMessage message = smc.getMessage();
SOAPEnvelope envelope = null;
SOAPHeader header = null;
try {
envelope = smc.getMessage().getSOAPPart().getEnvelope();
header = envelope.addHeader();
SOAPElement security =
header.addChildElement("Security", "wsse", "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd");
//http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd
SOAPElement BinarySecurityToken =
security.addChildElement("BinarySecurityToken", "wsse");
BinarySecurityToken.addAttribute(new QName("xmlns:wsu"), "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd");
//EncodingType="SSHA" ValueType="AccessManagerSSOSecurityToken" wsu:Id="OAMToken"
BinarySecurityToken.addAttribute(new QName("EncodingType"), "SSHA");
BinarySecurityToken.addAttribute(new QName("ValueType"), "AccessManagerSSOSecurityToken");
BinarySecurityToken.addAttribute(new QName("wsu:Id"), "OAMToken");
BinarySecurityToken.addTextNode(token);
//message.writeTo(System.out);
//System.out.println("");
} catch (Exception e) {
try {
header = envelope.addHeader();
} catch (SOAPException e1) {
e1.printStackTrace();
}
//e.printStackTrace();
}
} else {
try {
//This handler does nothing with the response from the Web Service so
//we just print out the SOAP message.
SOAPMessage message = smc.getMessage();
//message.writeTo(System.out);
//System.out.println("");
} catch (Exception ex) {
ex.printStackTrace();
}
}
return outboundProperty;
}
编辑:
错误是
com.sun.xml.internal.messaging.saaj.soap.impl.EnvelopeImpl addHeader
SEVERE: SAAJ0120: Can't add a header when one is already present
com.sun.xml.internal.messaging.saaj.SOAPExceptionImpl: Can't add a header when one is already present.
at com.sun.xml.internal.messaging.saaj.soap.impl.EnvelopeImpl.addHeader(EnvelopeImpl.java:114)
at com.sun.xml.internal.messaging.saaj.soap.impl.EnvelopeImpl.addHeader(EnvelopeImpl.java:94)
at com.standardandpoors.wso.HeaderHandler.handleMessage(HeaderHandler.java:33)
at com.standardandpoors.wso.HeaderHandler.handleMessage(HeaderHandler.java:1)
at com.sun.xml.ws.handler.HandlerProcessor.callHandleMessage(HandlerProcessor.java:297)
at com.sun.xml.ws.handler.HandlerProcessor.callHandlersRequest(HandlerProcessor.java:138)
at com.sun.xml.ws.handler.ClientSOAPHandlerTube.callHandlersOnRequest(ClientSOAPHandlerTube.java:144)
at com.sun.xml.ws.handler.HandlerTube.processRequest(HandlerTube.java:115)
at com.sun.xml.ws.api.pipe.Fiber.__doRun(Fiber.java:892)
at com.sun.xml.ws.api.pipe.Fiber._doRun(Fiber.java:841)
at com.sun.xml.ws.api.pipe.Fiber.doRun(Fiber.java:804)
at com.sun.xml.ws.api.pipe.Fiber.runSync(Fiber.java:706)
at com.sun.xml.ws.client.Stub.process(Stub.java:385)
at com.sun.xml.ws.client.sei.SEIStub.doProcess(SEIStub.java:189)
at com.sun.xml.ws.client.sei.SyncMethodHandler.invoke(SyncMethodHandler.java:119)
at com.sun.xml.ws.client.sei.SyncMethodHandler.invoke(SyncMethodHandler.java:102)
at com.sun.xml.ws.client.sei.SEIStub.invoke(SEIStub.java:172)
at $Proxy125.findUserById(Unknown Source)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at weblogic.wsee.jaxws.spi.ClientInstanceInvocationHandler.invoke(ClientInstanceInvocationHandler.java:84)
at $Proxy120.findUserById(Unknown Source)
at com.standardandpoors.wso.IdmWSUtil.findUserById(IdmWSUtil.java:44)
at com.standardandpoors.idm.controller.JspRedirectionController.forgotPassword(JspRedirectionController.java:123)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at org.springframework.web.bind.annotation.support.HandlerMethodInvoker.invokeHandlerMethod(HandlerMethodInvoker.java:175)
at org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter.invokeHandlerMethod(AnnotationMethodHandlerAdapter.java:421)
at org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter.handle(AnnotationMethodHandlerAdapter.java:409)
at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:774)
at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:719)
at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:644)
at org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:560)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:751)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:844)
at weblogic.servlet.internal.StubSecurityHelper$ServletServiceAction.run(StubSecurityHelper.java:242)
at weblogic.servlet.internal.StubSecurityHelper$ServletServiceAction.run(StubSecurityHelper.java:216)
at weblogic.servlet.internal.StubSecurityHelper.invokeServlet(StubSecurityHelper.java:132)
at weblogic.servlet.internal.ServletStubImpl.execute(ServletStubImpl.java:338)
at weblogic.servlet.internal.ServletStubImpl.execute(ServletStubImpl.java:221)
at weblogic.servlet.internal.WebAppServletContext$ServletInvocationAction.wrapRun(WebAppServletContext.java:3284)
at weblogic.servlet.internal.WebAppServletContext$ServletInvocationAction.run(WebAppServletContext.java:3254)
at weblogic.security.acl.internal.AuthenticatedSubject.doAs(AuthenticatedSubject.java:321)
at weblogic.security.service.SecurityManager.runAs(SecurityManager.java:120)
at weblogic.servlet.provider.WlsSubjectHandle.run(WlsSubjectHandle.java:57)
at weblogic.servlet.internal.WebAppServletContext.doSecuredExecute(WebAppServletContext.java:2163)
at weblogic.servlet.internal.WebAppServletContext.securedExecute(WebAppServletContext.java:2089)
at weblogic.servlet.internal.WebAppServletContext.execute(WebAppServletContext.java:2074)
at weblogic.servlet.internal.ServletRequestImpl.run(ServletRequestImpl.java:1513)
at weblogic.servlet.provider.ContainerSupportProviderImpl$WlsRequestExecutor.run(ContainerSupportProviderImpl.java:254)
at weblogic.work.ExecuteThread.execute(ExecuteThread.java:256)
at weblogic.work.ExecuteThread.run(ExecuteThread.java:221)
编辑: 使用JDK 1.8存在问题,而不是1.7
答案 0 :(得分:23)
您可以先删除旧标题来替换标题。
if (envelope.getHeader() != null) {
envelope.getHeader().detachNode();
}
SOAPHeader header = envelope.addHeader();
http://docs.oracle.com/javaee/5/api/javax/xml/soap/SOAPEnvelope.html
答案 1 :(得分:10)
我终于能够解决这个问题了。我只是添加了一个if条件来检查标头是否为NULL,如果它只是NULL,那么我添加了标头,继续使用该程序。 这对我有用!
if(header == null){
header = envelope.addHeader();
}
答案 2 :(得分:1)
使用会不会更容易?
SOAPHeader header = envelope.getHeader();
答案 3 :(得分:0)
SOAPHeader header = envelope.getHeader();
if (header == null) {
header = add.addHeader();
}
header.addChildElement(wsSecurityElement);
我的个人选择是:分离先前的SOAPHeader可能会破坏代码,例如在处理程序链接的情况下,其他上游处理程序可能会将其元素添加到标头中。 空测试使代码向后兼容以前的JRE;否则,从addHeader切换到getHeader就足够了。