zoukankan      html  css  js  c++  java
  • Akka-HTTP服务器支持https

    1. 首先先获取HTTPS的数字证书文件(官方的证书。。)
    2. 配置HTTPS实例,具体代码如下:
    import java.io.InputStream
    import java.security.{ SecureRandom, KeyStore }
    import javax.net.ssl.{ SSLContext, TrustManagerFactory, KeyManagerFactory }
    
    import akka.actor.ActorSystem
    import akka.http.scaladsl.server.{ Route, Directives }
    import akka.http.scaladsl.{ ConnectionContext, HttpsConnectionContext, Http }
    import akka.stream.ActorMaterializer
    import com.typesafe.sslconfig.akka.AkkaSSLConfig
    implicit val system = ActorSystem()
    implicit val mat = ActorMaterializer()
    implicit val dispatcher = system.dispatcher
    
    // Manual HTTPS configuration
    
    val password: Array[Char] = "change me".toCharArray // do not store passwords in code, read them from somewhere safe!
    
    val ks: KeyStore = KeyStore.getInstance("PKCS12")
    val keystore: InputStream = getClass.getClassLoader.getResourceAsStream("server.p12")  
    //数字证书是固定的p12文件格式 require(keystore
    != null, "Keystore required!") ks.load(keystore, password) val keyManagerFactory: KeyManagerFactory = KeyManagerFactory.getInstance("SunX509") keyManagerFactory.init(ks, password) val tmf: TrustManagerFactory = TrustManagerFactory.getInstance("SunX509") tmf.init(ks) val sslContext: SSLContext = SSLContext.getInstance("TLS") sslContext.init(keyManagerFactory.getKeyManagers, tmf.getTrustManagers, new SecureRandom) val https: HttpsConnectionContext = ConnectionContext.https(sslContext)

        3、但是不是什么官方的证书都可以的。有严格要求 

    • 对应的域名的证书(如*.a.com的证书不能应用到s.b.com的server服务器上。会造成证书错误的问题,与此站点不安全的结果,更甚至会导致数字证书泄露)
    • 严格的p12文档证书 

       4 、一个server服务器能够同时允许https和http,但是两种方法一起运行是因为是不能是同一个地址,会报错。

              一个server需要同时运行http和https的话就需要准备两个端口地址

           即单个应用程序中运行HTTP和HTTPS服务器,则可以调用bind...两次方法,一种用于HTTPS,另一种用于HTTP。

          具体代码如下:

    上面的2的https的代码也要调用
    // you can run both HTTP and HTTPS in the same application as follows:
    val commonRoutes: Route = get { complete("Hello world!") }
    Http().bindAndHandle(commonRoutes, "127.0.0.1", 443, connectionContext = https)
    Http().bindAndHandle(commonRoutes, "127.0.0.1", 80)

       5、http重定向https

     在akka-http的官方文档中:https://doc.akka.io/docs/akka-http/current/routing-dsl/directives/scheme-directives/scheme.html#description

    import akka.http.scaladsl.model._
    import akka.http.scaladsl.model.headers.Location
    import StatusCodes.MovedPermanently
    
    val route =
      concat(
        scheme("http") {
          extract(_.request.uri) { uri =>
            redirect(uri.copy(scheme = "https").withPort(443), MovedPermanently)
           //重定向的http的server就是类似于进入 一个新网址,即被换的Uri
          }
        },
        scheme("https") {
          complete(s"Safe and secure!")
        }
      )
    
    // tests:
    Get("http://www.example.com/hello") ~> route ~> check {
      status shouldEqual MovedPermanently
      header[Location] shouldEqual Some(Location(Uri("https://www.example.com/hello")))
    }
    
    Get("https://www.example.com/hello") ~> route ~> check {
      responseAs[String] shouldEqual "Safe and secure!"
    }

    在重定向的时候,一开始是是只能进入https的网址,无法进入scheme(“http”)内部,但是后来才发现,

    必须同时具备:一个http的server 和一个https的server,即配置https与http并行如下:

    而80和443即是http和http带域名能够隐藏的端口。

    Http().bindAndHandle(commonRoutes, "127.0.0.1", 443, connectionContext = https)
    Http().bindAndHandle(commonRoutes, "127.0.0.1", 80)
     
     
  • 相关阅读:
    asp.net页面生命周期追踪
    asp.net Forums 之配置,缓存,多数据访问
    沪江技术部程序员招聘试题,大家一起讨论一下。
    httpd does not appear to be running and proxying cobbler, or SELinux is in the way.
    网络知识OSI七层网络与TCP/IP五层网络架构及二层/三层网络
    python中用psutil模块,yagmail模块监控CPU、硬盘、内存使用,阈值后发送邮件
    Linux中访问Apache报403错误处理方法
    centos7的启动流程
    pycharm介绍
    监测NGINX服务的shell脚本
  • 原文地址:https://www.cnblogs.com/0205gt/p/12719408.html
Copyright © 2011-2022 走看看