【问题标题】:Axis2 Adb Client Basic Auth over https secure urlAxis2 Adb Client Basic Auth over https 安全 url
【发布时间】:2012-12-22 05:38:55
【问题描述】:

我在 Stackoverflow 上搜索了这个问题,发现了一些类似的问题,但没有一个能解决我的问题。我已经编译了所有“建议”的解决方案,但没有任何效果:-( 我有一个 wsdl,我使用 adb 客户端(Axis 2)生成了客户端代码。 wsdl 表示此请求将通过 Https url 发送。我能够使用 wsdl to java 成功创建存根。但是我不确定如何进行基本身份验证。告诉我详细信息的文档还说我的用户名和密码应该使用 Base64 编码。

使用的身份验证方法是 HTTP Basic 。用户名和 密码需要以 base64 格式编码 – UTF8 字符 设置。

示例:用户名:密码 = “VXNlcm5hbWU6UGFzc3dvcmQ=”

顺便说一句,我已经在 SOAP UI 中尝试过这个 wsdl,并且我得到了正确的响应,但是有些我的 java 代码将无法工作

现在这里是 wsdl

<?xml version="1.0" encoding="UTF-8"?>
<definitions targetNamespace="urn:OTSB2B" xmlns="http://schemas.xmlsoap.org/wsdl/" xmlns:apachesoap="http://xml.apache.org/xml-soap" xmlns:impl="urn:OTSB2B" xmlns:intf="urn:OTSB2B" xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/" xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/" xmlns:wsdlsoap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
    <wsdl:types>
        <schema targetNamespace="urn:OTSB2B" xmlns="http://www.w3.org/2001/XMLSchema" xmlns:urn="urn:OTSB2B">
            <simpleType name="tn">
                <restriction base="string">
                    <length value="10"/>
                </restriction>
            </simpleType>
            <simpleType name="prov">
                <restriction base="string">
                    <length value="2"/>
                    <enumeration value="on"/>
                    <enumeration value="qc"/>
                </restriction>
            </simpleType>
            <element name="getPresaleByTN">
                <complexType>
                    <sequence>
                        <element name="tn" type="urn:tn"/>
                        <element name="prov" type="urn:prov"/>
                    </sequence>
                </complexType>
            </element>
            <element name="getPresaleByTNReturn" type="xsd:string"/>
            <element name="isAlive"/>
            <element name="isAliveReturn" type="xsd:boolean"/>
        </schema>
    </wsdl:types>
    <message name="isAliveRequest">
        <part element="impl:isAlive" name="isAlive"/>
    </message>
    <message name="getPresaleByTNRequest">
        <part element="impl:getPresaleByTN" name="getPresaleByTN"/>
    </message>
    <message name="isAliveResponse">
        <part element="impl:isAliveReturn" name="isAliveReturn"/>
    </message>
    <message name="getPresaleByTNResponse">
        <part element="impl:getPresaleByTNReturn" name="getPresaleByTNReturn"/>
    </message>
    <portType name="GetPresaleByTN">
        <operation name="getPresaleByTN">
            <input message="impl:getPresaleByTNRequest" name="getPresaleByTNRequest"/>
            <output message="impl:getPresaleByTNResponse" name="getPresaleByTNResponse"/>
        </operation>
        <operation name="isAlive">
            <input message="impl:isAliveRequest" name="isAliveRequest"/>
            <output message="impl:isAliveResponse" name="isAliveResponse"/>
        </operation>
    </portType>
    <binding name="DominoSoapBinding" type="impl:GetPresaleByTN">
        <wsdlsoap:binding style="document" transport="http://schemas.xmlsoap.org/soap/http"/>
        <operation name="getPresaleByTN">
            <wsdlsoap:operation soapAction=""/>
            <input name="getPresaleByTNRequest">
                <wsdlsoap:body use="literal"/>
            </input>
            <output name="getPresaleByTNResponse">
                <wsdlsoap:body use="literal"/>
            </output>
        </operation>
        <operation name="isAlive">
            <wsdlsoap:operation soapAction=""/>
            <input name="isAliveRequest">
                <wsdlsoap:body use="literal"/>
            </input>
            <output name="isAliveResponse">
                <wsdlsoap:body use="literal"/>
            </output>
        </operation>
    </binding>
    <service name="GetPresaleByTNService">
        <port binding="impl:DominoSoapBinding" name="Domino">
            <wsdlsoap:address location="https://b2b.ivv.bell.ca/ots-qualification-service-tn"/>
        </port>
    </service>
</definitions>

我试过这个:

GetPresaleByTNServiceStub stub = new GetPresaleByTNServiceStub();
            ServiceClient client = stub._getServiceClient();
            client.addStringHeader(new QName("userName"), "XXX");
            client.addStringHeader(new QName("password"), "YYYYYYYY");

            GetPresaleByTNServiceStub.GetPresaleByTN request = new GetPresaleByTN();
            Tn tn = new Tn();
            tn.setTn("4164390001");
            request.setTn(tn);
            request.setProv(Prov.on);

            GetPresaleByTNReturn response = stub.getPresaleByTN(request);
            System.out.println(response.getGetPresaleByTNReturn());

这给了我以下错误:

org.apache.axis2.AxisFault:添加字符串头失败,你必须 QName 的 namespaceURI 位于 org.apache.axis2.client.ServiceClient.addStringHeader(ServiceClient.java:434) 在 com.dinesh.bellAxis.App.main(App.java:30)

然后我尝试了这个

GetPresaleByTNServiceStub stub = new GetPresaleByTNServiceStub();
            ServiceClient client = stub._getServiceClient();

            HttpTransportProperties.Authenticator basicAuth = new HttpTransportProperties.Authenticator();
            basicAuth.setUsername("XXX");
            basicAuth.setPassword("CCCCC");
            basicAuth.setPreemptiveAuthentication(true);

            stub._getServiceClient().getOptions().setProperty(HTTPConstants.AUTHENTICATE, basicAuth);

            GetPresaleByTNServiceStub.GetPresaleByTN request = new GetPresaleByTN();
            Tn tn = new Tn();
            tn.setTn("4164390001");
            request.setTn(tn);
            request.setProv(Prov.on);

            GetPresaleByTNReturn response = stub.getPresaleByTN(request);
            System.out.println(response.getGetPresaleByTNReturn());

这给了我以下错误:

org.apache.axis2.AxisFault:传输级别信息不匹配 SOAP 消息命名空间 URI 位于 org.apache.axis2.AxisFault.makeFault(AxisFault.java:430) 在 org.apache.axis2.transport.TransportUtils.createSOAPMessage(TransportUtils.java:90) 在 org.apache.axis2.description.OutInAxisOperationClient.handleResponse(OutInAxisOperation.java:353) 在 org.apache.axis2.description.OutInAxisOperationClient.send(OutInAxisOperation.java:416) 在 org.apache.axis2.description.OutInAxisOperationClient.executeImpl(OutInAxisOperation.java:228) 在 org.apache.axis2.client.OperationClient.execute(OperationClient.java:163) 在 com.acn.client.GetPresaleByTNServiceStub.getPresaleByTN(GetPresaleByTNServiceStub.java:460)

接下来我尝试了这个:我认为这是不正确的,因为它是 WS 安全性而不是基本身份验证,但是我用尽了所有选项

GetPresaleByTNServiceStub stub = new GetPresaleByTNServiceStub();
            ServiceClient client = stub._getServiceClient();

            OMFactory omFactory = OMAbstractFactory.getOMFactory();
            OMElement omSecurityElement = omFactory.createOMElement(new QName( "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd", "Security", "wsse"), null);


            OMElement omusertoken = omFactory.createOMElement(new QName("http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd", "UsernameToken", "wsu"), null);

            OMElement omuserName = omFactory.createOMElement(new QName("", "Username", "wsse"), null);
            omuserName.setText("XXXX");

            OMElement omPassword = omFactory.createOMElement(new QName("", "Password", "wsse"), null);
            omPassword.addAttribute("Type","http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-username-token-profile-1.0#PasswordText",null );
            omPassword.setText("YYYYYYY");

            omusertoken.addChild(omuserName);
            omusertoken.addChild(omPassword);
            omSecurityElement.addChild(omusertoken);
            stub._getServiceClient().addHeader(omSecurityElement);

            GetPresaleByTNServiceStub.GetPresaleByTN request = new GetPresaleByTN();
            Tn tn = new Tn();
            tn.setTn("4164390001");
            request.setTn(tn);
            request.setProv(Prov.on);

            GetPresaleByTNReturn response = stub.getPresaleByTN(request);
            System.out.println(response.getGetPresaleByTNReturn());

这会产生以下错误:

线程“主”java.lang.IllegalArgumentException 中的异常:不能 创建一个带有空命名空间名称的前缀元素 org.apache.axiom.om.impl.llom.OMElementImpl.handleNamespace(OMElementImpl.java:186) 在 org.apache.axiom.om.impl.llom.OMElementImpl.(OMElementImpl.java:161) 在 org.apache.axiom.om.impl.llom.factory.OMLinkedListImplFactory.createOMElement(OMLinkedListImplFactory.java:126) 在 com.dinesh.bellAxis.App2.main(App2.java:37)

我不知道接下来要做什么,我已经检查了 Apache Axis2 上的所有文档,并在所有地方搜索过,但可以让代码正常工作。

任何建议

【问题讨论】:

  • 我也遇到了这个问题,因为我正在向 WCF 服务的服务发送soap响应并且我得到 org.apache.axis2.AxisFault: Failed to add string header, you must have namespaceURI for QName。任何可能解决此问题的工作将不胜感激。

标签: web-services https client axis2 axis


【解决方案1】:

代码片段 1 和 3 不起作用,因为它们试图创建对 SOAP(片段 1)或 XML(片段 3)无效的消息。无论如何,他们尝试将 SOAP 标头添加到消息中,这不是基本身份验证的目的。

代码片段 2 看起来正确。从异常的堆栈跟踪(更准确地说是handleResponse 方法的存在),您可以看到响应存在问题。错误消息可能表明响应的内容类型与响应中实际使用的 SOAP 版本不匹配。这意味着服务有问题,而不是客户端有问题。

【讨论】:

  • 谢谢安德烈亚斯。如果服务有问题,那么它就不能与 SOAP UI 工具一起使用。我使用 HttpTransportProperties.Authenticator 的第二个代码片段在理论上是正确的,过去当我尝试访问各种其他服务但错误传输级别信息与 org 的 SOAP 消息命名空间 URI 不匹配时,我对此没有任何问题。 apache.axis2.AxisFault.makeFault(AxisFault.java:430) 表明我的 SOAP 是 1.1 而服务接受 1.2/2.0,我应该尝试将 wsdl 转换为 2.0 吗?
  • 您的论点是有缺陷的,因为它隐含地假定 SOAPUI 在内容类型和响应的 SOAP 名称空间之间执行与 Axis2 相同的一致性检查。有什么证据吗?
【解决方案2】:

经过大量试验和错误并在 Oracle 网站上查看 SAAj 教程后,我找到了答案。

我可以使用它进行基本身份验证 字符串授权 = new sun.misc.BASE64Encoder().encode((“myUserName”+”:”+”myPassword”).getBytes()); headers.addHeader(“授权”, “基本” + 授权);

这是完整的教程 - http://www.javahabit.com/2014/10/17/quick-tutorial-saaj-api/

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-08-09
    • 2021-07-06
    • 1970-01-01
    • 1970-01-01
    • 2020-08-09
    • 2023-03-09
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多