问题
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