Tengo un servidor REST creado en Grizzly que usa HTTPS y funciona maravillosamente con Firefox. Aquí está el código:Uso de HTTPS con REST en Java
//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();
Ahora, trato de llegar a ella en 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);
}
Y conseguir:
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)
De ejemplos que he encontrado en la web, parece que tendría que configurar un Truststore y luego configurar algún tipo de TrustManager. Esto parece mucho trabajo de configuración y código para mi pequeño proyecto simple. ¿Hay alguna manera más fácil de decirlo ... Confío en este certificado y apunto a un archivo .cert?
Muy bien, pero ... ¿Puedo establecer trustStore y trustStorePassword en el código en lugar de argumentos para la VM? – User1
Sí, antes de cualquier llamada a métodos que requieran SSL, use System.setProperty ("javax.net.ssl.trustStore", ") y haga lo mismo con javax.net.ssl. trustStorePassword. –
delfuego
Una parte triste es que un nombre de archivo incorrecto ofrece una excepción muy vaga: "java.security.InvalidAlgorithmParameterException: el parámetro trustAnchors debe estar no vacío" – User1