当前位置: 首页 > 知识库问答 >
问题:

使用Java中的SOAP web服务(具有安全性)

公孙宸
2023-03-14

我想将以下XML发布到web服务:-

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:v1="http://<some_site>/retail/schema/inventory/orderlookupservice/v1">
      <soapenv:Header>
            <Security xmlns:wsu="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd" xmlns="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd">
                  <UsernameToken>
                        <Username>xyzUser</Username>
                        <Password>xyzPass</Password>
                  </UsernameToken>
            </Security>
      </soapenv:Header>
      <soapenv:Body>
            <v1:OrderSearchRequest>
                  <v1:RETAS400OrderNumber>1</v1:RETAS400OrderNumber>
            </v1:OrderSearchRequest>
      </soapenv:Body>
</soapenv:Envelope>

我希望得到以下响应XML:-

<?xml version="1.0" encoding="UTF-8"?>
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">
      <soapenv:Header/>
      <soapenv:Body>
            <v1:OrderSearchResponse xmlns:v1="http://<some_site>/retail/schema/inventory/orderlookupservice/v1">
                  <v1:error>
                        <v1:errorCode>ERRODR01</v1:errorCode>
                        <v1:errorMessage>Order Number is Invalid</v1:errorMessage>
                  </v1:error>
            </v1:OrderSearchResponse>
      </soapenv:Body>
</soapenv:Envelope>
<?xml version="1.0" encoding="UTF-8"?>
<env:Envelope xmlns:env="http://schemas.xmlsoap.org/soap/envelope/">
   <env:Header>
   </env:Header>
   <env:Body>
      <env:Fault>
         <faultcode>env:Server
         </faultcode>
         <faultstring>
         </faultstring>
         <detail xmlns:fault="http://www.vordel.com/soapfaults" fault:type="faultDetails">
         </detail>
      </env:Fault>
   </env:Body>
</env:Envelope>
public class RequestInitiation {

    public static void main(String args[]) {

        try {
            // Create SOAP Connection
            SOAPConnectionFactory soapConnectionFactory = SOAPConnectionFactory.newInstance();
            SOAPConnection soapConnection = soapConnectionFactory.createConnection();

            // Send SOAP Message to SOAP Server
            String url = "<service_endpoint>";
            SOAPMessage soapResponse = soapConnection.call(createSOAPRequest(), url);

            // Process the SOAP Response
            printSOAPResponse(soapResponse);

            soapConnection.close();
        } catch (Exception e) {
            System.err.println("Error occurred while sending SOAP Request to Server");
            e.printStackTrace();
        }
    }

    private static SOAPMessage createSOAPRequest() throws Exception {

        MessageFactory messageFactory = MessageFactory.newInstance();
        SOAPMessage soapMessage = messageFactory.createMessage();
        SOAPPart soapPart = soapMessage.getSOAPPart();

        String serverURI = "http://<some_site>/retail/schema/inventory/orderlookupservice/v1";

        SOAPEnvelope envelope = soapPart.getEnvelope();
        envelope.addNamespaceDeclaration("v1", serverURI);

        SOAPHeader header = envelope.getHeader();

        SOAPElement security =
                header.addChildElement("Security", "", "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd");
        security.addAttribute(new QName("xmlns:wsu"), "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd");

        SOAPElement usernameToken =
                security.addChildElement("UsernameToken", "");

        SOAPElement username =
                usernameToken.addChildElement("Username", "");
        username.addTextNode("xyzUser");

        SOAPElement password =
                usernameToken.addChildElement("Password", "");
        password.addTextNode("xyzPass");

        SOAPBody soapBody = envelope.getBody();

        SOAPElement soapBodyElem = soapBody.addChildElement("OrderSearchRequest", "v1");
        SOAPElement soapBodyElem1 = soapBodyElem.addChildElement("RETAS400OrderNumber", "v1");
        soapBodyElem1.addTextNode("1");

        soapMessage.setProperty(SOAPMessage.CHARACTER_SET_ENCODING, "UTF-8");

        soapMessage.saveChanges();

        /* Print the request message */
        System.out.print("Request SOAP Message = ");
        soapMessage.writeTo(System.out);
        System.out.println();

        return soapMessage;
    }

    private static void printSOAPResponse(SOAPMessage soapResponse) throws Exception {
        TransformerFactory transformerFactory = TransformerFactory.newInstance();
        Transformer transformer = transformerFactory.newTransformer();
        Source sourceContent = soapResponse.getSOAPPart().getContent();
        System.out.print("\nResponse SOAP Message = ");
        StreamResult result = new StreamResult(System.out);
        transformer.transform(sourceContent, result);
    }
}
public class PostSOAPRequest {

    public static String post() throws Exception {

        HttpClient client = HttpClientBuilder.create().build();
        HttpPost post = new HttpPost("<service_endpoint>");
        String xml = "<soapenv:Envelope xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:v1=\"http://<some_site>/retail/schema/inventory/orderlookupservice/v1\"><soapenv:Header><Security xmlns:wsu=\"http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd\" xmlns=\"http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd\"><UsernameToken><Username>xyzUser</Username><Password>xyzPass</Password></UsernameToken></Security></soapenv:Header><soapenv:Body><v1:OrderSearchRequest><v1:RETAS400OrderNumber>1</v1:RETAS400OrderNumber></v1:OrderSearchRequest></soapenv:Body></soapenv:Envelope>";
        HttpEntity entity = new ByteArrayEntity(xml.getBytes("UTF-8"));
        post.setEntity(entity);
        HttpResponse response = client.execute(post);
        String result = EntityUtils.toString(response.getEntity());
        return result;
    }

    public static void main(String[] args) {
        try {
            System.out.println(post()); 
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}

共有1个答案

宁欣怿
2023-03-14

有两个层次的操作发生:1。应用凭据2。发送消息。这种两步机制在SAAJ中可用。

 类似资料:
  • 本文,我们讲介绍如何在Linux和Windows上配置Minio服务使用TLS。 1. 前提条件 下载Minio server 这里 2. 配置已存在的证书 如果你已经有私钥和公钥证书,你需要将它们拷贝到Minio的config/certs文件夹,分别取名为private.key 和 public.crt。 如果这个证书是被证书机构签发的,public.crt应该是服务器的证书,任何中间体的证书以

  • Navicat 为你的服务器提供安全性管理工具。你可以新建、编辑、删除用户、授予或撤消在已选择的数据库及它们的数据库对象的权限。点击 来打开 用户 的对象列表。对象列表窗格显示全部存在于服务器中的用户。

  • 我有两个微服务,例如A和B。微服务B有剩余的enpoint,必须只能从微服务A访问。如何限制微服务之间的访问?如果可能的话,最佳做法是什么? 我正在使用Spring Cloud Security(oAuth2, jwt)。

  • 问题内容: 我有X.509证书保护的远程Web服务。 我生成了Web服务客户端的内容(使用jax-ws),但是需要配置是否用于证书的用途。 我应该如何进行? 我想我应该在本地信任的密钥库中注册证书,并且它们设置如下所示: 但是尚不清楚应提供哪些数据作为参数。 请帮忙。 谢谢。 问题答案: 密钥库属性定义用于向服务器标识您的证书: 这是带有x509证书的Java密钥库。您可以使用Java程序keyt

  • 答案可能涵盖所有框架,但我对SpringMVC案例特别感兴趣。我正在重构一个访问内部数据库和远程服务的服务层。这些方法应该是事务性的,它们需要来自远程服务的数据。下面是类似的伪代码: 这样更容易实现。但是有许多缺点,例如当远程服务调用失败时不必要地创建和回滚事务,由于远程服务调用而导致的事务更长,并且可能更复杂。我正在考虑将服务调用移动到单独的非事务性方法,并调用事务性方法,如下面的代码段所示 假

  • Navicat 提供强大的工具让你管理服务器用户帐号和数据库对象的权限。所有用户和权限的信息都保存于服务器。在主窗口中,点击 “用户”或 “角色”来打开用户或角色的对象列表。