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)聽(tīng)8080端口,servletPath默認(rèn)為 / 通過(guò)需要用到的就是端口、上下文路徑的修改,在spring boot中其修改方法及其簡(jiǎn)單;
在資源文件中配置:
server.port=9090 server.contextPath=/lkl
啟動(dòng)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)聽(tīng)端口9090,執(zhí)行 http://localhost:9090/lkl/springboot/liaokailin 成功訪問(wèn)
自定義tomcat
在實(shí)際的項(xiàng)目中簡(jiǎn)單的配置tomcat端口肯定無(wú)法滿足大家的需求,因此需要自定義tomcat配置信息來(lái)靈活的控制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í)例以后可以對(duì)tomcat進(jìn)行設(shè)置,例如這里設(shè)置編碼為UTF-8
SSL配置
生成證書(shū)
keytool -genkey -alias springboot -keyalg RSA -keystore /Users/liaokailin/software/ca1/keystore 設(shè)置密碼123456
tomcat中驗(yàn)證證書(shū)是否正確
修改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"/>
啟動(dòng)tomcat ,訪問(wèn) 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 啟動(dòng)tomcat ssl配置
- server.ssl.keyAlias 別名
- server.ssl.keyPassword 密碼
- server.ssl.keyStore 位置
啟動(dòng) spring boot
訪問(wèn)https://localhost:8443/springboot/helloworld
多端口監(jiān)聽(tīng)配置
前面啟動(dòng)ssl后只能走h(yuǎn)ttps,不能通過(guò)http進(jìn)行訪問(wèn),如果要監(jiān)聽(tīng)多端口,可采用編碼形式實(shí)現(xiàn)。
1.注銷(xiāo)前面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);
}
}
}
通過(guò)addAdditionalTomcatConnectors方法添加多個(gè)監(jiān)聽(tīng)連接;此時(shí)可以通過(guò)http 9090端口,https 8443端口。
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
SpringBoot前后端json數(shù)據(jù)交互的全過(guò)程記錄
現(xiàn)在大多數(shù)互聯(lián)網(wǎng)項(xiàng)目都是采用前后端分離的方式開(kāi)發(fā),下面這篇文章主要給大家介紹了關(guān)于SpringBoot前后端json數(shù)據(jù)交互的相關(guān)資料,文中通過(guò)實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下2022-03-03
SpringBoot整合JDBC的實(shí)現(xiàn)
這篇文章主要介紹了SpringBoot整合JDBC的實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2021-01-01
關(guān)于spring依賴注入的方式以及優(yōu)缺點(diǎn)
這篇文章主要介紹了關(guān)于spring依賴注入的方式以及優(yōu)缺點(diǎn),依賴注入,是IOC的一個(gè)方面,是個(gè)通常的概念,它有多種解釋,這概念是說(shuō)你不用創(chuàng)建對(duì)象,而只需要描述它如何被創(chuàng)建,需要的朋友可以參考下2023-07-07
如何使用Jackson和JSON Pointer查詢解析任何JSON節(jié)點(diǎn)
本文介紹了JSON Pointer是字符串表達(dá)式,可以非常方便解析復(fù)雜JSON節(jié)點(diǎn)值,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-09-09
在Docker中部署Spring Boot項(xiàng)目過(guò)程詳解
這篇文章主要介紹了在Docker中部署Spring Boot項(xiàng)目,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2019-08-08
在Java中編輯PowerPoint?PPTX文檔的操作過(guò)程
構(gòu)建用于程序化編輯?Open?Office?XML文檔(如?PowerPoint、Excel?和?Word)的應(yīng)用程序從未如此簡(jiǎn)單,在本文中,我們將專(zhuān)門(mén)討論?PowerPoint?演示文稿?XML(PPTX)文件的結(jié)構(gòu),并學(xué)習(xí)如何操作?PPTX?內(nèi)容的基本過(guò)程,需要的朋友可以參考下2025-04-04
Springboot使用ResponseBody漢字返回問(wèn)號(hào)問(wèn)題
這篇文章主要介紹了Springboot使用ResponseBody漢字返回問(wèn)號(hào)問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-06-06

