【问题标题】:Handling HTTPS requests with Akka-http [java]使用 Akka-http [java] 处理 HTTPS 请求
【发布时间】:2016-04-17 09:10:05
【问题描述】:

我正在创建一个小型代理来拦截所有本地 http 请求,然后使用远程代理授权它们。这是代码sn-p:

Source<IncomingConnection, CompletionStage<ServerBinding>> serverSource =
                Http.get(system).bind(ConnectWithHttps.toHost("localhost", LOCAL_PORT), materializer);
        Authorization authorization = Authorization.basic(USERNAME, PASSWORD);

        Flow<HttpRequest, HttpRequest, NotUsed> applySecurity =
                Flow.of(HttpRequest.class)
                    .map(request -> request.addHeader(authorization));

        Flow<HttpRequest, HttpResponse, CompletionStage<OutgoingConnection>> forwardToProxy =
                Http.get(system).outgoingConnection(ConnectWithHttps.toHost(PROXY_ADDRESS, PROXY_PORT));

        Flow<HttpRequest, HttpResponse, NotUsed> handler =
                Flow.of(HttpRequest.class)
                   .via(applySecurity)
                .via(forwardToProxy);


    serverSource.to(Sink.foreach(conn -> {
             System.out.println("Accepted new connection from " + conn.remoteAddress());
             conn.handleWith(handler, materializer);
                }
 )).run(materializer);

我还需要处理 Https 请求。但我总是得到这个错误:

[WARN] [04/14/2016 00:07:28.480] [MyActorSystem-akka.actor.default-dispatcher-13] [akka.actor.ActorSystemImpl(MyActorSystem)] Illegal request, responding with status '400 Bad Request': Request is missing required `Host` header: 'Host' header value of request to `eg.linkedin.com:443` doesn't match request target authority: Host header: Some(Host: eg.linkedin.com:443)

我尝试使用“ConnectWithHttps”而不是“ConnectHttp”,但没有。我找不到任何与 Https 相关的示例

有什么想法吗,谢谢?

注意:

文档没有专门的Java示例

问候,

巴桑

【问题讨论】:

    标签: https akka akka-stream akka-http


    【解决方案1】:

    尝试改变

    ConnectWithHttps.toHost("localhost", LOCAL_PORT)
    

    ConnectWithHttps.toHost("localhost", LOCAL_PORT).withCustomHttpsContext(ctx)
    

    然后您需要创建 HttpsConnectionContext 以使其工作 (ctx)。如何做到这一点的简单示例:

    char[] password = config.getString("https.password").toCharArray();
    SSLContext context = SSLContext.getInstance("TLS");
    KeyStore ks = KeyStore.getInstance("PKCS12");
    
    ks.load(App.class.getClassLoader().getResourceAsStream("keys/server.p12"), password);
    KeyManagerFactory keyManagerFactory = KeyManagerFactory.getInstance("SunX509");
    
    keyManagerFactory.init(ks, password);
    context.init(keyManagerFactory.getKeyManagers(), null, new SecureRandom());
    
    HttpsConnectionContext ctx = ConnectionContext.https(context);
    

    【讨论】:

    • 谢谢@eudgee,我会试试这个。我正在使用来自 scala example 的证书
    猜你喜欢
    • 1970-01-01
    • 2018-04-29
    • 1970-01-01
    • 2018-08-28
    • 1970-01-01
    • 1970-01-01
    • 2022-11-20
    • 2016-04-05
    • 1970-01-01
    相关资源
    最近更新 更多