当前位置: 首页>>代码示例>>Scala>>正文


Scala TrustManagerFactory类代码示例

本文整理汇总了Scala中javax.net.ssl.TrustManagerFactory的典型用法代码示例。如果您正苦于以下问题:Scala TrustManagerFactory类的具体用法?Scala TrustManagerFactory怎么用?Scala TrustManagerFactory使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。


在下文中一共展示了TrustManagerFactory类的8个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Scala代码示例。

示例1: WebserverApp

//设置package包名称以及导入依赖的类
import java.security.KeyStore
import javax.net.ssl.{KeyManagerFactory, SSLContext, TrustManagerFactory}

import com.twitter.finagle.Http
import com.twitter.util.Await
import io.circe.generic.auto._
import io.finch._
import io.finch.circe._


object WebserverApp extends App {

  def getSSLContext: SSLContext = {
    // Create and initialize the SSLContext with key material
    val passphrase = "sample".toCharArray()
    val trustPassphrase = "sample".toCharArray()
    // First initialize the key and trust material
    val ksKeys = KeyStore.getInstance("JKS")
    val keystoreResource = this.getClass.getClassLoader.getResourceAsStream("sample-keystore.jks")
    ksKeys.load(keystoreResource, passphrase)
    val ksTrust = KeyStore.getInstance("JKS")
    val trustStoreResource = this.getClass.getClassLoader.getResourceAsStream("sample-keystore.jks")
    ksTrust.load(trustStoreResource, trustPassphrase)
    // KeyManagers decide which key material to us
    val kmf = KeyManagerFactory.getInstance("SunX509")
    kmf.init(ksKeys, passphrase)
    // TrustManagers decide whether to allow connections
    val tmf = TrustManagerFactory.getInstance("SunX509")
    tmf.init(ksTrust)
    val sslContext = SSLContext.getInstance("TLS")
    sslContext.init(kmf.getKeyManagers, tmf.getTrustManagers, null)
    sslContext
  }


  def sample: Endpoint[String] =
    get("sample") {
      Ok("Was it a TLS connection?... probably not")
    }

  val routes = sample

  val server = Http.server
    .withTransport.tls(getSSLContext)
    .serve(s":38082", routes.handle {
      case e: Exception =>
        InternalServerError(e)
    }.toService)

  println("Server running on :38082")
  Await.result(server)

} 
开发者ID:nicojs,项目名称:poc-finch-tls,代码行数:54,代码来源:WebserverApp.scala

示例2: sslContext

//设置package包名称以及导入依赖的类
package org.packtpublishing.security

import java.security.{SecureRandom, KeyStore}
import javax.net.ssl.{KeyManagerFactory, SSLContext, TrustManagerFactory}
import spray.io.ServerSSLEngineProvider
import resource._

trait SslSupport {
  val random = SecureRandom.getInstance("SHA1PRNG")
  
  val keyStoreLocation = "/spray-book-catalog.jks"
  val keyStorePassword = "passw0rd"

  implicit def sslContext: SSLContext = {    
    val keyStore = KeyStore.getInstance("jks")
    
    for (jks <- managed(getClass.getResourceAsStream(keyStoreLocation))) {
      keyStore.load(jks, keyStorePassword.toCharArray)
    }
    
    val keyManagerFactory = KeyManagerFactory.getInstance("SunX509")
    keyManagerFactory.init(keyStore, keyStorePassword.toCharArray)
    
    val trustManagerFactory = TrustManagerFactory.getInstance("SunX509")
    trustManagerFactory.init(keyStore)
    
    val context = SSLContext.getInstance("TLS")
    context.init(keyManagerFactory.getKeyManagers, trustManagerFactory.getTrustManagers, random)
    context
  }
   
  implicit def sslEngineProvider: ServerSSLEngineProvider = {
    ServerSSLEngineProvider { engine =>
      engine.setEnabledProtocols(Array("TLSv1", "TLSv1.1", "TLSv1.2"))
      engine
    }
  }
} 
开发者ID:allansene,项目名称:spray-scala-akka-book-catalog,代码行数:39,代码来源:SslSupport.scala

示例3: getSSLContext

//设置package包名称以及导入依赖的类
package rere.driver.connection

import java.io.ByteArrayInputStream
import java.security.KeyStore
import java.security.cert.{CertificateFactory, X509Certificate}
import javax.net.ssl.{SSLContext, TrustManagerFactory}

import rere.driver.TLSProtocolVersion

sealed trait SSLContextProvider {
  def getSSLContext: SSLContext
}

class PreparedSSLContextProvider(context: SSLContext) extends SSLContextProvider {
  override def getSSLContext: SSLContext = context
}

class FromCertificateFileProvider(bytes: Array[Byte], protocolVersion: TLSProtocolVersion) extends SSLContextProvider {
  override def getSSLContext: SSLContext = {
    val certificateFactory = CertificateFactory.getInstance("X.509")
    val certificate = certificateFactory.generateCertificate(new ByteArrayInputStream(bytes)).asInstanceOf[X509Certificate]

    val trustManagerFactory = TrustManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm)
    val keyStore = KeyStore.getInstance(KeyStore.getDefaultType)
    keyStore.load(null)
    keyStore.setCertificateEntry("rootCa", certificate)
    trustManagerFactory.init(keyStore)

    val context = SSLContext.getInstance(protocolVersion.canonicalName)
    context.init(null, trustManagerFactory.getTrustManagers, null)
    context
  }
} 
开发者ID:pbaun,项目名称:rere,代码行数:34,代码来源:SSLContextProvider.scala

示例4: sslFile

//设置package包名称以及导入依赖的类
package console

import java.security._
import java.io.{InputStream, FileInputStream, File}
import javax.net.ssl.{KeyManagerFactory, SSLContext, TrustManagerFactory}
import akka.http.scaladsl.{ConnectionContext, HttpsConnectionContext}

trait SslSupport {
  val algorithm = "SunX509"

  def sslFile: String

  private def create(in: InputStream, keyPass: String, storePass: String) = {
    val keyStore = KeyStore.getInstance("JKS")
    keyStore.load(in, storePass.toCharArray)

    val keyManagerFactory = KeyManagerFactory.getInstance(algorithm)
    keyManagerFactory.init(keyStore, keyPass.toCharArray)

    val tmf = TrustManagerFactory.getInstance(algorithm)
    (tmf init keyStore)

    val sslContext = SSLContext.getInstance("TLS")
    sslContext.init(keyManagerFactory.getKeyManagers, tmf.getTrustManagers, new SecureRandom)

    (ConnectionContext https sslContext)
  }

  def https(keyPass: String, storePass: String): HttpsConnectionContext = {
    val file = new File("./"  + sslFile)
    if(file.exists) create(new FileInputStream(file), keyPass, storePass)
    else {
      resource.managed(getClass.getResourceAsStream("/"+ sslFile))
        .map { in => create(in, keyPass, storePass) }
        .opt
        .fold(throw new Exception("jks file hasn't been found"))(identity)
    }
  }
} 
开发者ID:haghard,项目名称:akka-cluster-console,代码行数:40,代码来源:SslSupport.scala

示例5: ClientSSLConfiguration

//设置package包名称以及导入依赖的类
package com.biosimilarity.evaluator.spray.client

import java.io.InputStream
import java.security.cert.{Certificate, CertificateFactory}
import java.security.{KeyStore, SecureRandom}
import javax.net.ssl.{SSLContext, SSLParameters, TrustManagerFactory}

import com.biosimilarity.evaluator.util._
import spray.io.{ClientSSLEngineProvider, SSLContextProvider}

object ClientSSLConfiguration {

  private def loadX509Certificate(resourceName: String): Certificate = {
    val certResource: InputStream = resourceStream(resourceName)
    try {
      CertificateFactory.getInstance("X.509").generateCertificate(certResource)
    } finally {
      certResource.close()
    }
  }

  private def clientSSLContext: SSLContext = {
    val keystore: KeyStore                       = KeyStore.getInstance(KeyStore.getDefaultType)
    val trustManagerFactory: TrustManagerFactory = TrustManagerFactory.getInstance("SunX509")
    val context: SSLContext                      = SSLContext.getInstance("TLS")
    val params: SSLParameters                    = new SSLParameters
    keystore.load(null, null)
    keystore.setCertificateEntry("ca", loadX509Certificate("gloseval.pem"))
    trustManagerFactory.init(keystore)
    context.init(null, trustManagerFactory.getTrustManagers, new SecureRandom)
    params.setEndpointIdentificationAlgorithm("https")
    context
  }

  def clientSSLEngineProvider: ClientSSLEngineProvider = ClientSSLEngineProvider(identity)(SSLContextProvider.forContext(clientSSLContext))
} 
开发者ID:synereo,项目名称:synereo,代码行数:37,代码来源:ClientSSLConfiguration.scala

示例6: SSLConfiguration

//设置package包名称以及导入依赖的类
package com.biosimilarity.evaluator.spray

import java.io.InputStream
import java.security.{KeyStore, SecureRandom}
import javax.net.ssl.{KeyManagerFactory, SSLContext, TrustManagerFactory}

import com.biosimilarity.evaluator.distribution.EvalConfigWrapper
import com.biosimilarity.evaluator.util._
import spray.io.{SSLContextProvider, ServerSSLEngineProvider}

object SSLConfiguration extends Serializable {

  private def sslContext: SSLContext = {
    val keystoreResource: InputStream = resourceStream("keystore.jks")
    try {
      val storepass: String                        = EvalConfigWrapper.readString("storepass")
      val keypass: String                          = EvalConfigWrapper.readString("keypass")
      val keyStore: KeyStore                       = KeyStore.getInstance("jks")
      val keyManagerFactory: KeyManagerFactory     = KeyManagerFactory.getInstance(KeyManagerFactory.getDefaultAlgorithm)
      val trustManagerFactory: TrustManagerFactory = TrustManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm)
      val context: SSLContext                      = SSLContext.getInstance("TLS")
      keyStore.load(keystoreResource, storepass.toCharArray)
      keyManagerFactory.init(keyStore, keypass.toCharArray)
      trustManagerFactory.init(keyStore)
      context.init(keyManagerFactory.getKeyManagers, trustManagerFactory.getTrustManagers, new SecureRandom)
      context
    } finally {
      keystoreResource.close()
    }
  }

  def sslEngineProvider: ServerSSLEngineProvider = ServerSSLEngineProvider(identity)(SSLContextProvider.forContext(sslContext))
} 
开发者ID:synereo,项目名称:synereo,代码行数:34,代码来源:SSLConfiguration.scala

示例7: SSLUtil

//设置package包名称以及导入依赖的类
package org.hyperscala.util

import java.io.File
import java.nio.file.Files
import java.security.KeyStore
import javax.net.ssl.{KeyManagerFactory, SSLContext, TrustManagerFactory}

object SSLUtil {
  def createSSLContext(keyStoreFile: File, password: String): SSLContext = {
    val passwordChars = password.toCharArray

    val keyStore = KeyStore.getInstance("JKS")
    assert(keyStoreFile.exists(), s"No keystore file was found at the location: ${keyStoreFile.getAbsolutePath}")
    val keyStoreInput = Files.newInputStream(keyStoreFile.toPath)
    keyStore.load(keyStoreInput, passwordChars)

    val keyManagerFactory = KeyManagerFactory.getInstance(KeyManagerFactory.getDefaultAlgorithm)
    keyManagerFactory.init(keyStore, passwordChars)
    val keyManagers = keyManagerFactory.getKeyManagers

    val trustManagerFactory = TrustManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm)
    trustManagerFactory.init(keyStore)
    val trustManagers = trustManagerFactory.getTrustManagers

    val sslContext = SSLContext.getInstance("TLS")
    sslContext.init(keyManagers, trustManagers, null)
    sslContext
  }
} 
开发者ID:outr,项目名称:hyperscala,代码行数:30,代码来源:SSLUtil.scala

示例8: create

//设置package包名称以及导入依赖的类
package linguistic

import java.security._
import java.io.{InputStream, FileInputStream, File}
import javax.net.ssl.{KeyManagerFactory, SSLContext, TrustManagerFactory}

import akka.http.scaladsl.{ConnectionContext, HttpsConnectionContext}

trait SslSupport {

  val algorithm = "SunX509"

  private def create(in: InputStream, keyPass: String, storePass: String) = {
    val keyStore = KeyStore.getInstance("JKS")
    keyStore.load(in, storePass.toCharArray)

    val keyManagerFactory = KeyManagerFactory.getInstance(algorithm)
    keyManagerFactory.init(keyStore, keyPass.toCharArray)

    val tmf = TrustManagerFactory.getInstance(algorithm)
    (tmf init keyStore)

    val sslContext = SSLContext.getInstance("TLS")
    sslContext.init(keyManagerFactory.getKeyManagers, tmf.getTrustManagers, new SecureRandom)

    (ConnectionContext https sslContext)
  }

  def https(keyPass: String, storePass: String): HttpsConnectionContext = {
    val file = new File("./linguistic.jks")
    if(file.exists) {
      create(new FileInputStream(file), keyPass, storePass)
    } else {
      resource.managed(getClass.getResourceAsStream("/linguistic.jks")).map { in =>
        create(in, keyPass, storePass)
      }.opt.fold(throw new Exception("jks hasn't been found"))(identity)
    }
  }
} 
开发者ID:haghard,项目名称:linguistic,代码行数:40,代码来源:SslSupport.scala


注:本文中的javax.net.ssl.TrustManagerFactory类示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。