Programmatically set Jetty configuration to increase allowed URL length

♀尐吖头ヾ 提交于 2020-12-05 06:00:35

问题


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.

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?


回答1:


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();
}

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.



来源:https://stackoverflow.com/questions/31792358/programmatically-set-jetty-configuration-to-increase-allowed-url-length

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!