Spring boot Combat (seventh) embedded container tomcat configuration _springboot

Source: Internet
Author: User
Spring boot Combat (seventh) nested container Tomcat configuration default container

The Spring boot Default Web program enables Tomcat embedded container tomcat, listens for 8080 ports, servletpath defaults to/through the need to use is the port, the context path modification, in spring boot its modification method and its simplicity;

Configuring in Resource files:   
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 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.18 4)

You can see that it listens on port 9090 and executes
Http://localhost:9090/lkl/springboot/liaokailin successfully access custom Tomcat

Simply configuring the Tomcat port in the actual project will not meet your needs, so you need to customize Tomcat configuration information to control Tomcat flexibly. To define 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 a.m. 12:11:47 Liaokailin EXP $
 */
@Configuration public
class Tomcatconfig {

    @Bean
    public Embeddedservletcontainerfactory Servletcontainer () {
        tomcatembeddedservletcontainerfactory tomcat = new Tomcatembeddedservletcontainerfactory ();
        Tomcat.seturiencoding ("UTF-8");
        return tomcat.
    }

}

Builds a embeddedservletcontainerfactory bean that can be set for Tomcat after obtaining the Tomcatembeddedservletcontainerfactory instance. For example, set the encoding to generate certificates for UTF-8 SSL configuration

Keytool-genkey-alias springboot-keyalg rsa-keystore/users/liaokailin/software/ca1/keystore
Set Password 123456

Verify that the certificate is correct in Tomcat

modifying Tomcat/conf/server.xml files

<connector
              protocol= "Org.apache.coyote.http11.Http11NioProtocol"
              port= "8443" maxthreads=
              " Scheme= "https" secure= "true" sslenabled= "true"
              keystorefile= "/users/liaokailin/software/ca1/keystore" keystorepass= "123456"
              clientauth= "false" sslprotocol= "TLS"/>

Start Tomcat, Access http://localhost:8443

Spring Boot inline Tomcat SSL

Configuring 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 Tomcat SSL configuration Server.ssl.keyAlias alias Server.ssl.keyPassword password Server.ssl.keyStore location

Start Spring Boot

Visit Https://localhost:8443/springboot/helloworld

Multi-Port listener configuration

Before the start of SSL can only go to HTTPS, can not access through HTTP, if you want to listen to multiple ports, you can use the encoding form.

1. Log off the front SSL configuration, 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 October 4, 2015 a.m. 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;
        The catch (Exception ex) {throw new illegalstateexception ("Cant access keystore: [" + "KeyStore" + "]", ex);

 }
    }
}

Add multiple listening connections through the Addadditionaltomcatconnectors method, at which point you can use HTTP 9090 ports and HTTPS 8443 ports.

Reprint Please specify
http://blog.csdn.net/liaokailin/article/details/48948093 welcome attention, your affirmation is the biggest support for me

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.