Axis2 Adb客户端基本身份验证通过https安全URL

时间:2012-12-22 05:38:56

标签: web-services https client axis2 axis

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

  

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

     

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

BTW我在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)at at   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());

这会出现以下错误:

  

线程“main”中的异常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上的所有文档,并在各处搜索,但可以使代码生效。

任何建议

2 个答案:

答案 0 :(得分:0)

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

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

答案 1 :(得分:0)

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

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

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