org.springframework.boot.web.servlet.server.ServletWebServerFactory Java Examples

The following examples show how to use org.springframework.boot.web.servlet.server.ServletWebServerFactory. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: HttpsConfiguration.java    From nbp with Apache License 2.0 7 votes vote down vote up
@Bean
public ServletWebServerFactory servletContainer() {
    TomcatServletWebServerFactory tomcat = new TomcatServletWebServerFactory() {
        @Override
        protected void postProcessContext(Context context) {
            SecurityConstraint securityConstraint = new SecurityConstraint();
            securityConstraint.setUserConstraint("CONFIDENTIAL");
            SecurityCollection collection = new SecurityCollection();
            collection.addPattern("/*");
            securityConstraint.addCollection(collection);
            context.addConstraint(securityConstraint);
        }
    };
    tomcat.addAdditionalTomcatConnectors(redirectConnector());
    return tomcat;
}
 
Example #2
Source File: TomcatHttpConfig.java    From Java-API-Test-Examples with Apache License 2.0 7 votes vote down vote up
/**
 * 配置内置的Servlet容器工厂为Tomcat
 * @return
 */
@Bean
public ServletWebServerFactory servletContainer() {
	TomcatServletWebServerFactory tomcat = new TomcatServletWebServerFactory() {
		@Override
		protected void postProcessContext(Context context) {
			SecurityConstraint securityConstraint = new SecurityConstraint();
			securityConstraint.setUserConstraint("CONFIDENTIAL");
			SecurityCollection collection = new SecurityCollection();
			collection.addPattern("/*");
			securityConstraint.addCollection(collection);
			context.addConstraint(securityConstraint);
		}
	};
	//添加配置信息,主要是Http的配置信息
	tomcat.addAdditionalTomcatConnectors(redirectConnector());
	return tomcat;
}
 
Example #3
Source File: HomeController.java    From odo with Apache License 2.0 6 votes vote down vote up
@Bean
public ServletWebServerFactory servletContainer() throws Exception {
    TomcatServletWebServerFactory factory = new TomcatServletWebServerFactory();

    int apiPort = Utils.getSystemPort(Constants.SYS_API_PORT);
    factory.setPort(apiPort);
    factory.getSession().setTimeout(Duration.ofMinutes(10));
    factory.setContextPath("/testproxy");
    baseDirectory = new File("./tmp");
    factory.setBaseDirectory(baseDirectory);
    List<TomcatConnectorCustomizer> cs = new ArrayList();
    cs.add(tomcatConnectorCustomizers());
    factory.setTomcatConnectorCustomizers(cs);

    if (Utils.getEnvironmentOptionValue(Constants.SYS_LOGGING_DISABLED) != null) {
        HistoryService.getInstance().disableHistory();
    }
    return factory;
}
 
Example #4
Source File: LayuiAdminStartUp.java    From layui-admin with MIT License 6 votes vote down vote up
@Bean
public ServletWebServerFactory servletContainer() {
    TomcatServletWebServerFactory tomcat = new TomcatServletWebServerFactory();
    tomcat.addInitializers(new ServletContextInitializer(){
        @Override
        public void onStartup(ServletContext servletContext) throws ServletException {
            XmlWebApplicationContext context = new XmlWebApplicationContext();
            context.setConfigLocations(new String[]{"classpath:applicationContext-springmvc.xml"});
            DispatcherServlet dispatcherServlet = new DispatcherServlet(context);
            ServletRegistration.Dynamic dispatcher = servletContext
                    .addServlet("dispatcher", dispatcherServlet);

            dispatcher.setLoadOnStartup(1);
            dispatcher.addMapping("/");
        }
    });
    tomcat.setContextPath("/manager");
    tomcat.addTldSkipPatterns("xercesImpl.jar","xml-apis.jar","serializer.jar");
    tomcat.setPort(port);

    return tomcat;
}
 
Example #5
Source File: CustomerSvcShedLoadConfig.java    From cloud-espm-cloud-native with Apache License 2.0 5 votes vote down vote up
/**
 *  It is used to register the tomcat valve with the tomcat container.
 *  
 * @return embeddedTomcat
 */
@Bean
public ServletWebServerFactory servletContainerWithSemaphoreRateLimiterValve() {
	TomcatServletWebServerFactory embeddedTomcat = new TomcatServletWebServerFactory();
	embeddedTomcat.addEngineValves(new CustomerShedLoadSemaphoreValve(shedLoad));
	return embeddedTomcat;
}
 
Example #6
Source File: WebServerService.java    From kurento-java with Apache License 2.0 5 votes vote down vote up
@Bean
@ConditionalOnMissingBean
public ServletWebServerFactory servletContainer() {
	TomcatServletWebServerFactory tomcat = new TomcatServletWebServerFactory();
  Connector connector = new Connector("org.apache.coyote.http11.Http11NioProtocol");
  connector.setScheme("http");
  connector.setPort(getAppHttpPort());

  tomcat.addAdditionalTomcatConnectors(connector);
  return tomcat;
}
 
Example #7
Source File: SleuthBenchmarkingSpringApp.java    From spring-cloud-sleuth with Apache License 2.0 5 votes vote down vote up
@Bean
public ServletWebServerFactory servletContainer(
		@Value("${server.port:0}") int serverPort) {
	log.info("Starting container at port [" + serverPort + "]");
	return new TomcatServletWebServerFactory(
			serverPort == 0 ? SocketUtils.findAvailableTcpPort() : serverPort);
}
 
Example #8
Source File: App.java    From danyuan-application with Apache License 2.0 5 votes vote down vote up
public ServletWebServerFactory servletContainer() {
	TomcatServletWebServerFactory tomcat = new TomcatServletWebServerFactory() {
		@Override
		protected void postProcessContext(org.apache.catalina.Context context) {
			SecurityConstraint constraint = new SecurityConstraint();
			constraint.setUserConstraint("CONFIDENTIAL");
			SecurityCollection collection = new SecurityCollection();
			collection.addPattern("/*");
			constraint.addCollection(collection);
			context.addConstraint(constraint);
		}
	};
	tomcat.addAdditionalTomcatConnectors(createHTTPConnector());
	return tomcat;
}
 
Example #9
Source File: TomcatConfiguration.java    From api-layer with Eclipse Public License 2.0 5 votes vote down vote up
@Bean
public ServletWebServerFactory servletContainer() {
    System.setProperty("org.apache.tomcat.util.buf.UDecoder.ALLOW_ENCODED_SLASH", "true");
    System.setProperty("org.apache.catalina.connector.CoyoteAdapter.ALLOW_BACKSLASH", "true");
    TomcatServletWebServerFactory tomcat = new TomcatServletWebServerFactory();
    tomcat.setProtocol(TomcatServletWebServerFactory.DEFAULT_PROTOCOL);
    return tomcat;
}
 
Example #10
Source File: TomcatConfiguration.java    From api-layer with Eclipse Public License 2.0 5 votes vote down vote up
@Bean
public ServletWebServerFactory servletContainer() {
    System.setProperty("org.apache.tomcat.util.buf.UDecoder.ALLOW_ENCODED_SLASH", "true");
    TomcatServletWebServerFactory tomcat = new TomcatServletWebServerFactory();
    tomcat.setProtocol(TomcatServletWebServerFactory.DEFAULT_PROTOCOL);
    return tomcat;
}
 
Example #11
Source File: ProductSvcShedLoadConfig.java    From cloud-espm-cloud-native with Apache License 2.0 5 votes vote down vote up
/**
 * It is used to register the tomcat valve with the tomcat container.
 * 
 * @return embeddedTomcat
 */
@Bean
@ConditionalOnClass({ Servlet.class, Tomcat.class })
public ServletWebServerFactory servletContainerWithSemaphoreRateLimiterValve() {
	TomcatServletWebServerFactory embeddedTomcat = new TomcatServletWebServerFactory();
	embeddedTomcat.addEngineValves(new ProductShedLoadSemaphoreValve(shedLoad));
	return embeddedTomcat;
}
 
Example #12
Source File: WebConfiguration.java    From Spring-Boot-2.0-Cookbook-Second-Edition with MIT License 5 votes vote down vote up
@Bean
public ServletWebServerFactory servletContainer(TomcatSslConnectorProperties properties) {
    TomcatServletWebServerFactory tomcat = new TomcatServletWebServerFactory();
    tomcat.addAdditionalTomcatConnectors(createSslConnector(properties));
    tomcat.setSessionTimeout(Duration.ofMinutes(1));
    return tomcat;
}
 
Example #13
Source File: WebConfiguration.java    From Spring-Boot-2.0-Cookbook-Second-Edition with MIT License 5 votes vote down vote up
@Bean
public ServletWebServerFactory servletContainer(TomcatSslConnectorProperties properties) {
    TomcatServletWebServerFactory tomcat = new TomcatServletWebServerFactory();
    tomcat.addAdditionalTomcatConnectors(createSslConnector(properties));
    tomcat.setSessionTimeout(Duration.ofMinutes(1));
    return tomcat;
}
 
Example #14
Source File: WebConfiguration.java    From Spring-Boot-2.0-Cookbook-Second-Edition with MIT License 5 votes vote down vote up
@Bean
public ServletWebServerFactory servletContainer(TomcatSslConnectorProperties properties) {
    TomcatServletWebServerFactory tomcat = new TomcatServletWebServerFactory();
    tomcat.addAdditionalTomcatConnectors(createSslConnector(properties));
    tomcat.setSessionTimeout(Duration.ofMinutes(1));
    return tomcat;
}
 
Example #15
Source File: WebConfiguration.java    From Spring-Boot-2.0-Cookbook-Second-Edition with MIT License 5 votes vote down vote up
@Bean
public ServletWebServerFactory servletContainer(TomcatSslConnectorProperties properties) {
    TomcatServletWebServerFactory tomcat = new TomcatServletWebServerFactory();
    tomcat.addAdditionalTomcatConnectors(createSslConnector(properties));
    tomcat.setSessionTimeout(Duration.ofMinutes(1));
    return tomcat;
}
 
Example #16
Source File: WebConfiguration.java    From Spring-Boot-2.0-Cookbook-Second-Edition with MIT License 5 votes vote down vote up
@Bean
public ServletWebServerFactory servletContainer(TomcatSslConnectorProperties properties) {
    TomcatServletWebServerFactory tomcat = new TomcatServletWebServerFactory();
    tomcat.addAdditionalTomcatConnectors(createSslConnector(properties));
    tomcat.setSessionTimeout(Duration.ofMinutes(1));
    return tomcat;
}
 
Example #17
Source File: WebConfiguration.java    From Spring-Boot-2.0-Cookbook-Second-Edition with MIT License 5 votes vote down vote up
@Bean
public ServletWebServerFactory servletContainer(TomcatSslConnectorProperties properties) {
    TomcatServletWebServerFactory tomcat = new TomcatServletWebServerFactory();
    tomcat.addAdditionalTomcatConnectors(createSslConnector(properties));
    tomcat.setSessionTimeout(Duration.ofMinutes(1));
    return tomcat;
}
 
Example #18
Source File: WebasebeeApplication.java    From WeBASE-Collect-Bee with Apache License 2.0 5 votes vote down vote up
/**
 * 配置tomcat
 *
 * @return
 */
@Bean
public ServletWebServerFactory servletContainer() {
    TomcatServletWebServerFactory tomcat = new TomcatServletWebServerFactory();
    tomcat.addConnectorCustomizers(gracefulShutdown());
    return tomcat;
}
 
Example #19
Source File: WebConfiguration.java    From Spring-Boot-2.0-Cookbook-Second-Edition with MIT License 5 votes vote down vote up
@Bean
public ServletWebServerFactory servletContainer(TomcatSslConnectorProperties properties) {
    TomcatServletWebServerFactory tomcat = new TomcatServletWebServerFactory();
    tomcat.addAdditionalTomcatConnectors(createSslConnector(properties));
    tomcat.setSessionTimeout(Duration.ofMinutes(1));
    return tomcat;
}
 
Example #20
Source File: TomcatConfig.java    From singleton with Eclipse Public License 2.0 5 votes vote down vote up
@Bean
public ServletWebServerFactory servletContainer(ServerProperties serverProperties) {
	TomcatServletWebServerFactory tomcat = new TomcatServletWebServerFactory();
	tomcat.addConnectorCustomizers(new VIPTomcatConnectionCustomizer(serverProperties));
	if (serverProperties.getServerScheme().equalsIgnoreCase(ConstantsTomcat.HTTP_HTTPS) ||
			serverProperties.getServerScheme().equalsIgnoreCase(ConstantsTomcat.HTTPS_HTTP)) {
		tomcat.addAdditionalTomcatConnectors(initiateHttpsConnector(serverProperties));
	}
	return tomcat;
}
 
Example #21
Source File: TomcatConfig.java    From singleton with Eclipse Public License 2.0 5 votes vote down vote up
@Bean
public ServletWebServerFactory servletContainer(ServerProperties serverProperties) {
	TomcatServletWebServerFactory tomcat = new TomcatServletWebServerFactory();
	tomcat.addConnectorCustomizers(new VIPTomcatConnectionCustomizer(serverProperties));
	if (serverProperties.getServerScheme().equalsIgnoreCase(ConstantsTomcat.HTTP_HTTPS) ||
			serverProperties.getServerScheme().equalsIgnoreCase(ConstantsTomcat.HTTPS_HTTP)) {
		tomcat.addAdditionalTomcatConnectors(initiateHttpsConnector(serverProperties));
	}
	return tomcat;
}
 
Example #22
Source File: TestConfig.java    From staffjoy with MIT License 4 votes vote down vote up
@Bean
public ServletWebServerFactory servletContainer() {
    TomcatServletWebServerFactory tomcat = new TomcatServletWebServerFactory();
    tomcat.addConnectorCustomizers(connector -> connector.setAllowTrace(true));
    return tomcat;
}
 
Example #23
Source File: ParaServer.java    From para with Apache License 2.0 4 votes vote down vote up
/**
 * @return Jetty config bean
 */
@Bean
public ServletWebServerFactory jettyConfigBean() {
	JettyServletWebServerFactory jef = new JettyServletWebServerFactory();
	jef.addServerCustomizers((JettyServerCustomizer) (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) {
						ForwardedRequestCustomizer frc = new ForwardedRequestCustomizer() {
							public void customize(Connector connector, HttpConfiguration config, Request request) {
								super.customize(connector, config, request);
								String cfProto = request.getHeader("CloudFront-Forwarded-Proto");
								if (StringUtils.equalsIgnoreCase(cfProto, config.getSecureScheme())) {
									request.setScheme(cfProto);
									request.setSecure(true);
								}
							}
						};
						HttpConfiguration httpConfiguration = dcf.getHttpConfiguration();
						httpConfiguration.addCustomizer(frc);
					}
					// Disable Jetty version header
					dcf.getHttpConfiguration().setSendServerVersion(false);
					// Increase idle timeout
					dcf.getHttpConfiguration().setIdleTimeout(TimeUnit.MINUTES.toMillis(5));
				}
			}
		}
	});
	String contextPath = Config.getConfigParam("context_path", "");
	if (StringUtils.length(contextPath) > 1 && contextPath.charAt(0) == '/') {
		jef.setContextPath(contextPath);
	}
	jef.setPort(getServerPort());
	logger.info("Listening on port {}...", jef.getPort());
	return jef;
}
 
Example #24
Source File: AppRun.java    From yshopmall with Apache License 2.0 4 votes vote down vote up
@Bean
public ServletWebServerFactory webServerFactory() {
    TomcatServletWebServerFactory fa = new TomcatServletWebServerFactory();
    fa.addConnectorCustomizers(connector -> connector.setProperty("relaxedQueryChars", "[]{}"));
    return fa;
}
 
Example #25
Source File: App.java    From modeldb with Apache License 2.0 4 votes vote down vote up
@Bean
public ServletWebServerFactory servletContainer(final GracefulShutdown gracefulShutdown) {
  TomcatServletWebServerFactory factory = new TomcatServletWebServerFactory();
  factory.addConnectorCustomizers(gracefulShutdown);
  return factory;
}
 
Example #26
Source File: ResourceServerTokenServicesConfigurationTests.java    From spring-security-oauth2-boot with Apache License 2.0 4 votes vote down vote up
@Bean
public ServletWebServerFactory webServerFactory() {
	return new MockServletWebServerFactory();
}
 
Example #27
Source File: CustomContainerWebSocketsApplicationTests.java    From spring-boot-tutorial with Creative Commons Attribution Share Alike 4.0 International 4 votes vote down vote up
@Bean
public ServletWebServerFactory webServerFactory() {
    return new TomcatServletWebServerFactory("/ws", 0);
}
 
Example #28
Source File: HttpServer.java    From guardedbox with GNU Affero General Public License v3.0 4 votes vote down vote up
/**
 * Bean: ServletWebServerFactory.
 * Creates a dual port Tomcat, listening both in an http port and an https port. The http port simply redirects to the https one.
 *
 * @return TomcatServletWebServerFactory.
 */
@Bean
public ServletWebServerFactory servletWebServerFactory() {

    // Check if there is dual port configuration.
    if (serverProperties.getInternalHttpPort() == null
            || serverProperties.getExternalHttpPort() == null
            || serverProperties.getInternalHttpsPort() == null
            || serverProperties.getExternalHttpsPort() == null
            || serverProperties.getPort().equals(serverProperties.getInternalHttpPort())
            || !serverProperties.getPort().equals(serverProperties.getInternalHttpsPort())) {
        return new TomcatServletWebServerFactory();
    }

    // Set TLS ECDH offered curves.
    if (!StringUtils.isEmpty(sslProperties.getEcdhCurves()))
        System.setProperty(JdkProperty.TLS_ECDH_CURVES.getPropertyName(), sslProperties.getEcdhCurves());

    // Enable TLS OCSP stapling.
    if (sslProperties.getEnableOcspStapling() != null)
        System.setProperty(JdkProperty.TLS_ENABLE_OCSP_STAPLING.getPropertyName(), sslProperties.getEnableOcspStapling().toString());

    // Create the https Tomcat.
    TomcatServletWebServerFactory tomcat = new TomcatServletWebServerFactory() {

        @Override
        protected void postProcessContext(
                Context context) {

            SecurityConstraint securityConstraint = new SecurityConstraint();
            securityConstraint.setUserConstraint("CONFIDENTIAL");
            SecurityCollection collection = new SecurityCollection();
            collection.addPattern("/*");
            securityConstraint.addCollection(collection);
            context.addConstraint(securityConstraint);

        }

    };

    // Customize the https connector.
    tomcat.addConnectorCustomizers(new TomcatConnectorCustomizer() {

        @Override
        public void customize(
                Connector connector) {

            SSLHostConfig sslHostConfig = connector.findSslHostConfigs()[0];
            sslHostConfig.setHonorCipherOrder(true);

        }

    });

    // Add the http connector with a redirection to the https port.
    Connector httpConnector = new Connector(TomcatServletWebServerFactory.DEFAULT_PROTOCOL);
    httpConnector.setScheme("http");
    httpConnector.setPort(serverProperties.getInternalHttpPort());
    httpConnector.setSecure(false);
    httpConnector.setRedirectPort(serverProperties.getExternalHttpsPort());
    tomcat.addAdditionalTomcatConnectors(httpConnector);

    return tomcat;

}
 
Example #29
Source File: EmbeddedTomcatCustomizer.java    From pulsar-manager with Apache License 2.0 4 votes vote down vote up
@Bean
public ServletWebServerFactory servletContainer() {
    log.info("Starting servletContainer");
    return new TomcatServletWebServerFactory() {
        @Override
        protected TomcatWebServer getTomcatWebServer(Tomcat tomcat) {
            try {
                log.info("Catalina base is " + tomcat.getServer().getCatalinaBase().getAbsolutePath());
                File lib = new File("lib").getAbsoluteFile();
                if (lib.isDirectory()) {
                    File bkvmWar = searchWar(lib, "bkvm", ".war");
                    if (bkvmWar != null) {
                        File configFile = new File("bkvm.conf");
                        log.info("looking for BKVM configuration file at " + configFile.getAbsolutePath());
                        if (configFile.isFile()) {
                            Properties props = new Properties();
                            try (FileReader reader = new FileReader(configFile)) {
                                props.load(reader);
                            }
                            boolean bkvmEnabled = Boolean.parseBoolean(props.getProperty("bkvm.enabled", "false"));
                            log.info("Read bkvm.enabled = {}", bkvmEnabled);
                            if (bkvmEnabled) {
                                System.setProperty("bookkeeper.visual.manager.config.path", configFile.getAbsolutePath());
                                File file = new File(tomcat.getServer().getCatalinaBase(), "/webapps");
                                log.info("Tomcat Webapps directory is " + file.getAbsolutePath());
                                file.mkdirs();
                                File bkvmDirectory = new File(file, "bkvm");
                                log.info("Deploying BKVM to " + bkvmDirectory.getAbsolutePath());
                                unZip(bkvmWar, bkvmDirectory);
                                Context context = tomcat.addWebapp("/bkvm", bkvmDirectory.getAbsolutePath());
                                WebappLoader loader = new WebappLoader(Thread.currentThread().getContextClassLoader());
                                context.setLoader(loader);
                            }
                        }
                    }
                }
                return super.getTomcatWebServer(tomcat);
            } catch (IOException | ServletException ex) {
                throw new RuntimeException(ex);
            }
        }

    };
}
 
Example #30
Source File: TomcatConfiguration.java    From api-layer with Eclipse Public License 2.0 4 votes vote down vote up
@Bean
public ServletWebServerFactory servletContainer() {
    TomcatServletWebServerFactory tomcat = new TomcatServletWebServerFactory();
    tomcat.setProtocol(TomcatServletWebServerFactory.DEFAULT_PROTOCOL);
    return tomcat;
}