2016-04-17 118 views
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

问候,

巴萨姆

回答

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); 
+0

谢谢@eudgee,我会试试这个。我正在使用来自[scala示例]的证书(https://www.codatlas.com/github.com/akka/akka/HEAD/akka-http-core/src/test/scala/akka/http/impl/util /ExampleHttpContexts.scala) –