以编程方式设置Jetty配置以增加允许的URL长度 [英] Programmatically set Jetty configuration to increase allowed URL length

查看:278
本文介绍了以编程方式设置Jetty配置以增加允许的URL长度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们正在使用嵌入式Jetty 9.3.1.v20150714,并遇到了问题,其中我们的长查询网址是与其他标头结合使用的时间要长于允许的长度.

We're using embedded Jetty 9.3.1.v20150714 and ran into the problem in which our long query URL, combined with the other headers, were longer than those allowed.

解决方案似乎很简单:增加HttpConfiguration中的requestHeaderSize.但是,我该如何轻松地做到这一点?我当前正在创建ServerServletContextHandlerServletHolder.但是要混入自定义的HttpConfiguration,是否必须创建新的ServerConnectorHttpConnectionFactory?我是否必须覆盖HTTP和HTTPS配置?如何轻松更改requestHeaderSize而无需重新配置所有默认值?

The solution seems straightforward: increase the requestHeaderSize in HttpConfiguration. But how do I do that easily? I'm currently creating a Server, a ServletContextHandler, and a ServletHolder. But to mix in a custom HttpConfiguration, do I have to create a new ServerConnector and HttpConnectionFactory? Do I have to override the HTTP and HTTPS configurations? How can I easily just change requestHeaderSize without reconfiguring all the defaults?

推荐答案

如果仅设置一个属性,则可以在默认实例化的HttpConfiguration上进行设置:

If you're just setting that one property, you can set it on the HttpConfiguration that was instantiated by default:

public static void main(String[] args) throws Exception {
    Server server = new Server(8080);
    server.setHandler(new DefaultHandler()); // 404s for everything except favicon.ico

    for (Connector c : server.getConnectors()) {
        c.getConnectionFactory(HttpConnectionFactory.class).getHttpConfiguration().setRequestHeaderSize(65535);
    }

    server.start();
    server.join();
}

您不必单独覆盖HTTPS配置,因为根据您对当前实例化内容的描述,您没有任何HTTPS连接器.即使您确实具有HTTPS连接器,上述循环也可以工作,因为为HTTPS配置的ServerConnector仍将具有关联的HttpConnectionFactory.您可以在中查看如何配置HTTPS连接器.这个例子.

You don't have to separately override the HTTPS configuration, because based on your description of what you're currently instantiating, you don't have any HTTPS connectors. Even if you did have an HTTPS connector though, the above loop would work because a ServerConnector configured for HTTPS would still have an associated HttpConnectionFactory. You can see how an HTTPS connector would be configured in this example.

但是,实际上不需要太多代码来自己设置必要的对象:

However, it really isn't all that much code to set up the necessary objects yourself:

public static void main(String[] args) throws Exception {
    Server server = new Server();
    server.setHandler(new DefaultHandler()); // 404s for everything except favicon.ico

    HttpConfiguration config = new HttpConfiguration();
    config.setRequestHeaderSize(65535);
    ServerConnector http = new ServerConnector(server, new HttpConnectionFactory(config));
    http.setPort(8080);
    server.setConnectors(new Connector[] {http});

    server.start();
    server.join();
}

我建议您自己进行设置,因为如果将来进行其他配置更改,维护起来会更容易.

I would recommend doing the setup yourself because it'll be easier to maintain if you have other configuration changes in the future.

这篇关于以编程方式设置Jetty配置以增加允许的URL长度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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