JAX-WS:尝试添加安全头时获取DOMException - HIERARCHY_REQUEST_ERR

时间:2011-02-03 15:57:56

标签: soap weblogic jax-ws

当我在发送请求之前尝试将安全标头添加到SOAP信封时,我收到以下异常。

尝试检索消息时抛出异常(因此context.getMessage()抛出异常)。

当我使用SoapUI时,一切都按计划进行。我在客户端以及Web服务端使用JAXB2进行编组。

我已经没有想法......任何帮助都会受到赞赏!

谢谢!

P.S。当我在Web服务端取出安全检查时,异常仍然被抛出(并被捕获),但是请求通过,并且收到响应而没有任何问题。

这是我的代码:


public class SoapSecurityHandler implements SOAPHandler {

    private static Logger logger = Logger.getLogger(SoapSecurityHandler.class);

    private final String clntUserName = "my_username";
    private final String clntPassword = "my_password";

    public boolean handleMessage(SOAPMessageContext context) {
        logger.info("Enter handleMessage()");
        Boolean outboundProperty = (Boolean) context.get(MessageContext.MESSAGE_OUTBOUND_PROPERTY);
        if (outboundProperty.booleanValue()) {
            try {

                logger.info("Getting the message");
                SOAPMessage message = context.getMessage();

                logger.info("Getting the soappart");
                SOAPPart soapPart = message.getSOAPPart();

                logger.info("Getting the envelope");
                SOAPEnvelope envelope = soapPart.getEnvelope();

                logger.info("Getting the factory");
                SOAPFactory factory = SOAPFactory.newInstance();

                logger.info("Got the factory");
                QName name = new QName(
                        "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd",
                        "Security", "wsse");

                SOAPElement securityHeader = factory.createElement(name);
                SOAPElement usernameTokenElement = securityHeader.addChildElement("UsernameToken");

                SOAPElement usernameElement = usernameTokenElement.addChildElement("Username");
                usernameElement.addTextNode(clntUserName);

                SOAPElement pwdElement = usernameTokenElement.addChildElement("Password");
                pwdElement.addTextNode(clntPassword);
                logger.info("Added the username and password to the usernameToken element");

                SOAPHeader header = envelope.addHeader();
                logger.info("Added the header to the envelope");

                header.addChildElement(securityHeader);
                logger.info("Added the security header to the header");

            } catch (Exception e) {
                logger.error("Exception in handler: " + e);
                e.printStackTrace();
            }
        } else {
            logger.info("This is an inbound message");
        }
        logger.info("Exit handleMessage()");
        return true;
    }

    public boolean handleFault(SOAPMessageContext context) {
        return false;
    }

    public void close(MessageContext context) {
        //
    }

    public Set getHeaders() {
        return new HashSet();
    }
}

org.w3c.dom.DOMException: HIERARCHY_REQUEST_ERR: An attempt was made to insert a node where it is not permitted. 
    at org.apache.axiom.om.impl.dom.ParentNode.insertBefore(ParentNode.java:228)
    at org.apache.axiom.om.impl.dom.NodeImpl.appendChild(NodeImpl.java:240)
    at org.apache.axis2.saaj.SOAPPartImpl.appendChild(SOAPPartImpl.java:948)
    at com.sun.xml.bind.marshaller.SAX2DOMEx.startElement(SAX2DOMEx.java:176)
    at com.sun.xml.ws.message.AbstractMessageImpl.writeTo(AbstractMessageImpl.java:158)
    at com.sun.xml.ws.message.AbstractMessageImpl.readAsSOAPMessage(AbstractMessageImpl.java:193)
    at com.sun.xml.ws.handler.SOAPMessageContextImpl.getMessage(SOAPMessageContextImpl.java:84)
    at com.sun.xml.ws.handler.SOAPMessageContextImpl.getMessage(SOAPMessageContextImpl.java:77)
    at com.comcast.cet.web.service.routing.lb.f5.impl.SoapSecurityHandler.handleMessage(SoapSecurityHandler.java:40)
    at com.comcast.cet.web.service.routing.lb.f5.impl.SoapSecurityHandler.handleMessage(SoapSecurityHandler.java:1)
    at com.sun.xml.ws.handler.HandlerProcessor.callHandleMessage(HandlerProcessor.java:292)
    at com.sun.xml.ws.handler.HandlerProcessor.callHandlersRequest(HandlerProcessor.java:133)
    at com.sun.xml.ws.handler.ClientSOAPHandlerTube.callHandlersOnRequest(ClientSOAPHandlerTube.java:138)
    at com.sun.xml.ws.handler.HandlerTube.processRequest(HandlerTube.java:116)
    at com.sun.xml.ws.api.pipe.Fiber.__doRun(Fiber.java:598)
    at com.sun.xml.ws.api.pipe.Fiber._doRun(Fiber.java:557)
    at com.sun.xml.ws.api.pipe.Fiber.doRun(Fiber.java:542)
    at com.sun.xml.ws.api.pipe.Fiber.runSync(Fiber.java:439)
    at com.sun.xml.ws.client.Stub.process(Stub.java:248)
    at com.sun.xml.ws.client.sei.SEIStub.doProcess(SEIStub.java:135)
    at com.sun.xml.ws.client.sei.SyncMethodHandler.invoke(SyncMethodHandler.java:109)
    at com.sun.xml.ws.client.sei.SyncMethodHandler.invoke(SyncMethodHandler.java:89)
    at com.sun.xml.ws.client.sei.SEIStub.invoke(SEIStub.java:118)
    at $Proxy257.getWipInfoForSpecificPoolMember(Unknown Source)
    at com.comcast.cet.web.service.routing.lb.LoadBalancerMain.getWipInfo(LoadBalancerMain.java:74)
    at com.comcast.cet.web.service.routing.lb.LoadBalancerMain.getFarmStatus(LoadBalancerMain.java:42)
    at com.comcast.cet.web.controllers.rpc.RoutingServiceImpl.getFarmStatus(RoutingServiceImpl.java:95)
    at sun.reflect.GeneratedMethodAccessor130.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
    at java.lang.reflect.Method.invoke(Method.java:597)
    at com.google.gwt.user.server.rpc.RPC.invokeAndEncodeResponse(RPC.java:562)
    at com.google.gwt.user.server.rpc.RPC.invokeAndEncodeResponse(RPC.java:544)
    at com.google.gwt.user.server.rpc.RPC.invokeAndEncodeResponse(RPC.java:504)
    at com.comcast.cet.web.controllers.rpc.RoutingServiceRpcController.processCall(RoutingServiceRpcController.java:64)
    at com.google.gwt.user.server.rpc.RemoteServiceServlet.processPost(RemoteServiceServlet.java:243)
    at com.google.gwt.user.server.rpc.AbstractRemoteServiceServlet.doPost(AbstractRemoteServiceServlet.java:62)
    at com.comcast.cet.web.controllers.rpc.RoutingServiceRpcController.handleRequest(RoutingServiceRpcController.java:41)
    at sun.reflect.GeneratedMethodAccessor129.invoke(Unknown Source)
    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:176)
    at org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter.invokeHandlerMethod(AnnotationMethodHandlerAdapter.java:42
6)
    at org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter.handle(AnnotationMethodHandlerAdapter.java:414)
    at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:790)
    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:727)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:820)
    at weblogic.servlet.internal.StubSecurityHelper$ServletServiceAction.run(StubSecurityHelper.java:227)
    at weblogic.servlet.internal.StubSecurityHelper.invokeServlet(StubSecurityHelper.java:125)
    at weblogic.servlet.internal.ServletStubImpl.execute(ServletStubImpl.java:292)
    at weblogic.servlet.internal.TailFilter.doFilter(TailFilter.java:26)
    at weblogic.servlet.internal.FilterChainImpl.doFilter(FilterChainImpl.java:42)
    at weblogic.servlet.internal.RequestEventsFilter.doFilter(RequestEventsFilter.java:27)
    at weblogic.servlet.internal.FilterChainImpl.doFilter(FilterChainImpl.java:42)
    at weblogic.servlet.internal.WebAppServletContext$ServletInvocationAction.run(WebAppServletContext.java:3496)
    at weblogic.security.acl.internal.AuthenticatedSubject.doAs(AuthenticatedSubject.java:321)
    at weblogic.security.service.SecurityManager.runAs(Unknown Source)
    at weblogic.servlet.internal.WebAppServletContext.securedExecute(WebAppServletContext.java:2180)
    at weblogic.servlet.internal.WebAppServletContext.execute(WebAppServletContext.java:2086)
    at weblogic.servlet.internal.ServletRequestImpl.run(ServletRequestImpl.java:1406)
    at weblogic.work.ExecuteThread.execute(ExecuteThread.java:201)
    at weblogic.work.ExecuteThread.run(ExecuteThread.java:173)

2 个答案:

答案 0 :(得分:2)

好的,经过大量的试验和错误后,这就是我添加到pom.xml中解决问题的原因:

    <dependency>
        <groupId>org.apache.axis2</groupId>
        <artifactId>axis2-jaxws</artifactId>
        <version>1.5.4</version>
    </dependency>

Weblogic正在使用自己的impl jar,它们一如既往地过时了。

这是我的pom.xml文件中的所有axis2声明:

    <dependency>
        <groupId>org.apache.axis2</groupId>
        <artifactId>axis2</artifactId>
        <version>1.5.4</version>
    </dependency>

    <dependency>
        <groupId>org.apache.axis2</groupId>
        <artifactId>axis2-kernel</artifactId>
        <version>1.5.4</version>
    </dependency>

    <dependency>
        <groupId>org.apache.axis2</groupId>
        <artifactId>axis2-transport-local</artifactId>
        <version>1.5.4</version>
    </dependency>

    <dependency>
        <groupId>org.apache.axis2</groupId>
        <artifactId>axis2-transport-http</artifactId>
        <version>1.5.4</version>
    </dependency>

    <dependency>
        <groupId>org.apache.axis2</groupId>
        <artifactId>axis2-jaxws</artifactId>
        <version>1.5.4</version>
    </dependency>

    <dependency>
        <groupId>org.apache.ws.commons.axiom</groupId>
        <artifactId>axiom-api</artifactId>
        <version>1.2.10</version>
    </dependency>

    <dependency>
        <groupId>org.apache.ws.commons.axiom</groupId>
        <artifactId>axiom-impl</artifactId>
        <version>1.2.10</version>
    </dependency>

答案 1 :(得分:1)

从我的类路径中删除 axis2-jaxws-1.6.2.jar 文件后,我收到了同样的错误。

实际上 axis2-jaxws-1.6.2.jar 导致了另一个错误,所以我无法将其放回去寻找另一个解决方案。最后我通过从类路径中删除axis2-saaj-1.6.2.jar来修复此错误。

删除axis2-saaj-1.6.2.jar禁止使用 org.apache.axis2.saaj.SoapPartImpl 而不是 com.sun.xml.internal.messaging.saaj。 soap.ver1_1.SOAPPart1_1Impl 作为抽象类 javax.xml.soap.SoapPart 的扩展名。

这解决了我的问题。