2
我正在创建一个小型代理来拦截所有本地http请求,然后使用远程代理对它们进行授权。这里是代码片段:使用Akka-http处理HTTPS请求[java]
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的例子
任何想法,谢谢?
注:
Documentation has no example specially in Java
问候,
巴萨姆
谢谢@eudgee,我会试试这个。我正在使用来自[scala示例]的证书(https://www.codatlas.com/github.com/akka/akka/HEAD/akka-http-core/src/test/scala/akka/http/impl/util /ExampleHttpContexts.scala) –