add Https Support by ag2s20150909 · Pull Request #3 · weeChanc/AndroidHttpServer · GitHub
Skip to content

add Https Support#3

Open
ag2s20150909 wants to merge 1 commit into
weeChanc:masterfrom
ag2s20150909:master
Open

add Https Support#3
ag2s20150909 wants to merge 1 commit into
weeChanc:masterfrom
ag2s20150909:master

Conversation

@ag2s20150909

Copy link
Copy Markdown

fix #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()

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

HTTPS support

1 participant