• SpringBoot入门之内嵌Tomcat配置


      spring boot默认web程序启用tomcat内嵌容器tomcat,监听8080端口,servletPath默认为 / 。需要用到的就是端口、上下文路径的修改,在spring boot中其修改方法极其简单,实例如下:

    server.port=8088
    server.context-path=/test

    启动程序,日志如下:

    2018-03-08 19:57:58.824  INFO 12046 --- [           main] s.b.c.e.t.TomcatEmbeddedServletContainer : Tomcat started on port(s): 8088 (http)

    可以看出其监听端口8088

    自定义tomcat  

      在实际的项目中简单的配置tomcat端口肯定无法满足大家的需求,因此需要自定义tomcat配置信息来灵活的控制tomcat。下面的实例定义了tomcat的默认编码格式

    package com.ysl.conf;
    
    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;
    
    import java.nio.charset.Charset;
    
    /**
     * tomcat配置
     */
    @Configuration
    public class TomcatConfig {
    
        @Bean
        public EmbeddedServletContainerFactory servletContainer(){
            TomcatEmbeddedServletContainerFactory factory = new TomcatEmbeddedServletContainerFactory();
            factory.setUriEncoding(Charset.forName("UTF-8"));
            return factory;
        }
    }

      构建EmbeddedServletContainerFactory的bean,获取到TomcatEmbeddedServletContainerFactory实例以后可以对tomcat进行设置,例如这里设置编码为UTF-8

    SSL配置

    生成证书

    keytool -genkey -alias springboot -keyalg RSA -keystore /Users/ysl/software/ca1/keystore
    设置密码123456

    修改tomcat的server.xml,验证证书是否正确

    <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"/>

    启动tomcat,访问https://localhost:8443

    spring boot 内嵌tomcat ssl

    配置资源文件:

    server.port=8443
    server.ssl.enabled=true
    server.ssl.keyAlias=springboot
    server.ssl.keyPassword=123456
    server.ssl.keyStore=/Users/ysl/software/ca1/keystore

    多端口监听配置

    前面启动ssl后只能走https,不能通过http进行访问,如果要监听多端口,可采用编码形式实现。

    1.注销前面ssl配置,设置配置 server.port=8088

    2.修改TomcatConfig.java

    package com.ysl.conf;
    
    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;
    
    import java.io.File;
    import java.nio.charset.Charset;
    
    /**
     * tomcat配置
     */
    @Configuration
    public class TomcatConfig {
    
        @Bean
        public EmbeddedServletContainerFactory servletContainer(){
            TomcatEmbeddedServletContainerFactory factory = new TomcatEmbeddedServletContainerFactory();
            factory.setUriEncoding(Charset.forName("UTF-8"));
            factory.addAdditionalTomcatConnectors(createSslConnector());
            return factory;
        }
    
        private Connector createSslConnector() {
            Connector connector = new Connector("org.apache.coyote.http11.Http11NioProtocol");
            Http11NioProtocol protocol = (Http11NioProtocol) connector.getProtocolHandler();
            try {
                File truststore = new File("/Users/ysl/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);
            }
        }
    }

    通过addAdditionalTomcatConnectors方法添加多个监听连接;此时可以通过http 8088端口,https 8443端口。

  • 相关阅读:
    Hibernate配置文件详解
    Struts工作原理、流程
    java三大框架原理
    JAVA三大框架的各自作用
    tomcat的种种
    jdk及tomcat的配置
    java-io-file
    JAVA-IO-文件过滤
    SPSS-单因素方差分析(ANOVA) 案例解析(转)
    SPSS-比较均值-独立样本T检验 案例解析(转)
  • 原文地址:https://www.cnblogs.com/senlinyang/p/8526633.html
Copyright © 2020-2023  润新知