@@ -197,7 +197,6 @@ public class ReverseHttpProxy {
197
197
);
198
198
199
199
200
-
201
200
public ReverseHttpProxy (HttpServer httpServer , HttpClient httpClient , Router router , String name ) {
202
201
this .httpServer = httpServer ;
203
202
this .httpClient = httpClient ;
@@ -602,9 +601,6 @@ protected Handler<RoutingContext> routingContextHandler(HttpClient httpClient) {
602
601
setContextData (ctx , key , route .getMetadata (key ));
603
602
}
604
603
605
- // log.warn("start--{}--{}--{}", getContextData(route, INTERNAL_CLIENT_LOCAL_ADDR), getContextData(route, INTERNAL_SERVER_REMOTE_ADDR), getContextData(route, INTERNAL_PROXY_URL));
606
-
607
-
608
604
// 记录请求开始时间
609
605
setContextData (ctx , INTERNAL_SEND_TIMESTAMP , System .currentTimeMillis ());
610
606
// 记录连接状态
@@ -644,6 +640,26 @@ protected Handler<RoutingContext> routingContextHandler(HttpClient httpClient) {
644
640
log .debug ("proxyServer remote connection {} closed" , getContextData (ctx , INTERNAL_SERVER_REMOTE_ADDR ).toString ());
645
641
});
646
642
643
+ // 如果跨域由代理服务接管,那么针对跨域使用的OPTIONS预检请求,就由代理服务接管,而不经过实际的后端服务
644
+ if (HttpMethod .OPTIONS .name ().equalsIgnoreCase (serverReq .method ().name ()) &&
645
+ getContextData (ctx , P_CORS_CONTROL ) != null && Boolean .parseBoolean (getContextData (ctx , P_CORS_CONTROL ).toString ()) &&
646
+ getContextData (ctx , P_ALLOW_CORS ) != null && Boolean .parseBoolean (getContextData (ctx , P_ALLOW_CORS ).toString ())
647
+ ) {
648
+ String header = serverReq .getHeader ("origin" );
649
+ if (header == null || header .isEmpty ()) {
650
+ serverResp .putHeader ("Access-Control-Allow-Origin" , "*" );
651
+ } else {
652
+ serverResp .putHeader ("Access-Control-Allow-Origin" , header );
653
+ }
654
+ serverResp .putHeader ("Access-Control-Allow-Methods" , "*" );
655
+ serverResp .putHeader ("Access-Control-Allow-Headers" , "*" );
656
+ serverResp .putHeader ("Access-Control-Allow-Credentials" , "true" );
657
+ serverResp .putHeader ("Access-Control-Expose-Headers" , "*" );
658
+ setStatusCode (ctx , serverResp , 200 ).end ();
659
+ doLog (ctx );
660
+ return ;
661
+ }
662
+
647
663
// 请求
648
664
if ((boolean ) getContextData (ctx , INTERNAL_CLIENT_CONNECTION_OPEN )) {
649
665
httpClient .request (requestOptions ).onComplete (connectHandler (ctx , serverReq , serverResp , proxyUrl ));
0 commit comments