3

我是 Web 服务的新手,我正在尝试使用 Eclipse Web 服务客户端使用 Axis2 自动生成的存根来创建 Java 客户端。以下是我的

client code. public static void main(String[] args) {
        new wsClient2().runService();
    }

public void runService(){

    try {

    CoreStub.COREEnvelopeRealTimeRequest req = new CoreStub.COREEnvelopeRealTimeRequest();
    CoreStub.COREEnvelopeRealTimeResponse res = new CoreStub.COREEnvelopeRealTimeResponse();

    req.setCORERuleVersion("2.2.0");
    req.setPayload("....some data over here...... Can't disclose");
    req.setPayloadID("..payload id goes here... can't disclose");
    req.setPayloadType("X12_276_Request_005010X212");

    RealTimeMode rtm = new RealTimeMode();
    rtm.setRealTimeMode("RealTime");
    req.setProcessingMode(rtm);
    req.setReceiverID("myreceiverid");
    req.setSenderID("mysenderid");
    req.setTimeStamp("2015-04-14 10:27:47");



    HttpTransportProperties.Authenticator basicAuthentication = new HttpTransportProperties.Authenticator();
    basicAuthentication.setUsername("myusername");
    basicAuthentication.setPassword("mypassword");
    basicAuthentication.setPreemptiveAuthentication(true);

    CoreStub _stub = new CoreStub();
    ServiceClient clientservice = _stub._getServiceClient();


    _stub._getServiceClient().getOptions().setProperty(org.apache.axis2.transport.http.HTTPConstants.AUTHENTICATE, basicAuthentication);
    _stub._getServiceClient().getOptions().setProperty(org.apache.axis2.transport.http.HTTPConstants.CHUNKED, "false");


    OMFactory factory2 = OMAbstractFactory.getOMFactory();

    OMNamespace SecurityElementNamespace = factory2.createOMNamespace("http://schemas.xmlsoap.org/ws/2002/12/secext", "wsse");

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

    //OMElement usernameEl = factory2.createOMElement(new QName("", "Username", "wsse"), null);
    OMElement usernameEl = factory2.createOMElement(new QName("", "Username", "wsse"), null);

    usernameEl.setText("myusername");

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

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

    usernameTokenEl.addChild(usernameEl);
    usernameTokenEl.addChild(passwordEl);

    omSecurityElement.addChild(usernameTokenEl);

    clientservice.addHeader(omSecurityElement);

    try {
        DisableSSLCertificateCheck();

    } catch (Exception e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

    System.out.println("Test");
    //System.out.println(clientservice.getLastOperationContext().getMessageContext("Out").getEnvelope().toString());

    System.out.println("Request created as: " + req.toString());

    res = _stub.realTimeTransaction(req);

    System.out.println("Version : " + res.getCORERuleVersion());
    System.out.println("Error Code :" + res.getErrorCode());
    System.out.println("Error Message:" + res.getErrorMessage());
    //System.out.println("Pay Load :" + res.getPayload());
    //System.out.println("Pay Load Type :" + res.getPayloadType());
    System.out.println("Receiver Id :" + res.getReceiverID());
    System.out.println("Sender Id :" + res.getSenderID());

    } catch (Exception e) {
        e.printStackTrace();

    }
}

我得到以下异常

java.lang.IllegalArgumentException:无法在 org.apache.axiom.om.impl.llom.OMElementImpl.handleNamespace(OMElementImpl.java:186) 在 org.apache.axiom.om.impl 中创建具有空命名空间名称的前缀元素。 llom.OMElementImpl.(OMElementImpl.java:161) at org.apache.axiom.om.impl.llom.factory.OMLinkedListImplFactory.createOMElement(OMLinkedListImplFactory.java:126) at org.myownpackage.www.soap.wsdl.wsClient2.runService (wsClient2.java:81) 在 org.myownpackage.www.soap.wsdl.wsClient2.main(wsClient2.java:37)

在以下行中,不允许使用空白字符串。我应该有什么来解决这个问题

  OMElement usernameEl = factory2.createOMElement(
                               new QName("", "Username", "wsse"), null
                             );

更快的帮助将不胜感激。先感谢您。

4

2 回答 2

1

您是否尝试将用户名和密码节点包装在用户令牌节点中,如this question

此外,查看oasis 文档,其中建议:

<wsse:UsernameToken ...>
  <wsse:Username> ... </wsse:Username>
  <wsse:Password Type="..."> ... </wsse:Password>
  ...
</wsse:UsernameToken>
于 2015-05-01T14:14:13.223 回答
0

你不应该使用

new QName("", "Username", "wsse")

用于生成没有显式设置为 XML 标记的 xmlns。相反,您应该将 Username 的命名空间值设置为与其父 UserToken 所使用的完全相同。当生成 XML 时,将不考虑这些重合的命名空间,并且将生成子元素,而不会将任何 xmlns 部分生成到标记中,完全符合您的要求。因此,由axis2:wsdl2code生成的存根类中的以下脏代码帮助了我:

    public void addWsSecurityHeader(String wsUser, String wsPass)
        {

    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);
            omSecurityElement.addAttribute("xmlns:wsu", "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-%20wssecurity-utility-1.0.xsd",  null);


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

    OMElement omuserName = omFactory.createOMElement(new QName("http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd","Username", "wsse"), null);
    omuserName.setText(wsUser);

    OMElement omPassword = omFactory.createOMElement(new QName("http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd","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(wsPass);

    omusertoken.addChild(omuserName);
    omusertoken.addChild(omPassword);
    omSecurityElement.addChild(omusertoken);
    this._getServiceClient().addHeader(omSecurityElement);
}

但是我还没有找到如何为同一个标签设置几个 xmlns,所以我做了一个肮脏的黑客来创建第二个 xmlns 作为标签属性。可能是我错了,但它奏效了。

于 2019-01-31T15:31:17.193 回答