spring boot actual inline container tomcat configuration
- 2020-12-26 05:46:59
- OfStack
This article introduces the spring boot inline container configuration and shares it with you as follows:
The default container
spring boot default web program enables tomcat embedded container tomcat to listen on port 8080. servletPath default/pass requires port and context path modification. In spring boot, the modification method is very simple.
Configure in the resource file:
server.port=9090
server.contextPath=/lkl
Start the spring boot
2015-10-04 00:06:55.768 INFO 609 --- [ main] o.s.w.s.handler.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] o.s.j.e.a.AnnotationMBeanExporter : Registering beans for JMX exposure on startup
2015-10-04 00:06:55.928 INFO 609 --- [ main] s.b.c.e.t.TomcatEmbeddedServletContainer : 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)
As you can see its listening on port 9090, perform http: / / localhost: 9090 / lkl/springboot/liaokailin successful visit
Custom tomcat
A simple configuration of tomcat port in a real project would certainly not meet everyone's needs, so you need to customize tomcat configuration information to flexibly control tomcat.
Take defining the default encoding as an example
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 2015 years 10 month 4 day In the morning 12:11:47 liaokailin Exp $
*/
@Configuration
public class TomcatConfig {
@Bean
public EmbeddedServletContainerFactory servletContainer() {
TomcatEmbeddedServletContainerFactory tomcat = new TomcatEmbeddedServletContainerFactory();
tomcat.setUriEncoding("UTF-8");
return tomcat;
}
}
Build bean for EmbeddedServletContainerFactory. You can set tomcat after you get an instance of TomcatEmbeddedServletContainerFactory, such as setting the encoding to ES49en-8 here
SSL configuration
Generate a certificate
keytool -genkey -alias springboot -keyalg RSA -keystore /Users/liaokailin/software/ca1/keystore
Set the password 123456
Verify in tomcat that the certificate is correct
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 is embedded with tomcat ssl
Configure resource files
server.port=8443
server.ssl.enabled=true
server.ssl.keyAlias=springboot
server.ssl.keyPassword=123456
server.ssl.keyStore=/Users/liaokailin/software/ca1/keystore
server.ssl.enabled Start the tomcat ssl configuration
server. ssl. keyAlias alias
server. ssl. keyPassword password
server. ssl. keyStore position
Start the spring boot
Visit https: / / localhost: 8443 / springboot/helloworld
Multi-port listening configuration
After starting ssl, you can only go to https and cannot access through http. If you want to listen to multiple ports, you can use the encoding form.
1. Log off the previous ssl configuration and set 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 2015 years 10 month 4 day In the morning 12:11:47 liaokailin Exp $
*/
@Configuration
public 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 point, http 9090 and https 8443 are available.