springboot開通http和https端口
springboot應用默認內嵌的web容器是tomcat,可以修改默認容器爲jetty
version: springboot 2.0及以上
一 默認內嵌tomcat方式開通http和https端口
1.1啓動類添加tomcat配置
package com.test;
import org.apache.catalina.connector.Connector;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.embedded.tomcat.TomcatServletWebServerFactory;
import org.springframework.boot.web.servlet.server.ServletWebServerFactory;
import org.springframework.boot.web.servlet.support.SpringBootServletInitializer;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.context.annotation.Bean;
import org.springframework.scheduling.annotation.EnableScheduling;
@SpringBootApplication
@EnableCaching
@EnableScheduling
public class TestApplication extends SpringBootServletInitializer {
public static void main(String[] args) {
SpringApplication.run(EasyRecordHsApplication.class, args);
}
@Bean
public ServletWebServerFactory servletContainer() {
TomcatServletWebServerFactory tomcat = new TomcatServletWebServerFactory();
tomcat.addAdditionalTomcatConnectors(createHTTPConnector());
return tomcat;
}
private Connector createHTTPConnector() {
Connector connector = new Connector("org.apache.coyote.http11.Http11NioProtocol");
connector.setScheme("http");
connector.setSecure(false);
connector.setPort(18080);//設置http端口
connector.setRedirectPort(8080); //訪問http端口重定向到https端口
return connector;
}
}
1.2.配置文件配置https證書
# tomcat 配置
server.port=8080
server.servlet.context-path=/testHttps
#https 配置
server.ssl.key-store=classpath:keystore.p12
server.ssl.key-store-password=123456
server.ssl.keyStoreType=PKCS12
server.ssl.keyAlias=tomcat
二 把tomcat替換成jetty需要排除默認的tomcat容器,然後引入依賴,然後配置jetty配置
2.1.修改pom.xml
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<!--排除默認的tomcat容器-->
<exclusions>
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
</exclusion>
</exclusions>
</dependency>
<!--添加jetty依賴-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jetty</artifactId>
</dependency>
2.2添加jetty配置類
package com.sinosoft.easyrecordhs.config;
import org.eclipse.jetty.http.HttpScheme;
import org.eclipse.jetty.http.HttpVersion;
import org.eclipse.jetty.server.*;
import org.eclipse.jetty.util.ssl.SslContextFactory;
import org.eclipse.jetty.util.thread.QueuedThreadPool;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.web.embedded.jetty.JettyServletWebServerFactory;
import org.springframework.boot.web.servlet.MultipartConfigFactory;
import org.springframework.boot.web.servlet.server.ServletWebServerFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import javax.servlet.MultipartConfigElement;
/**
* Created by lijunming
* on date 2018-12-19
* time 12:36
*/
@Configuration
public class JettyConfig {
@Value("${server.ssl.key-store}")
private String path;
@Value("${server.ssl.key-store-password}")
private String password;
@Value("${server.port}")
private int port;
/**
* 通過構造工廠造1個jetty
*/
@Bean
public ServletWebServerFactory servletContainer() {
JettyServletWebServerFactory jetty = new JettyServletWebServerFactory();
customizeJetty(jetty);
return jetty;
}
/**
* 爲jetty服務器開通http端口和https,並配置線程
*/
private void customizeJetty(JettyServletWebServerFactory container) {
container.addServerCustomizers((Server server) -> {
//配置線程
threadPool(server);
// 添加HTTP配置
ServerConnector connector = new ServerConnector(server);
connector.setPort(8888);
// 添加HTTPS配置
SslContextFactory sslContextFactory = new SslContextFactory();
sslContextFactory.setKeyStorePath(path);
sslContextFactory.setKeyStorePassword(password);
HttpConfiguration config = new HttpConfiguration();
config.setSecureScheme(HttpScheme.HTTPS.asString());
config.addCustomizer(new SecureRequestCustomizer());
ServerConnector sslConnector = new ServerConnector(
server,
new SslConnectionFactory(sslContextFactory, HttpVersion.HTTP_1_1.asString()),
new HttpConnectionFactory(config));
sslConnector.setPort(port);
server.setConnectors(new Connector[]{connector, sslConnector});
});
}
/**
* jetty線程配置
*/
private void threadPool(Server server) {
// connections
final QueuedThreadPool threadPool = server.getBean(QueuedThreadPool.class);
//默認最大線程連接數200
threadPool.setMaxThreads(300);
//默認最小線程連接數8
threadPool.setMinThreads(15);
//默認線程最大空閒時間60000ms
threadPool.setIdleTimeout(60000);
}
/**
* 配置文件上傳
*/
@Bean
MultipartConfigElement multipartConfigElement() {
MultipartConfigFactory factory = new MultipartConfigFactory();
// 單個數據大小
factory.setMaxFileSize("100MB"); // KB,MB
/// 總上傳數據大小
factory.setMaxRequestSize("200MB");
return factory.createMultipartConfig();
}
}
3.ssl配置文件和tomcat用一樣的即可,如上圖1.2所配置