Skip to content

Instantly share code, notes, and snippets.

@bees4ever
Created December 16, 2018 13:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bees4ever/9332ef913b597ced14c979c93f54a378 to your computer and use it in GitHub Desktop.
Save bees4ever/9332ef913b597ced14c979c93f54a378 to your computer and use it in GitHub Desktop.
Scala download binary file from untrusted ssl url
// based on https://stackoverflow.com/a/28787883/5885054 with own modification
import javax.net.ssl._
import java.security.cert.X509Certificate
import scala.io.Source
// Bypasses both client and server validation.
object TrustAll extends X509TrustManager {
val getAcceptedIssuers = null
def checkClientTrusted(x509Certificates: Array[X509Certificate], s: String) = {}
def checkServerTrusted(x509Certificates: Array[X509Certificate], s: String) = {}
}
// Verifies all host names by simply returning true.
object VerifiesAllHostNames extends HostnameVerifier {
def verify(s: String, sslSession: SSLSession) = true
}
// Main class
object Test extends App {
import sys.process._
import java.net.URL
import java.io.File
// SSL Context initialization and configuration
val sslContext = SSLContext.getInstance("SSL")
sslContext.init(null, Array(TrustAll), new java.security.SecureRandom())
HttpsURLConnection.setDefaultSSLSocketFactory(sslContext.getSocketFactory)
HttpsURLConnection.setDefaultHostnameVerifier(VerifiesAllHostNames)
new URL("https://localhost:8443/api/posts/5c13debcd054b44db38dd709/image") #> new File("Output.jpg") !!
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment