SpringBoot入门之内嵌Tomcat配置

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

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

启动程序,日志如下:

-- ::58.824  INFO  --- [           main] s.b.c.e.t.TomcatEmbeddedServletContainer : Tomcat started on port(s):  (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="" maxThreads=""
scheme="https" secure="true" SSLEnabled="true"
keystoreFile="/Users/liaokailin/software/ca1/keystore" keystorePass=""
clientAuth="false" sslProtocol="TLS"/>

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

spring boot 内嵌tomcat ssl

配置资源文件:

server.port=
server.ssl.enabled=true
server.ssl.keyAlias=springboot
server.ssl.keyPassword=
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();
protocol.setKeystoreFile(truststore.getAbsolutePath());
protocol.setKeystorePass("");
protocol.setKeyAlias("springboot");
return connector;
} catch (Exception ex) {
throw new IllegalStateException("cant access keystore: [" + "keystore" + "] ", ex);
}
}
}

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

上一篇:使用jsoup轻松爬数据


下一篇:Django的Model上都有些什么