题
我在使用HTTPS和使用Firefox奇妙的作品灰熊取得了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)
这是我在网上找到的例子,好像我需要设置那么信任存储区设置某种的TrustManager的。这似乎是一个大量的代码和安装工作,为我简单的小项目。是否有刚刚say..I信任这个证书,并指向一个.cert文件更简单的方法?
解决方案
当你说“是那里......相信这个证书更简单的方法”,这是你通过添加证书到Java信任库做什么。这是非常,非常容易做到的,并没有什么,你需要你的客户端应用程序中做的就是信任店承认或利用。
在您的客户机,寻找到你的cacerts文件是(这是默认的Java信任库,并且是在默认情况下,位于
然后,键入以下内容:
keytool -import -alias <Name for the cert> -file <the .cer file> -keystore <path to cacerts>
这将导入证书到您的信任存储,并在此之后,您的客户端应用程序将能够连接到您的灰熊HTTPS服务器没有问题。
如果你不希望将证书导入到您的默认信任存储 - 也就是说,你只希望它是提供给这一个客户端应用程序,而不是其他任何你在这台机器上的JVM上运行 - 那么你可以创建一个新的信托商店只为你的应用程序。相反,通过密钥工具的路径存在,默认cacerts文件中,通过密钥工具的路径,新的信任存储文件:
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>
易俗气,真的。
其他提示
这里的痛苦路线:
SSLContext ctx = null;
try {
KeyStore trustStore;
trustStore = KeyStore.getInstance("JKS");
trustStore.load(new FileInputStream("C:\\truststore_client"),
"asdfgh".toCharArray());
TrustManagerFactory tmf = TrustManagerFactory
.getInstance("SunX509");
tmf.init(trustStore);
ctx = SSLContext.getInstance("SSL");
ctx.init(null, tmf.getTrustManagers(), null);
} catch (NoSuchAlgorithmException e1) {
e1.printStackTrace();
} catch (KeyStoreException e) {
e.printStackTrace();
} catch (CertificateException e) {
e.printStackTrace();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (KeyManagementException e) {
e.printStackTrace();
}
ClientConfig config = new DefaultClientConfig();
config.getProperties().put(HTTPSProperties.PROPERTY_HTTPS_PROPERTIES,
new HTTPSProperties(null, ctx));
WebResource service = Client.create(config).resource(
"https://localhost:9999/");
service.addFilter(new HTTPBasicAuthFilter(username, password));
// Attempt to view the user's page.
try {
service.path("user/" + username).get(String.class);
} catch (Exception e) {
e.printStackTrace();
}
爱是爱的六种不同的捕获的异常:)。当然也有一些重构以简化代码位。但是,我很喜欢在虚拟机上delfuego的-D选项。我希望有我可以只设置一个javax.net.ssl.trustStore中静态属性。只是两行代码和done。任何人都知道那会是什么?
这可能是过分的要求,但是,理想情况下将不使用密钥工具。相反,trustedStore将被动态地由代码创建和CERT在运行时添加。
必须有更好的答案。
东西要记住的是,这种错误不仅是由于自签名的证书。新的委托CA证书失败,相同的错误,并做正确的事情就是更新相应的根证书的服务器,而不是禁用此重要安全功能。
检查了这一点: http://code.google.com/p/resting/。我可以用休息消耗 HTTPS REST服务。