1. 启用HTTPS
修改配置
application.yml
server:
# port: 80
port: 443
ssl:
enabled: true
key-store: /key_store.jks
key-store-password: key_store_pwd
2. 添加http协议连接器
增加JAVA配置
@Bean
public ServletWebServerFactory servletWebServerFactory() {
TomcatServletWebServerFactory tomcat = new TomcatServletWebServerFactory();
Connector connector = new Connector(TomcatServletWebServerFactory.DEFAULT_PROTOCOL);
connector.setPort(80);
tomcat.addAdditionalTomcatConnectors(connector);
return tomcat;
}
扩展知识点:springboot如何配置,同时支持https和http
使用jdk自带的keytools创建证书
keytool -genkey -alias tomcat -keyalg RSA -keystore ./server.keystore
按照提示完成操作
输入密钥库口令:123456
再次输入新口令:123456
您的名字与姓氏是什么?
[Unknown]: kaibowang
您的组织单位名称是什么?
[Unknown]: yuxuelian
您的组织名称是什么?
[Unknown]: yuxuelian
您所在的城市或区域名称是什么?
[Unknown]: chengdu
您所在的省/市/自治区名称是什么?
[Unknown]: chengdushi
该单位的双字母国家/地区代码是什么?
[Unknown]: china
CN=kaibowang, OU=yuxuelian, O=yuxuelian, L=chengdu, ST=chengdushi, C=china是否正确?
[否]: y输入 <tomcat> 的密钥口令
(如果和密钥库口令相同, 按回车):
再次输入新口令:Warning:
JKS 密钥库使用专用格式。建议使用 "keytool -importkeystore -srckeystore C:\Users\Administrator\.keystore -destkeystore C:\Users\Administrator\.keystore -deststoretype pkcs12" 迁移到行业标准格式 PKCS12。
创建完成后,可在用户根目录查看生成的keystore文件
将生成的keystore文件复制到项目的根目录下
在application.yml中添加配置
server:
port: 443
ssl:
key-store: server.keystore
key-store-password: 生成server.keystore时输入的密码
key-alias: tomcat
key-store-type: JKS
在application启动文件中添加配置
package com.cisdi.info.simple;
import org.apache.catalina.connector.Connector;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.domain.EntityScan;
import org.springframework.boot.autoconfigure.security.servlet.SecurityAutoConfiguration;
import org.springframework.boot.web.embedded.tomcat.TomcatServletWebServerFactory;
import org.springframework.boot.web.servlet.server.ServletWebServerFactory;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.context.annotation.Bean;
@SpringBootApplication(scanBasePackages = {"com.cisdi.info.simple.*"}, exclude = {SecurityAutoConfiguration.class})
@EntityScan("com.cisdi.info.simple.*")
@EnableDiscoveryClient(autoRegister = false)
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
//配置http
@Bean
public ServletWebServerFactory servletContainer() {
TomcatServletWebServerFactory tomcat = new TomcatServletWebServerFactory();
tomcat.addAdditionalTomcatConnectors(createStandardConnector()); // 添加http
return tomcat;
}
private Connector createStandardConnector() {
Connector connector = new Connector("org.apache.coyote.http11.Http11NioProtocol");
connector.setPort(8080);
return connector;
}
}
完成配置
以上就是springboot 同时启用http/https的配置方法的详细内容,更多关于springboot启用http/https的资料请关注编程网其它相关文章!