小编典典

在Java中将HTTPS与REST一起使用

javascript

我有一台使用HTTPS且使用Firefox的Grizzly制造的REST服务器。这是代码:

//Build a new Servlet Adapter.
ServletAdapter adapter=new ServletAdapter();
adapter.addInitParameter("com.sun.jersey.config.property.packages", "My.services");
adapter.addInitParameter(ResourceConfig.PROPERTY_CONTAINER_REQUEST_FILTERS, SecurityFilter.class.getName());
adapter.setContextPath("/");
adapter.setServletInstance(new ServletContainer());

//Configure SSL (See instructions at the top of this file on how these files are generated.)
SSLConfig ssl=new SSLConfig();
String keystoreFile=Main.class.getResource("resources/keystore_server.jks").toURI().getPath();
System.out.printf("Using keystore at: %s.",keystoreFile);
ssl.setKeyStoreFile(keystoreFile);
ssl.setKeyStorePass("asdfgh");

//Build the web server.
GrizzlyWebServer webServer=new GrizzlyWebServer(getPort(9999),".",true);

//Add the servlet.
webServer.addGrizzlyAdapter(adapter, new String[]{"/"});

//Set SSL
webServer.setSSLConfig(ssl);

//Start it up.
System.out.println(String.format("Jersey app started with WADL available at "
  + "%sapplication.wadl\n",
        "https://localhost:9999/"));
webServer.start();

现在,我尝试用Java达到它:

SSLContext ctx=null;
try {
    ctx = SSLContext.getInstance("SSL");
} catch (NoSuchAlgorithmException e1) {
    e1.printStackTrace();
}
ClientConfig config=new DefaultClientConfig();
config.getProperties().put(HTTPSProperties.PROPERTY_HTTPS_PROPERTIES, new HTTPSProperties(null,ctx));
WebResource service=Client.create(new DefaultClientConfig()).resource("https://localhost:9999/");

//Attempt to view the user's page.
try{
    service
        .path("user/"+username)
        .get(String.class);
}

得到:

com.sun.jersey.api.client.ClientHandlerException: javax.net.ssl.SSLHandshakeException: sun.security.validator.ValidatorException: PKIX path building failed: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target
 at com.sun.jersey.client.urlconnection.URLConnectionClientHandler.handle(URLConnectionClientHandler.java:128)
 at com.sun.jersey.api.client.Client.handle(Client.java:453)
 at com.sun.jersey.api.client.WebResource.handle(WebResource.java:557)
 at com.sun.jersey.api.client.WebResource.get(WebResource.java:179)

从我在网上找到的示例来看,似乎需要设置一个Truststore,然后再设置某种TrustManager。对于我这个简单的小项目,这似乎需要大量代码和设置工作。有没有更简单的方法可以说..我信任此证书并指向.cert文件?


阅读 580

收藏
2020-09-12

共1个答案

小编典典

当您说“是否有一种更简单的方法来信任此证书”时,这就是将证书添加到Java信任库中所做的工作。这非常非常容易做到,您无需在客户端应用程序中进行任何操作即可使该信任库得到认可或利用。

在客户端计算机上,找到cacerts文件的位置(这是默认的Java信任存储,默认情况下位于<java-home> / lib / security / certs / cacerts

然后,键入以下内容:

keytool -import -alias <Name for the cert> -file <the .cer file> -keystore <path to cacerts>

这会将证书导入到您的信任存储中,然后,您的客户端应用程序将能够连接到您的Grizzly HTTPS服务器,而不会出现问题。

如果您不想将证书导入到默认的信任库中(即,您只希望该证书可用于该客户端应用程序,而不希望该证书在该计算机上的JVM上运行),则可以为您的应用创建一个新的信任库。与其将keytool的路径传递给现有的默认cacerts文件,而不是将keytool的路径传递给新的信任存储文件:

keytool -import -alias <Name for the cert> -file <the .cer file> -keystore <path to new trust store>

系统将要求您设置并验证信任存储文件的新密码。然后,在启动客户端应用程序时,请使用以下参数启动它:

java -Djavax.net.ssl.trustStore=<path to new trust store> -Djavax.net.ssl.trustStorePassword=<trust store password>

容易俗气,真的。

2020-09-12