有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

ssl证书无法通过Java和IntelliJ连接到SOAP服务

我很难发送一个简单的SOAP请求并在java项目中获得响应

我对通过java实现这一点还相当陌生,所以我一直在看this教程,因为我在项目的其余部分也使用spring boot

目前我遇到了这个错误(由于与公司相关,我不得不删除细节,并用通用字符串替换,以便粘贴到这里。我不得不删除我的实际wsdl和实际主机名):

[INFO] --- maven-jaxb2-plugin:0.12.3:generate (default) @ dcc-vm-validation ---
[INFO] Up-to-date check for source resources [[myWsdl, file:pom.xml]] and taret resources [[]].
[WARNING] The URI [myWsdl] seems to represent an absolute HTTP or HTTPS URL. Getting the last modification timestamp is only possible if the URL is accessible and if the server returns the [Last-Modified] header correctly. This method is not reliable and is likely to fail. In this case the last modification timestamp will be assumed to be unknown.
[ERROR] Could not retrieve the last modification timestamp for the URI [myWsdl] from the HTTP URL connection. The [Last-Modified] header was probably not set correctly.
[WARNING] Last modification of the URI [myWsdl] is not known.
[INFO] Sources are not up-to-date, XJC will be executed.
[ERROR] Error while parsing schema(s).Location [].
com.sun.istack.SAXParseException2; IOException thrown when processing "myWsdl". Exception: javax.net.ssl.SSLHandshakeException: java.security.cert.CertificateException: No name matching hostname found.

以下是我的pom的“构建”部分:

<build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>

            <plugin>
                <groupId>org.jvnet.jaxb2.maven2</groupId>
                <artifactId>maven-jaxb2-plugin</artifactId>
                <version>0.12.3</version>

                <executions>
                    <execution>
                        <goals>
                            <goal>generate</goal>
                        </goals>
                    </execution>
                </executions>

                <configuration>
                    <schemaLanguage>WSDL</schemaLanguage>
                    <generatePackage>package.wsdl</generatePackage>

                    <schemas>
                        <schema>
                            <url>myWsdl</url>
                        </schema>
                    </schemas>
                </configuration>
            </plugin>
        </plugins>
    </build>

在研究了这个特定错误的含义之后,我能够将自签名证书从wsdl位置(通过Chrome)下载到我的桌面。然后我使用keytool将这个特定的证书添加到我的cacerts文件中(我确保证书别名与上面错误中的主机名匹配)。据我所知,这将解决我的连接问题,但事实并非如此。我仍然看到与上述相同的错误,即找不到我的主机名

我错过了什么

或者,我也读过关于完全绕过证书的文章。这也是一种我不会反对的方法,因为我正在从事的这个项目只是内部的,将位于公司内部网上。除了员工之外,任何人都无法访问它,因此安全风险基本上可以忽略。我知道这被认为是不好的做法,但我已经阅读了风险,并与我的老板讨论过,如果我们也这样做,他也会很好

我找到了这段代码片段,它似乎可以完成这项工作:

static {
            javax.net.ssl.HttpsURLConnection.setDefaultHostnameVerifier(new javax.net.ssl.HostnameVerifier() {
                public boolean verify(String hostname, javax.net.ssl.SSLSession session) {
                    if (hostname.equals(myHostname)) {
                        return  true;
                    }

                    return false;
                }
            });
        }

但在所有的例子中,我发现它从来没有指明放在哪里。它只是说,这就是您需要忽略SSL和自签名证书错误的原因。我应该把它放在哪里与我的代码相关联?既然我试图通过spring/jaxb来实现,那么有没有一种具体的方法来实现这一点

对不起,这堵墙太长了!只是想在这里学点新东西:)


共 (1) 个答案

  1. # 1 楼答案

    我通常不建议编译绝对URL。它会使构建变得不稳定,并引入像您所遇到的问题一样的问题

    更好的解决方案是在项目中本地下载和存储相关资源,并使用catalog files重写URL

    我将假设一个像http://www.my.org/wsdl/my.wsdl这样的绝对URL。从http://www.my.org下载资源并将其存储在src/main/resource/www.my.org

    然后编写一个src/main/resources/catalog.cat文件,如下所示:

    REWRITE_SYSTEM "http://www.my.org" "www.my.org"
    

    在插件中配置它:

    <configuration>
        <catalog>src/main/resources/catalog.cat</catalog>
        <schemaLanguage>WSDL</schemaLanguage>
        <schemas>
            <schema>
                <url>http://www.my.org/wsdl/my.wsdl</url>
            </schema>
        </schemas>
    </configuration>
    

    XJC将把您的http://www.my.org/wsdl/my.wsdlURL重写为src/main/resources/www.my.org/wsdl/my.wsdl

    这里有一些警告,比如您不能混合使用相对URL和绝对URL,但最终这比在构建中依赖外部资源要稳定得多。您确实希望构建是稳定和可重复的。想想如果有人更新服务器上的my.wsdl,会发生什么

    免责声明:我是maven-jaxb2-plugin的作者