默认容器
spring boot默认web程序启用tomcat内嵌容器tomcat,监听8080端口,servletPath默认为 / 。需要用到的就是端口、上下文路径的修改,在spring boot中其修改方法极其简单,实例如下:
server.port=8088
server.context-path=/test
启动程序,日志如下:
2021-08-17 15:28:36.239 INFO 25340 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat started on port(s): 8088 (http) with context path ''
2021-08-17 15:28:36.254 INFO 25340 --- [ main] c.e.springboot.SpringbootApplication : Started SpringbootApplication in 1.295 seconds (JVM running for 1.822)
可看出监听端口为8088
自定义tomcat
在实际的项目中简单的配置tomcat端口肯定无法满足大家的需求,因此需要自定义tomcat配置信息来灵活的控制tomcat。下面的实例定义了tomcat的默认编码格式
package com.example.springboot;
import org.springframework.boot.web.embedded.tomcat.TomcatServletWebServerFactory;
import org.springframework.boot.web.servlet.server.ConfigurableServletWebServerFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import java.nio.charset.Charset;
@Configuration
public class TomcatConfig {
@Bean
public ConfigurableServletWebServerFactory configurableServletWebServerFactory(){
TomcatServletWebServerFactory factory = new TomcatServletWebServerFactory();
factory.setPort(8088);
factory.setUriEncoding(Charset.forName("UTF-8"));
return factory;
}
}
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/ymm/software/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/ymm/software/keystore
- server.ssl.enabled 启动tomcat ssl配置
- server.ssl.keyAlias 别名
- server.ssl.keyPassword 密码
- server.ssl.keyStore 位置
多端口监听配置
前面启动ssl后只能走https,不能通过http进行访问,如果要监听多端口,可采用编码形式实现。
1.注销前面ssl配置,设置配置 server.port=8088
2.修改TomcatConfig.java
package com.example.springboot;
import org.apache.catalina.connector.Connector;
import org.apache.coyote.http11.Http11NioProtocol;
import org.springframework.boot.web.embedded.tomcat.TomcatServletWebServerFactory;
import org.springframework.boot.web.servlet.server.ConfigurableServletWebServerFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import java.io.File;
import java.nio.charset.Charset;
@Configuration
public class TomcatConfig {
@Bean
public ConfigurableServletWebServerFactory configurableServletWebServerFactory(){
TomcatServletWebServerFactory factory = new TomcatServletWebServerFactory();
factory.setPort(8088);
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/ymm/software/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端口。