Spring boot embedded container tomcat configuration, springtomcat

Source: Internet
Author: User

Spring boot embedded container tomcat configuration, springtomcat

This article introduces tomcat configurations of embedded containers in spring boot practice. The details are as follows:

Default container

Spring boot uses the default web program to enable tomcat embedded container tomcat and listen to port 8080. The default value of servletPath is/. You need to modify the port and context path. The modification method is simple in spring boot;

Configure in the resource file:

server.port=9090 server.contextPath=/lkl

Start 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 startup2015-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)

It can be seen that its listening port is 9090, and http: // localhost: 9090/lkl/springboot/liaokailin is successfully accessed.

Custom tomcat

In actual projects, simply configuring tomcat ports cannot meet your needs. Therefore, 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 October 4, 2015 12:11:47 liaokailin Exp $ */@ Configurationpublic class TomcatConfig {@ Bean public EmbeddedServletContainerFactory servletContainer () {TomcatEmbeddedServletContainerFactory tomcat = new Login (); tomcat. setUriEncoding ("UTF-8"); return tomcat ;}}

Construct the bean of EmbeddedServletContainerFactory, get the TomcatEmbeddedServletContainerFactory instance, you can set tomcat, for example, here set the encoding to UTF-8

SSL Configuration

Generate Certificate

Keytool-genkey-alias springboot-keyalg RSA-keystore/Users/liaokailin/software/A1/keystore set password 123456

Verify that the certificate is correct in tomcat

Modify the 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 access http: // localhost: 8443

Embedded tomcat ssl in spring boot

Configure resource files

server.port=8443server.ssl.enabled=trueserver.ssl.keyAlias=springbootserver.ssl.keyPassword=123456server.ssl.keyStore=/Users/liaokailin/software/ca1/keystore
  1. Server. ssl. enabled start tomcat ssl Configuration
  2. Server. ssl. keyAlias alias
  3. Server. ssl. keyPassword
  4. Server. ssl. keyStore location

Start spring boot

Access https: // localhost: 8443/springboot/helloworld

Multi-port Listener Configuration

After starting ssl, you can only use https and cannot access it through http. To listen to multiple ports, you can use the encoding method.

1. log out of the preceding ssl configuration and set 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 12:11:47 liaokailin Exp $ */@ Configurationpublic class TomcatConfig {@ Bean public EmbeddedServletContainerFactory servletContainer () {TomcatEmbeddedServletContainerFactory tomcat = new Login (); tomcat. setUriEncoding ("UTF-8"); tomcat. addAdditionalTomcatConnectors (createSslConnector (); return tomcat;} private Connector createSslConnector () {Connector conne= new connector ("org. apache. coyote. http11.Http11NioProtocol "); Http11NioProtocol = (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 );}}}

You can use the addAdditionalTomcatConnectors method to add multiple listener connections. In this case, you can use http 9090 and https 8443.

The above is all the content of this article. I hope it will be helpful for your learning and support for helping customers.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.