This article introduces the tomcat configuration of the embedded container in spring boot, and shares it with you, as follows:
Default container
The default web program of spring boot enables tomcat embedded container tomcat to listen to port 8080, and the default servletPath is / through the modification of port and context path. In spring boot, its modification method is very simple;
Configure in the resource file:
server.port=9090 server.contextPath=/lkl
Start spring boot
2015-10-04 00:06:55.768 INFO 609 --- [ main] oswshandler.SimpleUrlHandlerMapping : Mapped URL path [/**/favicon.ico] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]2015-10-04 00:06:55.844 INFO 609 --- [ main] osjeaAnnotationMBeanExporter : Registering beans for JMX exposure on startup2015-10-04 00:06:55.928 INFO 609 --- [ main] sbcetTomcatEmbeddedServletContainer : Tomcat started on port(s): 9090 (http)2015-10-04 00:06:55.930 INFO 609 --- [ main] com.lkl.springboot.Application : Started Application in 3.906 seconds (JVM running for 4.184)
It can be seen that its listening port 9090 is successfully accessed by executing http://localhost:9090/lkl/springboot/liaokailin.
Custom tomcat
In actual projects, simple configuration of tomcat ports will definitely not meet everyone's needs, so you need to customize tomcat configuration information to flexibly control tomcat.
Take the example of defining the default encoding
package com.lkl.springboot.container.tomcat;import org.springframework.boot.context.embedded.EmbeddedServletContainerFactory;import org.springframework.boot.context.embedded.tomcat.TomcatEmbeddedServletContainerFactory;import org.springframework.context.annotation.Bean;import org.springframework.context.annotation.Configuration;/** * tomcat Configuration* @author liaokailin * @version $Id: TomcatConfig.java, v 0.1 October 4, 2015 12:11:47 AM liaokailin Exp $ */@Configurationpublic class TomcatConfig { @Bean public EmbeddedServletContainerFactory servletContainer() { TomcatEmbeddedServletContainerFactory tomcat = new TomcatEmbeddedServletContainerFactory(); tomcat.setUriEncoding("UTF-8"); return tomcat; }}Build the bean of EmbeddedServletContainerFactory, and after obtaining the TomcatEmbeddedServletContainerFactory instance, you can set tomcat, for example, the encoding is set to UTF-8 here.
SSL configuration
Generate a certificate
keytool -genkey -alias springboot -keyalg RSA -keystore /Users/liaokailin/software/ca1/keystore Set password 123456
Verify that the certificate is correct in tomcat
Modify tomcat/conf/server.xml file
<Connector protocol="org.apache.coyote.http11.Http11NioProtocol" port="8443" maxThreads="200" scheme="https" secure="true" SSLEnabled="true" keystoreFile="/Users/liaokailin/software/ca1/keystore" keystorePass="123456" clientAuth="false" sslProtocol="TLS"/>
Start tomcat and visit http://localhost:8443
spring boot embedded tomcat ssl
Configure resource files
server.port=8443server.ssl.enabled=trueserver.ssl.keyAlias=springbootserver.ssl.keyPassword=123456server.ssl.keyStore=/Users/liaokailin/software/ca1/keystore
Start spring boot
Visit https://localhost:8443/springboot/helloworld
Multi-port monitoring configuration
After starting SSL, you can only go to https and cannot access it through http. If you want to listen to multiple ports, you can use encoding to implement it.
1. Log out the previous ssl configuration and set the configuration server.port=9090
2. Modify TomcatConfig.java
package com.lkl.springboot.container.tomcat;import java.io.File;import org.apache.catalina.connector.Connector;import org.apache.coyote.http11.Http11NioProtocol;import org.springframework.boot.context.embedded.EmbeddedServletContainerFactory;import org.springframework.boot.context.embedded.tomcat.TomcatEmbeddedServletContainerFactory;import org.springframework.context.annotation.Bean;import org.springframework.context.annotation.Configuration;/** * tomcat Configuration* @author liaokailin * @version $Id: TomcatConfig.java, v 0.1 October 4, 2015 liaokailin Exp $ */@Configurationpublic class TomcatConfig { @Bean public EmbeddedServletContainerFactory servletContainer() { TomcatEmbeddedServletContainerFactory tomcat = new TomcatEmbeddedServletContainerFactory(); tomcat.setUriEncoding("UTF-8"); tomcat.addAdditionalTomcatConnectors(createSslConnector()); return tomcat; } private Connector createSslConnector() { Connector connector = new Connector("org.apache.coyote.http11.Http11NioProtocol"); Http11NioProtocol protocol = (Http11NioProtocol) connector.getProtocolHandler(); try { File truststore = new File("/Users/liaokailin/software/ca1/keystore"); connector.setScheme("https"); protocol.setSSLEnabled(true); connector.setSecure(true); connector.setPort(8443); protocol.setKeystoreFile(truststore.getAbsolutePath()); protocol.setKeystorePass("123456"); protocol.setKeyAlias("springboot"); return connector; } catch (Exception ex) { throw new IllegalStateException("cant access keystore: [" + "keystore" + "] ", ex); } }} Add multiple listening connections through the addAdditionalTomcatConnectors method; at this time, you can use the http 9090 port and the https 8443 port.
The above is all the content of this article. I hope it will be helpful to everyone's learning and I hope everyone will support Wulin.com more.