springboot 配置兼容 https和http 访问
springboot版本 <!-- 继承springBoot默认值 --> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.4.0.RELEASE</version> </parent>
package cn.com.xxx;
import com.itextpdf.text.io.StreamUtil;
import org.apache.catalina.connector.Connector;
import org.apache.coyote.http11.Http11NioProtocol;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
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.ComponentScan;
import org.springframework.core.io.ClassPathResource;
import java.io.File;
import java.io.IOException;
@SpringBootApplication
@ComponentScan(basePackages={"cn.com.xxxx.*","com.xxxx.*"})
public class MainApplication {
@Value("${http.port}")
private Integer port;
// 这是spring boot 1.5.X以下版本的 添加了这个
@Bean
public EmbeddedServletContainerFactory servletContainer() {
TomcatEmbeddedServletContainerFactory tomcat = new TomcatEmbeddedServletContainerFactory();
tomcat.addAdditionalTomcatConnectors(createStandardConnector()); // 添加http
return tomcat;
}
// 配置http
private Connector createStandardConnector() {
Connector connector = new Connector("org.apache.coyote.http11.Http11NioProtocol");
connector.setPort(port);
return connector;
}
public static void main(String[] args) {
SpringApplication.run(MainApplication.class, args);
// new StreamUtil();
}
}
配置文件里的配置
server.port=8018
server.ssl.key-store=classpath:server.pfx
server.ssl.key-store-password=xxxx
server.ssl.keyStoreType=PKCS12http.port=8019
tomcat正常启动后:控制台 会打印 https(8018) 和(http)8019
在界面通过https://localhost:8018和http://localhost8019 都可以访问