@Bean public EmbeddedServletContainerFactory servletContainer() { TomcatEmbeddedServletContainerFactory tomcat = new TomcatEmbeddedServletContainerFactory(); tomcat.addConnectorCustomizers((TomcatConnectorCustomizer) connector -> { connector.setScheme("https"); connector.setPort(cfg.getPort()); Http11NioProtocol protocol = (Http11NioProtocol) connector.getProtocolHandler(); protocol.setSSLEnabled(true); protocol.setSSLCertificateKeyFile(cfg.getKey()); protocol.setSSLCertificateFile(cfg.getCert()); cfg.getChain().ifPresent(protocol::setSSLCertificateChainFile); }); return tomcat; }
@Bean public EmbeddedServletContainerFactory servletContainer() { TomcatEmbeddedServletContainerFactory tomcat = new TomcatEmbeddedServletContainerFactory() { @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(initiateHttpConnector()); return tomcat; }
@Bean public EmbeddedServletContainerFactory servletContainer() { final TomcatEmbeddedServletContainerFactory tomcat = new TomcatEmbeddedServletContainerFactory(); if (isEnabled()) { final Connector ajpConnector = new Connector(getProtocol()); ajpConnector.setPort(getPort()); ajpConnector.setSecure(isSecure()); ajpConnector.setAllowTrace(isAllowTrace()); ajpConnector.setScheme(getScheme()); tomcat.addAdditionalTomcatConnectors(ajpConnector); } return tomcat; }
@Bean public EmbeddedServletContainerFactory servletContainer(SslProperties properties) { TomcatEmbeddedServletContainerFactory tomcat = new TomcatEmbeddedServletContainerFactory() { @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(createSslConnector(properties)); return tomcat; }
@Bean public EmbeddedServletContainerFactory jettyConfigBean() { DynamicPropertyFactory propertyFactory = DynamicPropertyFactory.getInstance(); String accessLogFilePath = propertyFactory.getStringProperty("server.accessLog.config.file", ACCESS_LOG_FILE_PATH).getValue(); JettyEmbeddedServletContainerFactory jettyEmbeddedServletContainerFactory = new JettyEmbeddedServletContainerFactory(); jettyEmbeddedServletContainerFactory.addServerCustomizers(new org.springframework.boot.context.embedded.jetty.JettyServerCustomizer() { public void customize(Server server) { HandlerCollection handlers = new HandlerCollection(); for (Handler handler : server.getHandlers()) { handlers.addHandler(handler); } RequestLogHandler requestLogHandler = new RequestLogHandler(); RequestLogImpl requestLogImpl = new RequestLogImpl(); requestLogImpl.setFileName(accessLogFilePath); requestLogHandler.setRequestLog(requestLogImpl); handlers.addHandler(requestLogHandler); server.setHandler(handlers); LOGGER.info("Jetty Server Customized. Access Log Configuration File - {}", accessLogFilePath); } }); return jettyEmbeddedServletContainerFactory; }
@Bean public EmbeddedServletContainerFactory servletContainer() { final TomcatEmbeddedServletContainerFactory tomcatFactory = new TomcatEmbeddedServletContainerFactory(); // tomcatFactory.setAddress(InetAddress.getLocalHost());// you can restrict localhost access tomcatFactory.setPort(8080); // ServletContainerInitializer final Connector connector = new Connector(); connector.setPort(8443); connector.setSecure(true); connector.setScheme("https"); connector.setProperty("SSLEnabled", "true"); connector.setProperty("keystorePass", "spring"); try { final ClassPathResource classPathResource = new ClassPathResource("keystore"); System.err.println(classPathResource.getFile().getAbsolutePath()); connector.setProperty("keystoreFile", classPathResource.getFile().getAbsolutePath()); } catch (final Exception e) { System.err.println("Error while loading classpath resource " + e.getMessage()); } tomcatFactory.addAdditionalTomcatConnectors(connector); return tomcatFactory; }
/** * <p>servletContainer.</p> * * @return a {@link org.springframework.boot.context.embedded.EmbeddedServletContainerFactory} object. */ @Bean public EmbeddedServletContainerFactory servletContainer() { TomcatEmbeddedServletContainerFactory tomcat = new TomcatEmbeddedServletContainerFactory() { @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(initiateHttpConnector()); return tomcat; }
@Test public void onDifferentPortWithSpecificContainer() throws Exception { this.applicationContext.register(SpecificContainerConfig.class, RootConfig.class, DifferentPortConfig.class, EndpointConfig.class, BaseConfiguration.class, EndpointWebMvcAutoConfiguration.class, ErrorMvcAutoConfiguration.class); this.applicationContext.refresh(); assertContent("/controller", ports.get().server, "controlleroutput"); assertContent("/endpoint", ports.get().server, null); assertContent("/controller", ports.get().management, null); assertContent("/endpoint", ports.get().management, "endpointoutput"); assertContent("/error", ports.get().management, startsWith("{")); ApplicationContext managementContext = this.applicationContext .getBean(ManagementContextResolver.class).getApplicationContext(); List<?> interceptors = (List<?>) ReflectionTestUtils.getField( managementContext.getBean(EndpointHandlerMapping.class), "interceptors"); assertThat(interceptors).hasSize(1); EmbeddedServletContainerFactory parentContainerFactory = this.applicationContext .getBean(EmbeddedServletContainerFactory.class); EmbeddedServletContainerFactory managementContainerFactory = managementContext .getBean(EmbeddedServletContainerFactory.class); assertThat(parentContainerFactory) .isInstanceOf(SpecificEmbeddedServletContainerFactory.class); assertThat(managementContainerFactory) .isInstanceOf(SpecificEmbeddedServletContainerFactory.class); assertThat(managementContainerFactory).isNotSameAs(parentContainerFactory); }
/** * TODO: support Jetty as well */ @Bean public EmbeddedServletContainerFactory embeddedServletContainerFactory(ApplicationContext ctx) { String portProp = System.getProperty("mdw.server.port"); if (portProp == null) portProp = System.getProperty("server.port"); if (portProp == null) portProp = "8080"; String contextProp = System.getProperty("mdw.server.contextPath"); if (contextProp == null) contextProp = System.getProperty("server.contextPath"); if (contextProp == null) contextProp = "/mdw"; TomcatEmbeddedServletContainerFactory factory = new TomcatEmbeddedServletContainerFactory( contextProp, Integer.parseInt(portProp)); factory.addContextCustomizers(tomcatContextCustomizer()); factory.setDocumentRoot(new File(getBootDir() + "/web")); return factory; }
private void registerEmbeddedServletContainerFactory( AnnotationConfigEmbeddedWebApplicationContext childContext) { try { EmbeddedServletContainerFactory servletContainerFactory = this.applicationContext .getBean(EmbeddedServletContainerFactory.class); ConfigurableListableBeanFactory beanFactory = childContext.getBeanFactory(); if (beanFactory instanceof BeanDefinitionRegistry) { BeanDefinitionRegistry registry = (BeanDefinitionRegistry) beanFactory; registry.registerBeanDefinition("embeddedServletContainerFactory", new RootBeanDefinition(servletContainerFactory.getClass())); } } catch (NoSuchBeanDefinitionException ex) { // Ignore and assume auto-configuration } }
@Bean public EmbeddedServletContainerFactory servletContainer() { if (https) { TomcatEmbeddedServletContainerFactory tomcat = new TomcatEmbeddedServletContainerFactory() { @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(initiateHttpConnector()); return tomcat; } return new TomcatEmbeddedServletContainerFactory(); }
@Bean public EmbeddedServletContainerFactory servletContainer() { Integer ajpPortInt = Integer.parseInt(ajpPort); Boolean ajpEnabledBool = Boolean.valueOf(ajpEnabled); TomcatEmbeddedServletContainerFactory tomcat = new TomcatEmbeddedServletContainerFactory(); if (ajpEnabledBool) { Connector ajpConnector = new Connector(ajpProtocol); ajpConnector.setProtocol(ajpProtocol); ajpConnector.setPort(ajpPortInt); ajpConnector.setSecure(false); ajpConnector.setAllowTrace(false); ajpConnector.setScheme(ajpScheme); tomcat.addAdditionalTomcatConnectors(ajpConnector); } return tomcat; }
@Bean public EmbeddedServletContainerFactory servletContainer() { final TomcatEmbeddedServletContainerFactory tomcat = new TomcatEmbeddedServletContainerFactory(); if(useReverseProxy) { tomcat.addAdditionalTomcatConnectors(createAjpConnector()); } // Set the web resources cache size (this defaults to 10MB but that is too small for Find) tomcat.addContextCustomizers(context -> { final WebResourceRoot resources = new StandardRoot(context); resources.setCacheMaxSize(webResourcesCacheSize); context.setResources(resources); }); tomcat.addConnectorCustomizers(connector -> { connector.setMaxPostSize(connectorMaxPostSize); }); return tomcat; }
@Bean @Profile({ "staging", "production", "localpg" }) public EmbeddedServletContainerFactory servletContainer() { return new TomcatEmbeddedServletContainerFactory(){ @Override protected void postProcessContext(Context context) { if (environment.acceptsProfiles("~staging", "~production")) { SecurityConstraint securityConstraint = new SecurityConstraint(); securityConstraint.setUserConstraint("CONFIDENTIAL"); SecurityCollection collection = new SecurityCollection(); collection.addPattern("/*"); securityConstraint.addCollection(collection); context.addConstraint(securityConstraint); } } }; }
@Bean public EmbeddedServletContainerFactory servletContainer() throws Exception { TomcatEmbeddedServletContainerFactory factory = new TomcatEmbeddedServletContainerFactory(); int apiPort = Utils.getSystemPort(Constants.SYS_API_PORT); factory.setPort(apiPort); factory.setSessionTimeout(10, TimeUnit.MINUTES); 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; }
@Bean public EmbeddedServletContainerFactory servletContainer() { TomcatEmbeddedServletContainerFactory factory = new TomcatEmbeddedServletContainerFactory(); MockService config = MockService.getInstance(); factory.setPort(config.getPort()); factory.setSessionTimeout(10, TimeUnit.MINUTES); factory.addContextCustomizers(new TomcatContextCustomizer() { @Override public void customize(Context context) { JarScanner jarScanner = new JarScanner() { @Override public void scan(ServletContext arg0, ClassLoader arg1, JarScannerCallback arg2, Set<String> arg3) { } }; context.setJarScanner(jarScanner); } }); return factory; }
public EmbeddedServletContainerFactory servletContainer() { TomcatEmbeddedServletContainerFactory tomcat = new TomcatEmbeddedServletContainerFactory() { @Override protected void postProcessContext(Context context) { SecurityConstraint constraint = new SecurityConstraint(); constraint.setUserConstraint("CONFIDENTIAL"); SecurityCollection collection = new SecurityCollection(); collection.addPattern("/*"); constraint.addCollection(collection); context.addConstraint(constraint); } }; tomcat.addAdditionalTomcatConnectors(httpConnector()); return tomcat; }
/** * 定制方法二:注入EmbeddedServletContainerFactory * @return */ @Bean public EmbeddedServletContainerFactory servletContainer() { TomcatEmbeddedServletContainerFactory factory = new TomcatEmbeddedServletContainerFactory(); factory.setPort(8083); factory.setSessionTimeout(10, TimeUnit.MINUTES); //优先级高于配置在/static/error文件夹里面的404.html页面 factory.addErrorPages(new ErrorPage(HttpStatus.NOT_FOUND, "/error/403.html")); return factory; }
@ConditionalOnClass(value = {Tomcat.class, Http2Protocol.class}) @Bean public EmbeddedServletContainerFactory servletContainer() { final TomcatEmbeddedServletContainerFactory tomcat = new TomcatEmbeddedServletContainerFactory(); configureAjp(tomcat); configureHttp(tomcat); configureHttpProxy(tomcat); configureExtendedAccessLogValve(tomcat); configureRewriteValve(tomcat); configureSSLValve(tomcat); return tomcat; }
@Bean public EmbeddedServletContainerFactory servletContainer() { TomcatEmbeddedServletContainerFactory tomcat = new TomcatEmbeddedServletContainerFactory(); tomcat.addConnectorCustomizers((TomcatConnectorCustomizer) connector -> { connector.setScheme("http"); connector.setPort(cfg.getPort()); }); return tomcat; }
/** * Servlet container embedded servlet container factory. * * @return the embedded servlet container factory * @source http://stackoverflow.com/questions/39146476/how-does-spring-boot-control-tomcat-cache */ @Bean public EmbeddedServletContainerFactory servletContainer() { return new TomcatEmbeddedServletContainerFactory() { @Override protected void postProcessContext(Context context) { final int cacheSize = 256 * 1024; final StandardRoot standardRoot = new StandardRoot(context); standardRoot.setCacheMaxSize(cacheSize); context.setResources(standardRoot); logger.info(String.format("New cache size (KB): %d", context.getResources().getCacheMaxSize())); } }; }
@Bean public EmbeddedServletContainerFactory createEmbeddedServletContainerFactory() { TomcatEmbeddedServletContainerFactory tomcatFactory = new TomcatEmbeddedServletContainerFactory(); tomcatFactory.setUriEncoding(StandardCharsets.UTF_8); tomcatFactory.setSessionTimeout(3600, TimeUnit.SECONDS); tomcatFactory.addConnectorCustomizers(new MyTomcatConnectorCustomizer()); return tomcatFactory; }
/** * @return Jetty config bean */ @Bean public EmbeddedServletContainerFactory jettyConfigBean() { JettyEmbeddedServletContainerFactory jef = new JettyEmbeddedServletContainerFactory(); jef.setPort(getServerPort()); logger.info("Listening on port {}...", jef.getPort()); return jef; }
@Bean public EmbeddedServletContainerFactory servletContainer() { TomcatEmbeddedServletContainerFactory tomcat = new TomcatEmbeddedServletContainerFactory(); Connector[] additionalConnectors = this.additionalConnector(); if (additionalConnectors != null && additionalConnectors.length > 0) { tomcat.addAdditionalTomcatConnectors(additionalConnectors); } return tomcat; }
/** * @return servletContainer bean reconfigured using SSL */ @Bean public EmbeddedServletContainerFactory servletContainer() { final TomcatEmbeddedServletContainerFactory tomcat = new TomcatEmbeddedServletContainerFactory(); tomcat.addAdditionalTomcatConnectors(createHttpConnector()); return tomcat; }
@Bean public EmbeddedServletContainerFactory servletContainer() { TomcatEmbeddedServletContainerFactory tomcat = new TomcatEmbeddedServletContainerFactory(); if (tomcatAjpEnabled) { Connector ajpConnector = new Connector("AJP/1.3"); ajpConnector.setPort(ajpPort); ajpConnector.setSecure(false); ajpConnector.setAllowTrace(false); ajpConnector.setScheme("http"); tomcat.addAdditionalTomcatConnectors(ajpConnector); } return tomcat; }
@Bean @ConditionalOnClass({EmbeddedServletContainerFactory.class }) public EmbeddedServletContainerCustomizer tomcatContainerCustomizer() { return container -> { TomcatEmbeddedServletContainerFactory tomcat = (TomcatEmbeddedServletContainerFactory) container; tomcat.setTldSkip("*.jar"); }; }
@Bean public EmbeddedServletContainerFactory servletContainer() { TomcatEmbeddedServletContainerFactory factory = new TomcatEmbeddedServletContainerFactory(); factory.setPort(80); factory.setContextPath("/jersey"); factory.setSessionTimeout(60, TimeUnit.MINUTES); return factory; }
@Bean public EmbeddedServletContainerFactory servletContainer() { TomcatEmbeddedServletContainerFactory factory = new TomcatEmbeddedServletContainerFactory(); factory.setPort(80); factory.setContextPath("/liquid"); factory.setSessionTimeout(60, TimeUnit.MINUTES); return factory; }
@Bean public EmbeddedServletContainerFactory embeddedServletContainerFactory() { UndertowEmbeddedServletContainerFactory factory = new UndertowEmbeddedServletContainerFactory(); factory.addBuilderCustomizers(builder -> { builder.setServerOption(UndertowOptions.ENABLE_HTTP2, Boolean.TRUE); builder.setServerOption(UndertowOptions.ENABLE_SPDY, Boolean.TRUE); builder.setServerOption(UndertowOptions.ENABLE_STATISTICS, Boolean.TRUE); builder.setServerOption(UndertowOptions.RECORD_REQUEST_START_TIME, Boolean.TRUE); builder.setSocketOption(Options.BACKLOG, 100000); builder.addHttpListener(11081, "localhost"); }); return factory; }
private Class<?> determineEmbeddedServletContainerFactoryClass() throws NoSuchBeanDefinitionException { Class<?> servletContainerFactoryClass = this.applicationContext .getBean(EmbeddedServletContainerFactory.class).getClass(); if (cannotBeInstantiated(servletContainerFactoryClass)) { throw new FatalBeanException("EmbeddedServletContainerFactory implementation " + servletContainerFactoryClass.getName() + " cannot be instantiated. " + "To allow a separate management port to be used, a top-level class " + "or static inner class should be used instead"); } return servletContainerFactoryClass; }
/** * adds SSL connector to Tomcat * * @return */ @Bean public EmbeddedServletContainerFactory servletContainer() { TomcatEmbeddedServletContainerFactory tomcat = new TomcatEmbeddedServletContainerFactory(); if (StringUtils.hasText(keystoreFile)) { tomcat.addAdditionalTomcatConnectors(createSslConnector()); } return tomcat; }
@Bean public EmbeddedServletContainerFactory servletContainer() { UndertowEmbeddedServletContainerFactory factory = new UndertowEmbeddedServletContainerFactory(); factory.addDeploymentInfoCustomizers(new UndertowDeploymentInfoCustomizer() { @Override public void customize(DeploymentInfo deploymentInfo) { deploymentInfo.addInitialHandlerChainWrapper(new RootHandlerWrapper()); } }); factory.setPort(Integer.parseInt(environment.getProperty("shiny.proxy.port", "8080"))); return factory; }