Java and HTTPS url connection without downloading certificate

The reason why you don’t have to load a certificate locally is that you’ve explicitly chosen not to verify the certificate, with this trust manager that trusts all certificates.

The traffic will still be encrypted, but you’re opening the connection to Man-In-The-Middle attacks: you’re communicating secretly with someone, you’re just not sure whether it’s the server you expect, or a possible attacker.

If your server certificate comes from a well-known CA, part of the default bundle of CA certificates bundled with the JRE (usually cacerts file, see JSSE Reference guide), you can just use the default trust manager, you don’t have to set anything here.

If you have a specific certificate (self-signed or from your own CA), you can use the default trust manager or perhaps one initialised with a specific truststore, but you’ll have to import the certificate explicitly in your trust store (after independent verification), as described in this answer. You may also be interested in this answer.

Leave a Comment