private void customizeConnectionTimeout( JettyEmbeddedServletContainerFactory factory, final int connectionTimeout) { factory.addServerCustomizers(new JettyServerCustomizer() { @Override public void customize(Server server) { for (org.eclipse.jetty.server.Connector connector : server .getConnectors()) { if (connector instanceof AbstractConnector) { ((AbstractConnector) connector) .setIdleTimeout(connectionTimeout); } } } }); }
@Bean public EmbeddedServletContainerCustomizer disableServerHeader() { return container -> { if (container instanceof JettyEmbeddedServletContainerFactory) { ((JettyEmbeddedServletContainerFactory) container).addServerCustomizers(new JettyServerCustomizer() { @Override public void customize(Server server) { for (Connector connector : server.getConnectors()) { if (connector instanceof ServerConnector) { HttpConnectionFactory connectionFactory = connector.getConnectionFactory(HttpConnectionFactory.class); connectionFactory.getHttpConfiguration().setSendServerVersion(false); } } } }); } }; }
@Bean public JettyEmbeddedServletContainerFactory jettyEmbeddedServletContainerFactory( @Value("${server.port:8080}") final String port, @Value("${jetty.threadPool.maxThreads:200}") final String maxThreads, @Value("${jetty.threadPool.minThreads:8}") final String minThreads, @Value("${jetty.threadPool.idleTimeout:60000}") final String idleTimeout) { final JettyEmbeddedServletContainerFactory factory = new JettyEmbeddedServletContainerFactory(Integer.valueOf(port)); factory.addServerCustomizers((JettyServerCustomizer) server -> { final QueuedThreadPool threadPool = server.getBean(QueuedThreadPool.class); threadPool.setMaxThreads(Integer.valueOf(maxThreads)); threadPool.setMinThreads(Integer.valueOf(minThreads)); threadPool.setIdleTimeout(Integer.valueOf(idleTimeout)); final GzipHandler gzipHandler = new GzipHandler(); gzipHandler.setHandler(server.getHandler()); gzipHandler.setSyncFlush(true); server.setHandler(gzipHandler); }); return factory; }
@Override public Collection<JettyServerCustomizer> getServerCustomizers() { Collection<JettyServerCustomizer> jettyServerCustomizers = super.getServerCustomizers(); jettyServerCustomizers.add(server -> { // Customize the connection pool used by Jetty for very large dataset // which takes more than 30s to be rendered QueuedThreadPool threadPool = server.getBean(QueuedThreadPool.class); threadPool.setMaxThreads(Integer.valueOf(maxThreads)); threadPool.setMinThreads(Integer.valueOf(minThreads)); threadPool.setIdleTimeout(Integer.valueOf(idleTimeout)); for (Connector connector : server.getConnectors()) { if (connector instanceof ServerConnector) { ((ServerConnector) connector).setIdleTimeout(Long.parseLong(idleTimeout)); } } }); return jettyServerCustomizers; }
@Bean public EmbeddedServletContainerFactory servletContainer() { JettyEmbeddedServletContainerFactory factory = new JettyEmbeddedServletContainerFactory(); factory.addServerCustomizers(new JettyServerCustomizer() { public void customize(final Server server) { // Tweak the connection pool used by Jetty to handle incoming // HTTP connections Integer localServerMaxThreads = deriveValue(serverMaxThreads, DEFAULT_SERVER_MAX_THREADS); Integer localServerMinThreads = deriveValue(serverMinThreads, DEFAULT_SERVER_MIN_THREADS); Integer localServerIdleTimeout = deriveValue(serverIdleTimeout, DEFAULT_SERVER_IDLE_TIMEOUT); logger.info("Container Max Threads={}", localServerMaxThreads); logger.info("Container Min Threads={}", localServerMinThreads); logger.info("Container Idle Timeout={}", localServerIdleTimeout); final QueuedThreadPool threadPool = server.getBean(QueuedThreadPool.class); threadPool.setMaxThreads(Integer.valueOf(localServerMaxThreads)); threadPool.setMinThreads(Integer.valueOf(localServerMinThreads)); threadPool.setIdleTimeout(Integer.valueOf(localServerIdleTimeout)); } }); return factory; }
@Bean public EmbeddedServletContainerFactory servletContainer(SslProperties properties) { JettyEmbeddedServletContainerFactory jetty = new JettyEmbeddedServletContainerFactory() { @Override protected void postProcessWebAppContext(WebAppContext webAppContext) { // 所有请求都必须为 Https 协议 ConstraintSecurityHandler securityHandler = new ConstraintSecurityHandler(); ConstraintMapping mapping = new ConstraintMapping(); Constraint constraint = new Constraint(); constraint.setDataConstraint(Constraint.DC_CONFIDENTIAL); mapping.setConstraint(constraint); mapping.setPathSpec("/*"); securityHandler.addConstraintMapping(mapping); webAppContext.setSecurityHandler(securityHandler); } }; jetty.addServerCustomizers(new JettyServerCustomizer() { @Override public void customize(Server server) { // 移除Spring Boot 生成的 Connector int httpPort = 80; Connector[] connectors = server.getConnectors(); for (Connector connector : connectors) { if (connector instanceof ServerConnector) { httpPort = ((ServerConnector) connector).getPort(); } server.removeConnector(connector); } // 配置 Http 协议的 Connector HttpConfiguration httpConfig = new HttpConfiguration(); // 重定向 httpConfig.setSecureScheme(properties.getScheme()); httpConfig.setSecurePort(properties.getPort()); httpConfig.addCustomizer(new SecureRequestCustomizer()); ServerConnector httpConnector = new ServerConnector(server, new HttpConnectionFactory(httpConfig)); httpConnector.setPort(httpPort); server.addConnector(httpConnector); // 配置 Https 协议的 Connector HttpConfiguration httpsConfig = new HttpConfiguration(httpConfig); httpsConfig.addCustomizer(new SecureRequestCustomizer()); HttpConnectionFactory connectionFactory = new HttpConnectionFactory(httpsConfig); SslContextFactory sslContextFactory = new SslContextFactory(); File keystoreFile = new File(properties.getKeystore()); if (keystoreFile.exists()) { sslContextFactory.setKeyStorePath(properties.getKeystore()); sslContextFactory.setKeyStorePassword(properties.getKeystorePassword()); } SslConnectionFactory sslConnectionFactory = new SslConnectionFactory(sslContextFactory, HttpVersion.HTTP_1_1.asString()); ServerConnector serverConnector = new ServerConnector(server, sslConnectionFactory, connectionFactory); serverConnector.setPort(properties.getPort()); server.addConnector(serverConnector); } }); return jetty; }
/** * @return Jetty config bean */ @Bean public EmbeddedServletContainerFactory jettyConfigBean() { JettyEmbeddedServletContainerFactory jef = new JettyEmbeddedServletContainerFactory(); jef.addServerCustomizers(new JettyServerCustomizer() { public void customize(Server server) { if (Config.getConfigBoolean("access_log_enabled", true)) { // enable access log via Logback HandlerCollection handlers = new HandlerCollection(); for (Handler handler : server.getHandlers()) { handlers.addHandler(handler); } RequestLogHandler reqLogs = new RequestLogHandler(); reqLogs.setServer(server); RequestLogImpl rli = new RequestLogImpl(); rli.setResource("/logback-access.xml"); rli.setQuiet(true); rli.start(); reqLogs.setRequestLog(rli); handlers.addHandler(reqLogs); server.setHandler(handlers); } for (Connector y : server.getConnectors()) { for (ConnectionFactory cf : y.getConnectionFactories()) { if (cf instanceof HttpConnectionFactory) { HttpConnectionFactory dcf = (HttpConnectionFactory) cf; // support for X-Forwarded-Proto // redirect back to https if original request uses it if (Config.IN_PRODUCTION) { HttpConfiguration httpConfiguration = dcf.getHttpConfiguration(); httpConfiguration.addCustomizer(new ForwardedRequestCustomizer()); } // Disable Jetty version header dcf.getHttpConfiguration().setSendServerVersion(false); } } } } }); int defaultPort = NumberUtils.toInt(System.getProperty("jetty.http.port", "8080")); jef.setPort(NumberUtils.toInt(System.getProperty("server.port"), defaultPort)); logger.info("Listening on port {}...", jef.getPort()); return jef; }