无法在泽西岛进行基本的 http 身份验证

Mar*_*ada 2 java rest ssl jersey jersey-client

我正在尝试使用 Jersey 1.X 版本连接到安全的外部休息服务。

我使用了以下代码

public class MyRestClient
{
  private static final String API_USER_NAME = "some value";
  private static final String API_PASSWORD = "some value";
  private static final String REST_URL = "https://<somevalue>";

  public static void main(String[] args)
  {
    ClientConfig config = new DefaultClientConfig();
    Client client = Client.create(config);
    client.addFilter(new HTTPBasicAuthFilter(API_USER_NAME, API_PASSWORD));
    WebResource webResource =
      client.resource(UriBuilder.fromUri(REST_URL).build());

    ClientResponse response = webResource.post(ClientResponse.class);
    System.out.println(response);
  }
}
Run Code Online (Sandbox Code Playgroud)

但我一直打这个例外..

com.sun.jersey.api.client.ClientHandlerException: javax.net.ssl.SSLHandshakeException: java.security.cert.CertificateException: No name matching 'somevalue' found
Run Code Online (Sandbox Code Playgroud)

我检查了这个外部休息服务的 API,它说它支持基本 HTTP 身份验证,但我不知道为什么我一直遇到这个错误。

有什么想法吗?

Pau*_*tha 5

因为 Basic Auth 本身缺乏安全性,所以它通常通过 SSL 完成,正如您https在 URL的架构中看到的那样。对于 SSL,使用了证书。SSL 握手包括服务器发送其证书和客户端检查其信任库以查看证书是否受信任。平台应该有一个它信任的证书颁发机构列表。例如,如果我们尝试访问

WebTarget target = client.target("https://wikipedia.org");
Run Code Online (Sandbox Code Playgroud)

这将起作用,因为维基百科发送的证书是由系统中受信任的机构签署的。另一方面,如果来自服务器的证书不是由这些受信任的机构之一签署的,那么 SSL 握手将失败。

如果是这种情况,则Client需要配置为处理 SSL 握手,这就是您收到异常的原因。你可以看到一些很好的答案在这里上如何配置Client与工作https


更新

您提供的链接已失效,所以我不知道“myTrustManager”和“hostnameVerifier”是什么……你能分享一些关于我如何提供的信息吗?

import com.sun.jersey.api.client.Client;
import com.sun.jersey.api.client.ClientResponse;
import com.sun.jersey.api.client.config.ClientConfig;
import com.sun.jersey.api.client.config.DefaultClientConfig;
import com.sun.jersey.client.urlconnection.HTTPSProperties;
import java.io.FileInputStream;
import java.security.KeyStore;
import javax.net.ssl.SSLContext;
import javax.net.ssl.TrustManagerFactory;
import org.junit.Test;

public class JUnitTest {

    private static final String TRUSTSTORE_FILE = "<location-of-truststore";
    private static final String TRUSTSTORE_PASSWORD = "trustStorePassword";

    @Test
    public void test() throws Exception {
        KeyStore truststore = KeyStore.getInstance("JKS");
        truststore.load(new FileInputStream(TRUSTSTORE_FILE), 
                                            TRUSTSTORE_PASSWORD.toCharArray());
        TrustManagerFactory tmf = TrustManagerFactory.getInstance("SunX509");
        tmf.init(truststore);
        SSLContext sslContext = SSLContext.getInstance("SSL");
        sslContext.init(null, tmf.getTrustManagers(), null);

        ClientConfig config = new DefaultClientConfig();
        config.getProperties().put(HTTPSProperties.PROPERTY_HTTPS_PROPERTIES, 
                new HTTPSProperties(null, sslContext));
        Client client = Client.create(config);

        final String httpsUrl = "https://...";
        ClientResponse response = client.resource(httpsUrl).get(ClientResponse.class);
        System.out.println(response.getStatus());
        System.out.println(response.getEntity(String.class));
    } 
}
Run Code Online (Sandbox Code Playgroud)

基本上,您需要从拥有 API 的人那里获得X.509 证书(有关编程方式,请参阅下面的链接)。然后您将其导入您的信任存储。这就是您的客户端知道信任连接的方式。如果您相信服务器就是他们所说的那样,则可以加密连接。

获得证书后,您可以使用Java keytool导入它。通过做这个

keytool -import -alias serverCert -file <cert.file> -keystore <client_trust_file>

您将被要求输入密码。然后询问您是否信任该证书。输入“是”,然后你就完成了。这是client_trust_file您键入的文件 ( ) 和密码,应在上述代码中使用。


更新 2

有关创建通过与 Tomcat 的安全 SSL 连接的简单应用程序的说明。使用上面的客户端代码来访问它。我将使用 Netbeans 8,但也会尝试包含以一般方式执行此操作的说明。我还将使用 Tomcat 8(配置可能与 Tomcat 7 略有不同。您应该查阅文档以了解任何差异)。我将使用 Maven,所以希望您能轻松使用它。

第1步:

创建一个新的应用程序。我将从 Maven 原型创建一个简单的 Jersey 应用程序。在 Netbeans 中

File → New Project → Maven → Project from Archetype → 搜索“jersey-quickstart-webapp”;选择 groupId 为“org.glassfish.jersey.archetypes”的那个→下一步→将项目命名为“secured-rest-app”→希望你能完成剩下的。您应该最终得到一个 Maven 应用程序。

在支持 Maven 的任何其他 IDE 中,只需查找具有坐标的原型:

  • groupId:org.glassfish.jersey.archetypes
  • artifactId: jersey-quickstart-webapp
  • 版本:2.13

从命令行:做

mvn archetype:generate -DarchetypeArtifactId=jersey-quickstart-grizzly2 \
    -DarchetypeGroupId=org.glassfish.jersey.archetypes -DinteractiveMode=false \
    -DgroupId=com.example -DartifactId=secured-rest-app -Dpackage=secured.rest.app \
    -DarchetypeVersion=2.13 
Run Code Online (Sandbox Code Playgroud)

第2步:

我们需要在应用程序中设置基本身份验证。这可以在 web.xml 中完成。打开项目的 web.xml 并将其添加到</servlet-mapping>

<security-constraint>
    <web-resource-collection>
        <web-resource-name>Secured Rest App</web-resource-name>
        <url-pattern>/webapi/*</url-pattern>
    </web-resource-collection>
    <auth-constraint>
        <role-name>*</role-name>
    </auth-constraint>
</security-constraint>

<login-config>
    <auth-method>BASIC</auth-method>
    <realm-name>secured-rest-app.com</realm-name>
</login-config>

<security-role>
    <role-name>user</role-name>
</security-role>
Run Code Online (Sandbox Code Playgroud)

第 3 步:

现在我们只需要在Tomcat中设置领域。默认情况下,Tomcat 使用领域名称UserDatabaseRealm。它基本上只是从一个 xml 文件中读取。这可能不是生产中最需要的方式,但作为示例,这是最容易使用的方式。有关领域的更多信息,请参阅领域配置 HOW-TO。对于这个特定领域,文件已经设置。我们只需要添加我们的用户。打开<tomcat-home>/conf/tomcat-users.xml并在里面输入以下用户<tomcat-users>

<user password="secret" roles="user" username="peeskillet"/>
Run Code Online (Sandbox Code Playgroud)

第四步:

现在我们可以测试一下。如果您已经在 Netbeans 上安装了 Tomcat,我们需要做的就是Run选择服务器。这应该会自动打开浏览器到我们的index.jsp. 该文件不受保护,因为它不符合<url-pattern>/webapi/*</url-pattern>out 安全约束。单击 Jersey Resource 链接,您将看到 Basic Auth 登录。键入peeskilletsecret分别对用户名和密码。现在您可以访问该资源。

第 5 步:

以上所有内容只是为我们设置了基本身份验证,但由于所有基本身份验证都是对我们的用户名和密码进行 base64 编码,因此可以轻松解码,因此我们需要通过安全连接进行身份验证。

我们需要做的第一件事是为我们的服务器创建一个密钥库。我们将在这里创建一个自签名证书,这应该只在开发中完成。在生产中,您需要从受信任的 CA 机构获取证书

cd<tomcat-home>/conf并键入以下内容(全部在一行上)

keytool -genkey -alias localhost -keyalg RSA -keysize 1024
        -dname "CN=localhost"
        -keypass supersecret
        -keystore tomcat-keystore.jks
        -storepass supersecret
Run Code Online (Sandbox Code Playgroud)

您现在应该tomcat-keystore.jksconf目录中看到一个文件。现在我们可以导出证书了。

keytool -export -alias localhost -rfc -keystore ./tomcat-keystore.jks > ./tomcat.cert
Run Code Online (Sandbox Code Playgroud)

系统将提示您输入密码,键入supersecret。您现在应该看到在tomcat.cert目录中创建的文件conf。将该文件复制到您在上面创建的应用程序的项目根目录中。

从命令行cd到项目根目录,tomcat.cert输入以下位置

keytool -import -alias tomcatCert -file ./tomcat.cert -keystore ./client-truststore.jks
Run Code Online (Sandbox Code Playgroud)

系统将提示您输入信任库的密码。使用trustpass. 您需要输入两次。完成后,它会提示信任证书,键入yes并回车。您现在应该client-truststore.jks在项目根目录中看到一个文件。这就是我们将用于我们的客户端应用程序的内容。

现在我们只需要配置 Tomcat 以使用我们的密钥库进行连接。在<tomcat-home>/conf/server.xml,<Service>元素内部。(注意Tomcat 7可能有点不同)

<Connector port="8443" 
    protocol="org.apache.coyote.http11.Http11NioProtocol"
    maxThreads="150" 
    SSLEnabled="true" 
    scheme="https" 
    secure="true"
    keystoreFile="absolute/path/to/tomcat-keystore.jks"
    keystorePass="supersecret"
    clientAuth="false" 
    sslProtocol="TLS" />
Run Code Online (Sandbox Code Playgroud)

最后,在我们的 webapp 中,我们应该通过改变 <security-constraint>

<security-constraint>
    <web-resource-collection>
        <web-resource-name>Secured Rest App</web-resource-name>
        <url-pattern>/webapi/*</url-pattern>
    </web-resource-collection>
    <auth-constraint>
        <role-name>*</role-name>
    </auth-constraint>
    <user-data-constraint>
        <transport-guarantee>CONFIDENTIAL</transport-guarantee>
    </user-data-constraint>
</security-constraint>
Run Code Online (Sandbox Code Playgroud)

现在我们准备使用我们的客户端代码。

第 6 步:

上面的代码使用 Jersey 1 客户端。我们使用的是 Jersey 2,所以代码会略有不同。在应用程序的任何地方,只需创建一个带有main运行客户端方法的类。这是我使用的:

import java.io.FileInputStream;
import java.security.KeyStore;
import javax.net.ssl.SSLContext;
import javax.net.ssl.TrustManagerFactory;
import javax.ws.rs.client.Client;
import javax.ws.rs.client.ClientBuilder;
import javax.ws.rs.core.Response;
import org.glassfish.jersey.client.authentication.HttpAuthenticationFeature;

public class SimpleClientApp {

    private static final String TRUSTSTORE_FILE = "client-truststore.jks";
    private static final String TRUSTSTORE_PASSWORD = "trustpass";
    private static final String APP_URL 
            = "https://localhost:8443/secured-rest-app/webapi/myresource";

    public static void main(String[] args) throws Exception {
        KeyStore truststore = KeyStore.getInstance("JKS");
        truststore.load(new FileInputStream(TRUSTSTORE_FILE), 
                                            TRUSTSTORE_PASSWORD.toCharArray());
        TrustManagerFactory tmf = TrustManagerFactory.getInstance("SunX509");
        tmf.init(truststore);
        SSLContext sslContext = SSLContext.getInstance("TLS");
        sslContext.init(null, tmf.getTrustManagers(), null);

        Client client = ClientBuilder.newBuilder()
                .sslContext(sslContext).build();
        client.register(HttpAuthenticationFeature.basic("peeskillet", "secret"));

        Response response = client.target(APP_URL).request().get();
        System.out.println(response.readEntity(String.class));

    }
}
Run Code Online (Sandbox Code Playgroud)

你应该能够运行它,它应该打印出来Got it!。我们完成了!

由于这个特定问题是关于 Jersey 1 的,我只想提一下您可以轻松创建 Jersey 1 应用程序。从第一步开始,只需使用 Maven 原型的坐标

  • groupId:com.sun.jersey.archetypes
  • artifactId: jersey-quickstart-webapp
  • 版本:1.18.1

在NetBeans中,只要按照上面的步骤,但选择com.sun.jersey.archetypes的版本jersey-quickstart-webapp

使用 Jersey 1,您可以使用原始答案中的代码,只需添加 Basic Auth 过滤器,就像 OP 在原始帖子中所做的那样,当然设置用户名和密码并更改 url。

如果帖子中有任何错误,请告诉我。我还没有机会校对这个:-)

一些资源