Pregunta

Estoy tratando de entender lo que está pasando en este código .

KeyStore trustStore  = KeyStore.getInstance(KeyStore.getDefaultType());        
FileInputStream instream = new FileInputStream(new File("my.keystore")); 
try {
    trustStore.load(instream, "nopassword".toCharArray());
} finally {
    instream.close();
}

SSLSocketFactory socketFactory = new SSLSocketFactory(trustStore);
Scheme sch = new Scheme("https", socketFactory, 443);
httpclient.getConnectionManager().getSchemeRegistry().register(sch);

Mis preguntas:

trustStore.load(instream, "nopassword".toCharArray()); está haciendo qué exactamente? De la lectura de la documentación load() cargará los datos de KeyStore de un flujo de entrada (que es sólo un archivo vacío que acabamos de crear), el uso de algunos arbitraria "NOPASSWORD". ¿Por qué no sólo tiene que cargar con null como el parámetro de InputStream y una cadena vacía como el campo de la contraseña?

Y entonces lo que está sucediendo cuando este almacén de claves vacío se pasa al constructor SSLSocketFactory? Lo que es el resultado de una operación de este tipo?

O -? Es esto simplemente un ejemplo donde en una aplicación real que tendría que poner realmente una referencia a un archivo de almacén / contraseña existente

¿Fue útil?

Solución

Or -- is this simply an example where in a real application you would have to actually put a reference to an existing keystore file / password?

It really looks that way. There is no "my.keystore" file distributed in either the binary or source distributions of HttpClient 4.0.1. For this to run you would create an actual keystore. You could use either keytool or Portecle.

This example is showing you how to utilize a different trust store than the one that the JVM uses by default ($JAVA_HOME/jre/lib/security/cacerts) for this instance of DefaultHttpClient. This is useful when an SSL site is using a certificate signed by their own in-house certificate authority. The SSL connection will only be established when the signer of the server certificate is recognized. The Wikipedia entry on TLS is a decent introduction if you are unfamiliar with the concept.

Otros consejos

This example tries to show you how to load your own trust store. To get this example working, you need to have a file called "my.keystore" in your current directory and the password for the keystore is "nopassword".

Please note new File("my.keystore") doesn't necessarily create a new file. It simply creates a File object pointing to the path.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top