使用ws.acceptAnyCertificate = true

时间:2017-05-30 12:17:12

标签: scala playframework-2.0

我正在使用Play WS下载文件,但即使使用handshake_failure,我也会收到ws.acceptAnyCertificate = true例外。

// libraryDependencies += "com.typesafe.play" %% "play-ws" % "2.5.15"
import akka.actor._
import akka.stream._
import akka.util.ByteString
import play.api.libs.ws.WSResponse
import play.api.libs.ws.ahc._

import scala.concurrent.duration.Duration
import scala.concurrent.{Await, Future}

implicit val system = ActorSystem()
implicit val materializer = ActorMaterializer()
val ws = AhcWSClient()

println(ws.config.isAcceptAnyCertificate) // false

def getBody(future: Future[WSResponse]): ByteString = {
  val response: WSResponse = Await.result(future, Duration.Inf)
  if (response.status != 200)
    throw new Exception(response.statusText)
  response.bodyAsBytes
}

def download(url: String): ByteString =
  HttpUtils.getBody(ws.url(url).withFollowRedirects(true).get())

download("https://www.tivo.com/legal/patents")
// Exception in thread "main" java.net.ConnectException: Received fatal alert: handshake_failure

在播放应用程序中,我可以使用文本conf/application.conf创建文件ws.acceptAnyCertificate=true。但我使用play-ws作为lib,而不是作为应用程序。所以我这样做:

val loose = SSLLooseConfig(acceptAnyCertificate = true, allowWeakCiphers = true, allowWeakProtocols = true, allowUnsafeRenegotiation = Option(true))
val sslConfig = SSLConfig(loose = loose)
val wsClientConfig = WSClientConfig(ssl = sslConfig)
private val config = AhcWSClientConfig(wsClientConfig)
val ws = AhcWSClient(config)
println(ws.config.isAcceptAnyCertificate) // true

但是,我仍然得到同样的例外:

download("https://www.tivo.com/legal/patents")
// Exception in thread "main" java.net.ConnectException: Received fatal alert: handshake_failure

适用于下载(“https://www.epfl.ch/”),但不适用于下载(“https://www.tivo.com/legal/patents”)

如何解决这个问题?

0 个答案:

没有答案