Java 有没有一种简单的方法来实现代理HTTP请求并将其转换为SSL?
我正在寻找一个基本上将为我执行所有SSL通信的代理,但我(客户机)只请求HTTP 我尝试实现我自己的,但这是一项非常艰巨的工作。我正在寻找一个简单/平滑的API来构建这种代理Java 有没有一种简单的方法来实现代理HTTP请求并将其转换为SSL?,java,python,ssl,proxy,Java,Python,Ssl,Proxy,我正在寻找一个基本上将为我执行所有SSL通信的代理,但我(客户机)只请求HTTP 我尝试实现我自己的,但这是一项非常艰巨的工作。我正在寻找一个简单/平滑的API来构建这种代理 任何提示?如果这是传入流量,您可以寻找如下解决方案: 如果这是用于传出请求,则可以使用请求:下面是我使用的代理示例;它将转换任何http://stackoverflow.com请求到https:// HttpClient downstream = new HttpClient(); HttpHandler
任何提示?如果这是传入流量,您可以寻找如下解决方案:
如果这是用于传出请求,则可以使用请求:下面是我使用的代理示例;它将转换任何
http://stackoverflow.com
请求到https://
HttpClient downstream = new HttpClient();
HttpHandler handler = request->
{
...
if( !request.isHttps() && request.host().equals("stackoverflow.com"))
{
request = new HttpRequestImpl(request).isHttps(true);
System.err.println("transformed: "+request.absoluteUri());
}
return downstream.send0(request, null);
};
HttpServer proxy = new HttpServer(handler);
请参阅
您还可以检查和转换服务器响应。例如,如果服务器想要重定向到https://xxx
,我们想将其修复为http://xxx
HttpHandler handler = request->
{
...
return downstream
.send0(request, null)
.map( response->fixRedirect(response) );
}
static HttpResponse fixRedirect(HttpResponse serverResponse)
{
if(serverResponse.statusCode()!=303)
return serverResponse;
String location = serverResponse.header(Headers.Location);
if(location==null || !location.startsWith("https"))
return serverResponse;
// server tries to redirect to an https://xxx url. change it to http://xxx
location = "http" + location.substring(5);
return new HttpResponseImpl(serverResponse)
.header(Headers.Location, location);
}
要信任任何服务器SSL证书,我们需要配置下游
// trust all certs
SslConf sslConf = new SslConf()
.trustAll();
// disable host name verification
ConsumerX<SSLEngine> sslEngineConf = engine->
{
SSLParameters sslParameters = engine.getSSLParameters();
sslParameters.setEndpointIdentificationAlgorithm(null);
engine.setSSLParameters(sslParameters);
};
HttpClientConf clientConf = new HttpClientConf()
.sslEngineConf(sslEngineConf)
.sslContext(sslConf.createContext());
HttpClient downstream = new HttpClient(clientConf);
//信任所有证书
SslConf SslConf=新的SslConf()
.trustAll();
//禁用主机名验证
ConsumerX sslEngineConf=引擎->
{
SSLParameters SSLParameters=engine.getSSLParameters();
sslParameters.setEndpointIdentificationAlgorithm(null);
发动机参数(SSL参数);
};
HttpClientConf clientConf=新的HttpClientConf()
.sslEngineConf(sslEngineConf)
.sslContext(sslConf.createContext());
HttpClient下游=新的HttpClient(clientConf);
hmm。。你为什么需要这个?你可以看看我的。基本上,代理接受一个HttpRequest
,将其转换为=true
,并将其发送到下游。我认为这可能是因为“HTTP公钥锁定”。该浏览器有一个已知主机列表,包括facebook;浏览器不会为这些主机发送HTTP或通过代理;任何人都不能站在中间。不知道如何在Firefox中配置它。请尝试可能尚未支持该功能的IE。请不要诋毁您的问题。作为Stack Overflow的新成员,您可能需要在某个时候阅读介绍。@bayou.io好的,谢谢。我试过了,但它仍然拒绝连接。也许是因为它将我重定向到HTTPS?如果是这样,是否可以获取响应头并将https位置重定向替换为http,以便请求可以通过代理作为http并由me@bayou.io转换?@bayou.io由于http请求将我重定向到https,代理拒绝连接,因为它的设置是http代理。有没有办法使用lib将HTTPS重定向设置为仅HTTP?谢谢你的回答!哇,正是我要找的!有没有办法使其通用化,以便将该端口上的所有http转换为https?而且,它是否支持post数据等,而无需我添加80行代码?谢谢,但不是每个网站都支持HTTPS。。。许多小型网站不这样做。向仅HTTP的服务器发送HTTPS请求无效。是的,支持POST和所有常见功能。