带有 SSL 支持的 Jetty ProxyServlet [英] Jetty ProxyServlet with SSL support

查看:24
本文介绍了带有 SSL 支持的 Jetty ProxyServlet的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用 Jetty 的 ProxyServlet 作为 HTTP 代理.

I am using Jetty's ProxyServlet as a HTTP proxy.

启动服务器并在firefox中添加socks代理后,我可以通过代理访问网站,没有任何问题.

After I start the server and add the socks proxy in firefox I can access websites through the proxy without any problems.

问题是当我尝试通过代理访问 HTTPS 网站时.Firefox 显示未找到服务器"错误,在调试期间我没有看到我的 Java 代码中发生任何事情.

The problem is that when I try to access a HTTPs website through the proxy. Firefox displays a "Server not found" error and during debugging I don't see anything happening in my Java code.

我是否缺少向 Jetty 添加 SSL 支持的内容?

Am I missing something here to add SSL support to Jetty?

这是部分代码:

    Server httpProxy = new Server(8087);

    ServletHandler servletHandler = new ServletHandler();
    servletHandler.addServletWithMapping(new ServletHolder(new TunnelProxyServlet()), "/*");

    httpProxy.setHandler(servletHandler);
    try {
        httpProxy.start();
    } catch (Exception ex) {
        Logger.getLogger(HttpProxy.class.getName()).log(Level.SEVERE, null, ex);
    }

    public class TunnelProxyServlet extends ProxyServlet {
      @Override
      public void init(ServletConfig config) throws ServletException {
        super.init(config);
        System.out.println("init done !");
      }

      @Override
      public void service(ServletRequest req, ServletResponse res) throws ServletException, IOException {
        System.out.println("got a request !");
        super.service(req, res);
      } 
   }

推荐答案

您可以使用ConnectHandler"

You can use a "ConnectHandler"

http://grepcode.com/file/repo1.maven.org/maven2/org.eclipse.jetty/example-jetty-embedded/8.1.1.v20120215/org/eclipse/jetty/embedded/ProxyServer.java

public class ProxyServer {

    public static void main(String[] args) throws Exception {

        Server server = new Server();
        SelectChannelConnector connector = new SelectChannelConnector();
        connector.setPort(8888);
        server.addConnector(connector);

        HandlerCollection handlers = new HandlerCollection();
        server.setHandler(handlers);

        // Setup proxy servlet
        ServletContextHandler context = new ServletContextHandler(handlers, "/", ServletContextHandler.SESSIONS);
        ServletHolder proxyServlet = new ServletHolder(ProxyServlet.class);
        proxyServlet.setInitParameter("whiteList", "google.com, www.eclipse.org, localhost");
        proxyServlet.setInitParameter("blackList", "google.com/calendar/*, www.eclipse.org/committers/");
        context.addServlet(proxyServlet, "/*");

        // Setup proxy handler to handle CONNECT methods
        ConnectHandler proxy = new ConnectHandler();
        proxy.setWhite(new String[]{"mail.google.com"});
        proxy.addWhite("www.google.com");
        handlers.addHandler(proxy);

        server.start();

    }

}

这篇关于带有 SSL 支持的 Jetty ProxyServlet的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆