weeChanc / AndroidHttpServer

simple service + simple demo
27 stars 4 forks source link

add Https Support #3

Open ag2s20150909 opened 2 years ago

ag2s20150909 commented 2 years ago

fix https://github.com/weeChanc/AndroidHttpServer/issues/2 先获取SSLContext

 private fun getSSLContext(context: Context): SSLContext {
        val keyStore = KeyStore.getInstance(KeyStore.getDefaultType())
        val keyStream: InputStream =
            context.applicationContext.assets.open("server.bks") //打开证书文件(.bks格式)
        val keyStorePass = "xxxxxx".toCharArray() //证书密码
        keyStore.load(keyStream, keyStorePass)
        val keyManagerFactory =
            KeyManagerFactory.getInstance(KeyManagerFactory.getDefaultAlgorithm())
        keyManagerFactory.init(keyStore, keyStorePass)
        val clientContext = SSLContext.getInstance("TLS")
        clientContext.init(keyManagerFactory.keyManagers, null, null)
        return clientContext
    }

然后这样使用

   val service = HttpServerBuilder
            .with(this)
            .port(8080)
            .socketFactory(getSSLContext(this).serverSocketFactory)
            .getHttpServer()