ssl - Jersey 2 - 带有 .target HTTPS

标签 ssl https jax-rs jersey-2.0 jersey-client

我一直在尝试从 Jersey 客户端执行 POST,但继续遇到以下异常:

Caused by: java.lang.IllegalStateException: Already connected at sun.net.www.protocol.http.HttpURLConnection.setRequestProperty(HttpURLConnection.java:3071) at sun.net.www.protocol.https.HttpsURLConnectionImpl.setRequestProperty(HttpsURLConnectionImpl.java:325) at org.glassfish.jersey.client.internal.HttpUrlConnector.setOutboundHeaders(HttpUrlConnector.java:424) at org.glassfish.jersey.client.internal.HttpUrlConnector.lambda$_apply$0(HttpUrlConnector.java:381) at org.glassfish.jersey.message.internal.CommittingOutputStream.commitStream(CommittingOutputStream.java:195) at org.glassfish.jersey.message.internal.CommittingOutputStream.commitStream(CommittingOutputStream.java:189) at org.glassfish.jersey.message.internal.CommittingOutputStream.commit(CommittingOutputStream.java:257) at org.glassfish.jersey.message.internal.OutboundMessageContext.commitStream(OutboundMessageContext.java:825) at org.glassfish.jersey.client.ClientRequest.doWriteEntity(ClientRequest.java:553) at org.glassfish.jersey.client.ClientRequest.writeEntity(ClientRequest.java:498) at org.glassfish.jersey.client.internal.HttpUrlConnector._apply(HttpUrlConnector.java:384) at org.glassfish.jersey.client.internal.HttpUrlConnector.apply(HttpUrlConnector.java:282) at org.glassfish.jersey.client.ClientRuntime.invoke(ClientRuntime.java:278) ... 63 more

我在 SO 中尝试了很多答案和解决方案,但都没有成功。

这里快疯了,求助!

public class JerseyWithSSL {

    public static javax.ws.rs.client.Client getRESTClient() {

        try {
            TrustManager[] trustMgr = new TrustManager[]{new X509TrustManager() {
                public X509Certificate[] getAcceptedIssuers() {
                    return null;
                }

                public void checkClientTrusted1(X509Certificate[] certs,
                        String authType) {
                }

                public void checkServerTrusted1(X509Certificate[] certs,
                        String authType) {
                }

                @Override
                public void checkClientTrusted(X509Certificate[] arg0, String arg1)
                        throws CertificateException {
                    // TODO Auto-generated method stub

                }

                @Override
                public void checkServerTrusted(X509Certificate[] arg0, String arg1)
                        throws CertificateException {
                    // TODO Auto-generated method stub

                }
            }};

            SSLContext context = SSLContext.getInstance("SSL");
            context.init(null, trustMgr, null);

            javax.ws.rs.client.Client client = ClientBuilder.newBuilder().sslContext(context)
                    .hostnameVerifier(new HostnameVerifier() {

                        @Override
                        public boolean verify(String arg0, SSLSession arg1) {
                            // TODO Auto-generated method stub
                            return true;
                        }
                    }).build();

            return client;
        } catch (NoSuchAlgorithmException | KeyManagementException ex) {
            Logger.getLogger(JerseyWithSSL.class.getName()).log(Level.SEVERE, null, ex);
        }

        return null;
    }
}

使用上述类的代码(此处抛出异常):

    // send notification to all subscriptors for event: attendee.create
    if (!subscriptions.isEmpty()) {

        try {
            Client client = JerseyWithSSL.getRESTClient();

            for (Subscription sub : subscriptions) {

                System.out.println("Subscription: \n" + sub.getEventName() + "\n" + sub.getTargetUrl());

                Response resp = client.target(sub.getTargetUrl())
                        .request(MediaType.APPLICATION_JSON)
                        .post(Entity.entity(newAttdee, MediaType.APPLICATION_JSON));

                System.out.println("Status: " + resp.getStatus());
                System.out.println(resp.getEntity().toString());

                resp.close();
            }
        } catch (Exception ex) {
            Logger.getLogger(AttendeeResource.class.getName()).log(Level.SEVERE, null, ex);
        }

    }

添加 pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>ON24Hooks</groupId>
    <artifactId>ON24_Zapier_Hooks</artifactId>
    <version>1.0-SNAPSHOT</version>
    <packaging>war</packaging>

    <name>ON24_Zapier_Hooks</name>

    <properties>
        <endorsed.dir>${project.build.directory}/endorsed</endorsed.dir>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>

    <dependencies>
        <dependency>
            <groupId>javax</groupId>
            <artifactId>javaee-web-api</artifactId>
            <version>7.0</version>
            <scope>provided</scope>
        </dependency>

        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <version>1.16.20</version>
            <scope>provided</scope>
        </dependency>

        <dependency>
            <groupId>org.apache.httpcomponents</groupId>
            <artifactId>httpclient</artifactId>
            <version>4.5.5</version>
        </dependency>

        <dependency>
            <groupId>org.glassfish.jersey.connectors</groupId>
            <artifactId>jersey-apache-connector</artifactId>
            <version>2.16</version>
            <type>jar</type>
        </dependency>

    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.1</version>
                <configuration>
                    <source>1.7</source>
                    <target>1.7</target>
                    <compilerArguments>
                        <endorseddirs>${endorsed.dir}</endorseddirs>
                    </compilerArguments>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-war-plugin</artifactId>
                <version>2.3</version>
                <configuration>
                    <failOnMissingWebXml>false</failOnMissingWebXml>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-dependency-plugin</artifactId>
                <version>2.6</version>
                <executions>
                    <execution>
                        <phase>validate</phase>
                        <goals>
                            <goal>copy</goal>
                        </goals>
                        <configuration>
                            <outputDirectory>${endorsed.dir}</outputDirectory>
                            <silent>true</silent>
                            <artifactItems>
                                <artifactItem>
                                    <groupId>javax</groupId>
                                    <artifactId>javaee-endorsed-api</artifactId>
                                    <version>7.0</version>
                                    <type>jar</type>
                                </artifactItem>
                            </artifactItems>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>

</project>

最佳答案

IllegalStateException: Already connected 可能掩盖了 SSLHandshakeException,如问题 #3000 中所述(以前称为 JERSEY-2728)。

根据release notes ,它已在 Jersey 2.23 中修复。不过,我会将 Jersey 升级到最新版本。

关于ssl - Jersey 2 - 带有 .target HTTPS,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49887739/

相关文章:

java - 异常映射 - org.jboss.resteasy.spi.UnhandledException : exception. DenemeException: off bu exler

jakarta-ee - 无法从 ExceptionMapper 捕获 ConstaintValidationException

android - 如何在 android 上创建压缩的 SSL/TLS 连接

ssl - 启用 SSL 后 Apache 不会重新启动

https - 使用 HTTPS 时是否需要/想要 gzip 压缩?

安卓8 : Cleartext HTTP traffic not permitted

java - 修改 ContainerResponseFilter 中的响应

python - OpenSSL 错误 - RestClient Gem - Python WSGI

ssl - 不同组织的相同通配符 SSL 证书

Android:在向量中访问 http 服务器而不是 https(Android Matrix 客户端)