删除Jetty 9中的HTTP Server标头

Jac*_*cob 18 java jetty

这是您在Jetty 8中隐藏服务器版本的方法:

Server server = new Server(port);
server.setSendServerVersion(false);
Run Code Online (Sandbox Code Playgroud)

你是怎么在Jetty 9做的?所以现在看起来应该是这样的?

HttpConfiguration config = new HttpConfiguration();
config.setSendServerVersion(false);
//TODO: Associate config with server???
Server server = new Server(port);
Run Code Online (Sandbox Code Playgroud)

djs*_*hny 30

在Jetty 9中,您需要在HttpConfiguration上配置它:

HttpConfiguration httpConfig = new HttpConfiguration();
httpConfig.setSendServerVersion( false );
HttpConnectionFactory httpFactory = new HttpConnectionFactory( httpConfig );
ServerConnector httpConnector = new ServerConnector( server,httpFactory );
server.setConnectors( new Connector[] { httpConnector } );
Run Code Online (Sandbox Code Playgroud)

  • 你应该添加最后一步:`server.setConnectors(new Connector [] {httpConnector});`. (6认同)
  • 为新连接器设置端口的重要位是:`httpConnector.setPort(port);`.由于正在替换默认连接器,因此不使用传递给`Server`构造函数的端口. (2认同)

Jac*_*cob 23

如果计算出一些似乎有效的代码.不确定它是否正确,但至少它是否有效(:

Server server = new Server(port);
for(Connector y : server.getConnectors()) {
    for(ConnectionFactory x  : y.getConnectionFactories()) {
        if(x instanceof HttpConnectionFactory) {
            ((HttpConnectionFactory)x).getHttpConfiguration().setSendServerVersion(false);
        }
    }
}
Run Code Online (Sandbox Code Playgroud)


Tar*_*tor 10

如果将jetty9用作独立服务器,则可以通过jetty.httpConfig.sendServerVersion=false在文件中进行设置来禁用服务器签名start.ini.