Java Code Examples for org.springframework.boot.web.embedded.tomcat.TomcatServletWebServerFactory

The following examples show how to use org.springframework.boot.web.embedded.tomcat.TomcatServletWebServerFactory. These examples are extracted from open source projects. 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 Project: DrivingAgency   Source File: CorsConfig.java    License: MIT License 6 votes vote down vote up
@Bean
public TomcatServletWebServerFactory tomcatServletWebServerFactory(Connector connector){
    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(connector);
    return tomcat;
}
 
Example 2
Source Project: odo   Source File: HomeController.java    License: 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 3
@Bean
TomcatServletWebServerFactory tomcatServletWebServerFactory() {
    TomcatServletWebServerFactory factory = new TomcatServletWebServerFactory() {
        @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);
        }
    };
    factory.addAdditionalTomcatConnectors(createTomcatConnector());
    return factory;
}
 
Example 4
Source Project: Java-API-Test-Examples   Source File: TomcatHttpConfig.java    License: Apache License 2.0 6 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 5
Source Project: blog   Source File: EmbeddedTomcatConfig.java    License: BSD 2-Clause "Simplified" License 6 votes vote down vote up
@Override
public void customize(ConfigurableServletWebServerFactory factory) {
	((TomcatServletWebServerFactory) factory).addConnectorCustomizers(new TomcatConnectorCustomizer() {
		@Override
		public void customize(Connector connector) {
			Http11NioProtocol protocol = (Http11NioProtocol) connector.getProtocolHandler();
			protocol.setMaxConnections(200);
			protocol.setMaxThreads(200);
			protocol.setSelectorTimeout(3000);
			protocol.setSessionTimeout(3000);
			protocol.setConnectionTimeout(3000);

			connector.setPort(8888);
		}
	});
}
 
Example 6
Source Project: nbp   Source File: HttpsConfiguration.java    License: Apache License 2.0 6 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 7
Source Project: spring-boot-demo   Source File: HttpsConfig.java    License: MIT License 6 votes vote down vote up
@Bean
public TomcatServletWebServerFactory tomcatServletWebServerFactory(Connector connector) {
    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(connector);
    return tomcat;
}
 
Example 8
Source Project: layui-admin   Source File: LayuiAdminStartUp.java    License: 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 9
Source Project: joinfaces   Source File: WebFragmentRegistrationBean.java    License: Apache License 2.0 6 votes vote down vote up
@Override
public void customize(ConfigurableServletWebServerFactory factory) {

	if (factory instanceof TomcatServletWebServerFactory) {
		((TomcatServletWebServerFactory) factory).addContextCustomizers(new TomcatListenerAdder(this.listeners));
	}
	else if (factory instanceof JettyServletWebServerFactory) {
		((JettyServletWebServerFactory) factory).addConfigurations(new JettyListenerAdder(this.listeners));
	}
	else if (factory instanceof UndertowServletWebServerFactory) {
		((UndertowServletWebServerFactory) factory).addDeploymentInfoCustomizers(new UndertowListenerAdder(this.listeners));
	}
	else {
		log.warn("Unkown WebServerFactory implementation: {}", factory.getClass());
		factory.addInitializers(servletContext -> this.listeners.forEach(servletContext::addListener));
	}

	factory.addInitializers(servletContext -> this.contextParams.forEach(servletContext::setInitParameter));

	this.errorPages.forEach(factory::addErrorPages);
}
 
Example 10
Source Project: MaxKey   Source File: MaxKeySslConfig.java    License: Apache License 2.0 6 votes vote down vote up
@Bean
public TomcatServletWebServerFactory tomcatServletWebServerFactory(Connector connector) {
    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(connector);
    return tomcat;
}
 
Example 11
Source Project: micro-service   Source File: HttpsServerConfig.java    License: MIT License 6 votes vote down vote up
@Bean
    public ConfigurableServletWebServerFactory configurableServletWebServerFactory() {

        TomcatServletWebServerFactory factory = new TomcatServletWebServerFactory();
        factory.setSsl(getSsl());
        factory.setPort(8443);
        factory.addAdditionalTomcatConnectors(getHttpConnector());

        factory.addContextCustomizers(context -> {
            addSecurityConstraint(context);
        });

//        factory.addConnectorCustomizers(connector -> {
//            connector.setAllowTrace(true);
//        });

        return factory;
    }
 
Example 12
public static void configure(TomcatServletWebServerFactory tomcatFactory) {

        tomcatFactory.addContextCustomizers(context -> {

            StandardJarScanFilter standardJarScanFilter = new StandardJarScanFilter();
            standardJarScanFilter.setTldScan("jstl-*.jar,spring-security-taglibs-*.jar,spring-web-*.jar,spring-webmvc-*.jar,string-*.jar,taglibs-standard-impl-*.jar,tomcat-annotations-api-*.jar,tomcat-embed-jasper-*.jar");
            standardJarScanFilter.setTldSkip("*");
            context.getJarScanner().setJarScanFilter(standardJarScanFilter);

            boolean development = (System.getProperty("airsonic.development") != null);

            // Increase the size and time before eviction of the Tomcat
            // cache so that resources aren't uncompressed too often.
            // See https://github.com/jhipster/generator-jhipster/issues/3995

            StandardRoot resources = new StandardRoot();
            if (development) {
                resources.setCachingAllowed(false);
            } else {
                resources.setCacheMaxSize(100000);
                resources.setCacheObjectMaxSize(4000);
                resources.setCacheTtl(24 * 3600 * 1000);  // 1 day, in milliseconds
            }
            context.setResources(resources);

            // Put Jasper in production mode so that JSP aren't recompiled
            // on each request.
            // See http://stackoverflow.com/questions/29653326/spring-boot-application-slow-because-of-jsp-compilation
            Container jsp = context.findChild("jsp");
            if (jsp instanceof Wrapper) {
                ((Wrapper) jsp).addInitParameter("development", Boolean.toString(development));
            }
        });
    }
 
Example 13
Source Project: tensorboot   Source File: MvcConfiguration.java    License: Apache License 2.0 5 votes vote down vote up
@Bean
public ConfigurableTomcatWebServerFactory servletContainer() {
    return new TomcatServletWebServerFactory() {
        @Override
        protected void postProcessContext(Context context) {
            StandardRoot standardRoot = new StandardRoot(context);
            standardRoot.setCacheMaxSize(TOMCAT_RESOURCES_CACHE_SIZE);
            context.setResources(standardRoot);
            log.info(String.format("New cache size (KB): %d", context.getResources().getCacheMaxSize()));
        }
    };
}
 
Example 14
Source Project: ChengFeng1.5   Source File: ChengfengConfig.java    License: MIT License 5 votes vote down vote up
@Bean
public TomcatServletWebServerFactory tomcatEmbedded() {
    TomcatServletWebServerFactory tomcat = new TomcatServletWebServerFactory();
    tomcat.addConnectorCustomizers((TomcatConnectorCustomizer) connector -> {
        if ((connector.getProtocolHandler() instanceof AbstractHttp11Protocol<?>)) {
            //-1 means unlimited
            ((AbstractHttp11Protocol<?>) connector.getProtocolHandler()).setMaxSwallowSize(-1);
        }
    });
    return tomcat;
}
 
Example 15
Source Project: seppb   Source File: PortalTomcatWebServerCustomizer.java    License: MIT License 5 votes vote down vote up
@Override
public void customize(WebServerFactory container) {
    TomcatServletWebServerFactory containerFactory = (TomcatServletWebServerFactory) container;
    containerFactory.addConnectorCustomizers((TomcatConnectorCustomizer) connector -> {
        connector.setAttribute("relaxedQueryChars", "[]|{}^\" < > ");
        connector.setAttribute("relaxedPathChars", "[]|");
    });
}
 
Example 16
Source Project: WeBASE-Collect-Bee   Source File: WebasebeeApplication.java    License: 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 17
Source Project: springBoot   Source File: Http2Https.java    License: MIT License 5 votes vote down vote up
@Bean
public TomcatServletWebServerFactory servletContainerFactory() {
    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);
        }
    };
    Connector connector = new Connector("org.apache.coyote.http11.Http11NioProtocol");
    //设置将分配给通过此连接器接收到的请求的方案
    connector.setScheme("http");

    //true: http使用http, https使用https;
    //false: http重定向到https;
    connector.setSecure(false);

    //设置监听请求的端口号,这个端口不能其他已经在使用的端口重复,否则会报错
    connector.setPort(httpPort);

    //重定向端口号(非SSL到SSL)
    connector.setRedirectPort(sslPort);

    tomcat.addAdditionalTomcatConnectors(connector);
    return tomcat;
}
 
Example 18
Source Project: cymbal   Source File: TomcatConfiguration.java    License: Apache License 2.0 5 votes vote down vote up
@Bean
public ConfigurableServletWebServerFactory webServerFactory() {
    TomcatServletWebServerFactory factory = new TomcatServletWebServerFactory();
    factory.addConnectorCustomizers(
            (TomcatConnectorCustomizer) connector -> connector.setProperty("relaxedQueryChars", "|{}[]\\"));
    return factory;
}
 
Example 19
@Bean
public WebServerFactoryCustomizer accesslogWebServerFactoryCustomizer() {
    return factory -> {
        SimpleLogbackValve valve = new SimpleLogbackValve(LoggerFactory.getLogger("accesslog"));

        ((TomcatServletWebServerFactory) factory).addEngineValves(valve);
    };
}
 
Example 20
Source Project: flash-waimai   Source File: WebConfig.java    License: MIT License 5 votes vote down vote up
@Bean
public ConfigurableServletWebServerFactory webServerFactory() {
    TomcatServletWebServerFactory factory = new TomcatServletWebServerFactory();
    factory.addConnectorCustomizers(new TomcatConnectorCustomizer() {
        @Override
        public void customize(Connector connector) {
            connector.setProperty("relaxedQueryChars", "|{}[]");
        }
    });
    return factory;
}
 
Example 21
Source Project: singleton   Source File: TomcatConfig.java    License: 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 Project: singleton   Source File: TomcatConfig.java    License: 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 23
@Bean
public WebServerFactoryCustomizer tomcatServletWebServerFactory() {
    return server -> {
        if (server instanceof TomcatServletWebServerFactory) {
            ((TomcatServletWebServerFactory) server).addConnectorCustomizers(tomcatGracefulShutdown());
        }
    };
}
 
Example 24
@Bean
public ServletWebServerFactory servletContainer(TomcatSslConnectorProperties properties) {
    TomcatServletWebServerFactory tomcat = new TomcatServletWebServerFactory();
    tomcat.addAdditionalTomcatConnectors(createSslConnector(properties));
    tomcat.setSessionTimeout(Duration.ofMinutes(1));
    return tomcat;
}
 
Example 25
@Bean
public ServletWebServerFactory servletContainer(TomcatSslConnectorProperties properties) {
    TomcatServletWebServerFactory tomcat = new TomcatServletWebServerFactory();
    tomcat.addAdditionalTomcatConnectors(createSslConnector(properties));
    tomcat.setSessionTimeout(Duration.ofMinutes(1));
    return tomcat;
}
 
Example 26
@Bean
public ServletWebServerFactory servletContainer(TomcatSslConnectorProperties properties) {
    TomcatServletWebServerFactory tomcat = new TomcatServletWebServerFactory();
    tomcat.addAdditionalTomcatConnectors(createSslConnector(properties));
    tomcat.setSessionTimeout(Duration.ofMinutes(1));
    return tomcat;
}
 
Example 27
@Bean
public ServletWebServerFactory servletContainer(TomcatSslConnectorProperties properties) {
    TomcatServletWebServerFactory tomcat = new TomcatServletWebServerFactory();
    tomcat.addAdditionalTomcatConnectors(createSslConnector(properties));
    tomcat.setSessionTimeout(Duration.ofMinutes(1));
    return tomcat;
}
 
Example 28
@Bean
public ServletWebServerFactory servletContainer(TomcatSslConnectorProperties properties) {
    TomcatServletWebServerFactory tomcat = new TomcatServletWebServerFactory();
    tomcat.addAdditionalTomcatConnectors(createSslConnector(properties));
    tomcat.setSessionTimeout(Duration.ofMinutes(1));
    return tomcat;
}
 
Example 29
@Bean
public ServletWebServerFactory servletContainer(TomcatSslConnectorProperties properties) {
    TomcatServletWebServerFactory tomcat = new TomcatServletWebServerFactory();
    tomcat.addAdditionalTomcatConnectors(createSslConnector(properties));
    tomcat.setSessionTimeout(Duration.ofMinutes(1));
    return tomcat;
}
 
Example 30
@Bean
public WebServerFactoryCustomizer<TomcatServletWebServerFactory> webServerFactoryCustomizer() {
    return factory ->
            factory.addContextCustomizers(
                    context -> {
                        // 设置 JNDI 信息
                        ContextEnvironment environment = new ContextEnvironment();
                        environment.setName("jndi-name");
                        environment.setValue("My JNDI");
                        environment.setType(String.class.getName());
                        // 配置 Environment,等同于 <Environment/> 元素
                        context.getNamingResources().addEnvironment(environment);
                    }
            );
}