The configuration method in this section is common for inline servlet containers such as tomcat, jetty, and UNDERTOW.
1.Properties Configuring Tomcat
All of the properties of Tomcat are defined in the Org.springframework.boot.autoconfigure.web.ServerProperties configuration class, and we only need to configure the properties in the Application.properties Configuration. The common servlet container configuration is prefixed with "server", and the tomcat-specific configuration is prefixed with "server.tomcat". For example:
server.port= #配置程序端口, default is 8080server.session-timeout= #用户会话session过期时间, in seconds server.context-path= #配置访问路径, default Is/server.tomcat.uri-encoding= #配置tomcat编码, default = UTF-8server.tomcat.compression= # Whether Tomcat turns on compression, which is off by default
2. Configuring Tomcat through code
If you need to configure the servlet container in code, you can register a bean that implements the Embeddedservletcontainercustomizer interface, and if you want to configure tomcat, Jetty, undertow directly, You can directly define tomcatembeddedservletcontainerfactory, jettyembeddedservletcontainerfactory, Undertowembeddedservletcontainerfactory.
1>. Common Configuration
How to configure a new class
packagecom.wisely.ch7_4;Importjava.util.concurrent.TimeUnit;Importorg.springframework.boot.context.embedded.ConfigurableEmbeddedServletContainer;Importorg.springframework.boot.context.embedded.EmbeddedServletContainerCustomizer;Importorg.springframework.boot.context.embedded.ErrorPage;Importorg.springframework.http.HttpStatus;Importorg.springframework.stereotype.Component; @Component public classCustomservletcontainerImplementsEmbeddedservletcontainercustomizer{@Override public voidCustomize (configurableembeddedservletcontainer container) {container.setport (8888); Container.adderrorpages (NewErrorPage (httpstatus.not_found, "/404.html")); Container.setsessiontimeout (10, timeunit.minutes);}}
How to add a Class's bean within the current existing configuration file
packagecom.wisely.ch7_4;Importorg.springframework.boot.SpringApplication;Importorg.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication public classch74application { public Static voidmain (string[] Args) {springapplication.run (ch74application.class, args); } @Component public Static classCustomservletcontainerImplementsembeddedservletcontainercustomizer{//Note In spring configuration that the current class is to be declared static@Override public voidCustomize (configurableembeddedservletcontainer container) {container.setport (8888); Container.adderrorpages (NewErrorPage (httpstatus.not_found, "/404.html")); Container.setsessiontimeout (10, timeunit.minutes); } } }
2> Specific configuration
@Bean public embeddedservletcontainerfactory servletcontainer () { tomcatembeddedservletcontainerfactory Factory= New tomcatembeddedservletcontainerfactory (); Factory.setport (8888); Factory.adderrorpages (new errorpage (httpstatus.not_found, "/404.html")); // 404.html can be placed under Src/main/resources/static. factory.setsessiontimeout (ten, timeunit.minutes); return factory;}
3. Replace Tomcat
Spring Boot uses Tomcat as the embedded servlet container by default, and if you want to use jetty or Undertow as a servlet container, you only need to modify the Spring-boot-starter-web Dependency.
1> Replace with jetty
Replace Spring-boot-starter-web's dependency in Pom.xml with Spring-boot-starter-tomcat with Spring-boot-starter-jetty
<Dependency> <groupId>Org.springframework.boot</groupId> <Artifactid>Spring-boot-starter-web</Artifactid> <Exclusions> <exclusion> <groupId>Org.springframework.boot</groupId> <Artifactid>Spring-boot-starter-tomcat</Artifactid> </exclusion> </Exclusions> </Dependency> <Dependency> <groupId>Org.springframework.boot</groupId> <Artifactid>Spring-boot-starter-jetty</Artifactid> </Dependency>
2> Replace with Undertow
Replace Spring-boot-starter-web's dependency in Pom.xml with Spring-boot-starter-tomcat with Spring-boot-starter-undertow
<Dependency> <groupId>Org.springframework.boot</groupId> <Artifactid>Spring-boot-starter-web</Artifactid> <Exclusions> <exclusion> <groupId>Org.springframework.boot</groupId> <Artifactid>Spring-boot-starter-tomcat</Artifactid> </exclusion> </Exclusions> </Dependency> <Dependency> <groupId>Org.springframework.boot</groupId> <Artifactid>Spring-boot-starter-undertow</Artifactid> </Dependency>
4.SSL Configuration
SSL (secure Sockets layer, secured sockets Layer) is a security protocol that provides security and data integrity for network communication, and SSL encrypts the network connection at the network transport layer, providing the support of data encapsulation, compression, encryption and other functions for the high-level Protocol. It is used to authenticate the two sides of the communication before the actual data transmission begins, negotiate the encryption algorithm, and exchange the encryption Key.
In the Web application based on b/s, SSL is implemented via HTTPS.
1> Generate a certificate
Using SSL requires a certificate that can be either self-signed or obtained from the SSL Certificate Authority center. This example demonstrates the generation of a self-signed certificate.
Each JDK or JRE has a tool called keytool, which is a certificate management tool that can be used to generate self-signed certificates.
After configuring the Java_home and adding the Java_home bin directory to path, you can invoke the command on the Console:
Keytool-genkey-alias Tomcat
?
At this point we generate A. keystore hidden file in the current directory, which is the certificate file that we want to use.
2>. Spring Boot Configuration SSL
Add a index.html to src/main/resources/static, as a Test.
Copy the. KeyStore file to the root of your project, and then configure the following SSL in Application.properties:
server.port=8443server.ssl.key-store=. Keystoreserver.ssl.key-store-password=123456 Server.ssl.keyStoreType= JKSserver.ssl.keyAlias:tomcat
Start Spring boot and the console output looks like this:
?
Visit https://localhost:8443
3>.http turn to HTTPS
Many times, we visit http://www.baidu.com, but will automatically turn to Https://www.baidu.com. To achieve this, we need to configure Tomcatembeddedservletcontainerfactory and add Tomcat's Connector to implement it.
At this point we need to add the following configuration in the configuration file:
packagecom.wisely.ch7_4;Importorg.springframework.boot.SpringApplication;Importorg.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication public classch74application { public Static voidmain (string[] Args) {springapplication.run (ch74application.class, args); } @Bean publicembeddedservletcontainerfactory servletcontainer () {tomcatembeddedservletcontainerfactory Tomcat=Newtomcatembeddedservletcontainerfactory () {@Overrideprotected voidpostprocesscontext (context Context) {securityconstraint securityconstraint=NewSecurityconstraint (); Securityconstraint.setuserconstraint ("confidential"); Securitycollection Collection=Newsecuritycollection (); Collection.addpattern ("/*"); Securityconstraint.addcollection (collection); Context.addconstraint (securityconstraint); } }; Tomcat.addadditionaltomcatconnectors (httpconnector ()); returntomcat; } @Bean publicConnector httpconnector () {Connector Connector=NewConnector ("org.apache.coyote.http11.Http11NioProtocol"); Connector.setscheme ("http"); Connector.setport (8080); Connector.setsecure (false); Connector.setredirectport (8443); returnconnector; }}
Spring Boot Tutorial 30--tomcat configuration