8

我正在尝试在 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

4

4 回答 4

26

您可以先拆下旧的标题来替换标题。

  if (envelope.getHeader() != null) {
     envelope.getHeader().detachNode();
  }
  SOAPHeader header = envelope.addHeader();

http://docs.oracle.com/javaee/5/api/javax/xml/soap/SOAPEnvelope.html

于 2014-03-27T07:40:59.710 回答
10

我终于能够解决这个问题。我只是添加了一个 if 条件来检查标题是否为 NULL,如果它只是 NULL,那么我添加了标题,否则继续程序。这对我有用!

            if(header == null){
                header = envelope.addHeader();
                }
于 2013-06-18T07:06:52.457 回答
3

使用起来不是更方便吗?

SOAPHeader header = envelope.getHeader();
于 2018-04-18T07:09:46.907 回答
1
SOAPHeader header = envelope.getHeader();
if (header == null) {
   header = add.addHeader();
}
header.addChildElement(wsSecurityElement);

是我个人的选择:分离以前的 SOAPHeader 可能会破坏代码,例如在处理程序链接的情况下,其他上游处理程序可能会将其元素添加到标头中。空测试使代码与以前的 JRE 向后兼容;否则从 addHeader 切换到 getHeader 就足够了。

于 2018-06-28T07:20:53.337 回答