【问题标题】:Consuming a web service in java and SOAPUI在 Java 和 SOAPUI 中使用 Web 服务
【发布时间】:2013-11-26 07:31:08
【问题描述】:

我正在使用 java 中的 web 服务。此服务使用 UserNameToken、Timestamp 和 Signature 来确保消息级别的安全性。我已获得 SoapUI 项目文件(xml)来尝试从服务中获取数据。在 SoapUI 中一切正常。

现在,当我尝试使用相同的soapUI 文件来生成工件时,我收到了一条消息"Receiver Policy falsified"。为什么我可以在soapUI中连接到服务,但我不能在java中?

所以我发现我没有使用传输层安全性发送密钥库。如何在 SOAP 请求中发送它。我在 SSL 设置中的 SOAP UI 中做了类似的设置,它在那里工作。

这是我在 java 中的 SOAP 请求代码

SOAPConnectionFactory soapConnectionFactory = SOAPConnectionFactory.newInstance();
SOAPConnection soapConnection = soapConnectionFactory.createConnection();

    String url = "https://abc.org/test/ApplicantInformationService"; // Test System Web Service URL

SSLSocketFactory sslSocketFactory = getSSLSocketFactory();

conn = urlOnly.openConnection();
if (conn instanceof HttpsURLConnection) {
     ((HttpsURLConnection) conn).setSSLSocketFactory(sslSocketFactory);
     ((HttpsURLConnection) conn).setRequestMethod("POST");
 } 

conn.setRequestProperty("Content-Type", "application/soap+xml;charset=UTF-8");    
conn.setDoOutput(true);
SOAPMessage message = createSOAPRequest(user);
ByteArrayOutputStream os1 = new ByteArrayOutputStream();
message.writeTo(os1);
String requestXml = new String(os1.toByteArray()); 

OutputStreamWriter out = new OutputStreamWriter(conn.getOutputStream());
out.write(requestXml);
out.flush();
if(out != null){
    out.close();
}               
String line = "";                
BufferedReader in = new BufferedReader(new InputStreamReader(conn.getInputStream()));
String responseNewWay = "";
while ((line = in.readLine()) != null) {
    responseNewWay = responseNewWay + line;
}
//SOAPMessage soapResponse = soapConnection.call(createSOAPRequest(user), url);
//soapConnection.close();
//ByteArrayOutputStream os = new ByteArrayOutputStream();
//soapResponse.writeTo(os);
//String responseXml = new String(os.toByteArray());

而SSLFactory的代码是这样的

  private static SSLSocketFactory getSSLSocketFactory() 
    throws KeyStoreException, NoSuchAlgorithmException, CertificateException,  UnrecoverableKeyException, IOException, KeyManagementException{
    SSLSocketFactory sslsf = null;                  
    String keyStoreFileName = Preference.portalDir() + "mydocs/keystore.jks";           

        String keyStorePath = ClassLoader.getSystemResource(keyStoreFileName).getPath();        
    String keyStoreType = "JKS";
        String keyStorePassword = "mypassword";
        String trustStoreFileName = Preference.portalDir() + "mydocs/keystore.jks";        

        String trustStorePath = ClassLoader.getSystemResource(trustStoreFileName).getPath();
        String trustStorePassword = "mypassword";
        String alias = "clientcertificate";

        Properties systemProps = System.getProperties();
        systemProps.put("javax.net.ssl.keyStore", keyStorePath);
        systemProps.put("javax.net.ssl.keyStorePassword", keyStorePassword);
        systemProps.put("javax.net.ssl.keyStoreType", keyStoreType);

        systemProps.put("javax.net.ssl.trustStore", trustStorePath);
        systemProps.put("javax.net.ssl.trustStoreType", "JKS");
        systemProps.put("javax.net.ssl.trustStorePassword", trustStorePassword);
        System.setProperties(systemProps); 

        KeyManager[] keyManagers = createKeyManagers(keyStoreFileName,keyStorePassword, alias);
        TrustManager[] trustManagers = createTrustManagers(trustStoreFileName, trustStorePassword);
        sslsf = initItAll(keyManagers, trustManagers);

        return sslsf;
    }

我得到的错误 SOAP 响应是这样的

<?xml version="1.0" encoding="UTF-8"?>
<soapenv:Envelope xmlns:soapenv="http://www.w3.org/2003/05/soap-envelope">
    <soapenv:Body>
        <soapenv:Fault>
            <soapenv:Code>
                <soapenv:Value>soapenv:Receiver</soapenv:Value>
            </soapenv:Code>
            <soapenv:Reason>
                <soapenv:Text xml:lang="en-US">Policy Falsified</soapenv:Text>
            </soapenv:Reason>
            <soapenv:Role>https://abc.org/test/ApplicantInformationService</soapenv:Role>
            <soapenv:Detail>
                <l7:policyResult
                    status="Service Not Found.  The request may have been sent to an invalid URL, or intended for an unsupported operation." xmlns:l7="http://www.layer7tech.com/ws/policy/fault"/>
            </soapenv:Detail>
        </soapenv:Fault>
    </soapenv:Body>

现在我收到此错误

Server returned HTTP response code: 500 for URL: https://abc.org/test/ApplicantInformationService

【问题讨论】:

  • 您是否定义了任何策略集?
  • 不,如果我使用我通过java代码创建的请求,在soapui中,当我应用“身份验证”时,它也可以工作。所以我不明白我将如何在我的 java 代码中应用身份验证。
  • 你的应用服务器是什么?
  • 你对比过SoapUI发送的soap消息和你代码发送的消息吗?
  • policyResult 由工具插入,见layer7tech.com/tutorials/identityaccessmanagement。对方有系统管理员可以与之交谈吗?

标签: java web-services soapui


【解决方案1】:

也许你必须在 url 字符串的末尾附加“?wsdl”:

String url = "https://abc.org/test/ApplicantInformationService?wsdl";

【讨论】:

    【解决方案2】:

    您能否发布您的 createSOAPRequest 方法代码?我认为您的请求没有任何证书问题,因为您从服务器接收到有效的 SOAP 响应,看起来您的安全标头有问题,请检查您的请求 SOAP 消息对于 Timestamp 元素,可能您为 created 或 expires 标签发送了不正确的值,并且服务器拒绝了您的请求。

    【讨论】:

      【解决方案3】:

      看来您需要设置 SSL 上下文才能在 Java 代码中通过 HTTPS 发送 SOAP。为此,您可能需要遵循以下两个示例:

      1. http://blog.hexican.com/2010/12/sending-soap-messages-through-https-using-saaj/
      2. http://ruelajingsantiago.wordpress.com/2013/08/16/saaj-web-service-client-over-ssl/

      另外,如果您考虑使用框架来执行此操作,我建议您使用 CXF + Apache Camel:Apache Camel: CXF

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-01-21
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多