spring boot實(shí)戰(zhàn)之內(nèi)嵌容器tomcat配置
本文介紹了spring boot實(shí)戰(zhàn)之內(nèi)嵌容器tomcat配置,分享給大家,具體如下:
默認(rèn)容器
spring boot默認(rèn)web程序啟用tomcat內(nèi)嵌容器tomcat,監(jiān)聽8080端口,servletPath默認(rèn)為 / 通過需要用到的就是端口、上下文路徑的修改,在spring boot中其修改方法及其簡單;
在資源文件中配置:
server.port=9090 server.contextPath=/lkl
啟動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.184)
可以看出其監(jiān)聽端口9090,執(zhí)行 http://localhost:9090/lkl/springboot/liaokailin 成功訪問
自定義tomcat
在實(shí)際的項(xiàng)目中簡單的配置tomcat端口肯定無法滿足大家的需求,因此需要自定義tomcat配置信息來靈活的控制tomcat。
以定義默認(rèn)編碼為例
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 配置
* @author liaokailin
* @version $Id: TomcatConfig.java, v 0.1 2015年10月4日 上午12:11:47 liaokailin Exp $
*/
@Configuration
public class TomcatConfig {
@Bean
public EmbeddedServletContainerFactory servletContainer() {
TomcatEmbeddedServletContainerFactory tomcat = new TomcatEmbeddedServletContainerFactory();
tomcat.setUriEncoding("UTF-8");
return tomcat;
}
}
構(gòu)建EmbeddedServletContainerFactory的bean,獲取到TomcatEmbeddedServletContainerFactory實(shí)例以后可以對tomcat進(jìn)行設(shè)置,例如這里設(shè)置編碼為UTF-8
SSL配置
生成證書
keytool -genkey -alias springboot -keyalg RSA -keystore /Users/liaokailin/software/ca1/keystore 設(shè)置密碼123456
tomcat中驗(yàn)證證書是否正確
修改tomcat/conf/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 ,訪問 http://localhost:8443
spring boot 內(nèi)嵌tomcat ssl
配置資源文件
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 啟動tomcat ssl配置
- server.ssl.keyAlias 別名
- server.ssl.keyPassword 密碼
- server.ssl.keyStore 位置
啟動 spring boot
訪問https://localhost:8443/springboot/helloworld
多端口監(jiān)聽配置
前面啟動ssl后只能走h(yuǎn)ttps,不能通過http進(jìn)行訪問,如果要監(jiān)聽多端口,可采用編碼形式實(shí)現(xiàn)。
1.注銷前面ssl配置,設(shè)置配置 server.port=9090
2.修改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 配置
* @author liaokailin
* @version $Id: TomcatConfig.java, v 0.1 2015年10月4日 上午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;
} catch (Exception ex) {
throw new IllegalStateException("cant access keystore: [" + "keystore" + "] ", ex);
}
}
}
通過addAdditionalTomcatConnectors方法添加多個(gè)監(jiān)聽連接;此時(shí)可以通過http 9090端口,https 8443端口。
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
【Java】BigDecimal實(shí)現(xiàn)加減乘除運(yùn)算代碼
本篇文章主要介紹了【Java】BigDecimal實(shí)現(xiàn)加減乘除運(yùn)算代碼,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-02-02
java并發(fā)編程synchronized底層實(shí)現(xiàn)原理
這篇文章主要介紹了java并發(fā)編程synchronized底層實(shí)現(xiàn)原理2022-02-02
在mybatis執(zhí)行SQL語句之前進(jìn)行攔擊處理實(shí)例
本篇文章主要介紹了在mybatis執(zhí)行SQL語句之前進(jìn)行攔擊處理實(shí)例,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下。2017-04-04
springboot?項(xiàng)目啟動后無日志輸出直接結(jié)束的解決
這篇文章主要介紹了springboot?項(xiàng)目啟動后無日志輸出直接結(jié)束的解決方案,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-12-12

