因为项目利用了webRTC技术,查到解决方案是采用https可以把本地demo跑起来,于是就用springboot整合了https,本地demo也是顺利跑起来了。为了避免整合入坑,把细节代码贴出来。
1.生成证书
keytool -genkey -alias tomcat -keyalg RSA -keystore tomcatkeystore.keystore -dname "CN=localhost, OU=localhost, O=localhost, L=SH, ST=SH, C=CN" -keypass 123456
需要下载keytool插件,命令是在当前目录下生成 tomcatkeystore.keystore证书
2.配置springboot
2.1关于yml文件的配置
server:
port: 8080
custom:
httpPort: 8890
servlet:
session:
timeout: 1800
ssl:
key-store: tomcatkeystore.keystore
key-store-password: 123456
key-store-type: JKS
key-alias: tomcat
spring:
thymeleaf:
prefix: classpath:/templates/
servlet:
multipart:
max-file-size: 50MB
max-request-size: 50MB
location: /data/upload_tmp
2.2关于书写配置类
package com.sap.intellgentcam.demo1.config;
import org.apache.catalina.Context;
import org.apache.catalina.connector.Connector;
import org.apache.tomcat.util.descriptor.web.SecurityCollection;
import org.apache.tomcat.util.descriptor.web.SecurityConstraint;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.web.embedded.tomcat.TomcatServletWebServerFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
/**
* @author : Jenson.Liu
* @date : 2019/11/11 2:51 下午
*/
@Configuration
public class HttpsConfig {
//读写配置文件
@Value("${server.custom.httpPort}")
private Integer httpPort;
@Value("${server.port}")
private Integer httpsPort;
@Bean
public TomcatServletWebServerFactory servletContainer() { //springboot2 新变化
TomcatServletWebServerFactory tomcat = new TomcatServletWebServerFactory() {
@Override
protected void postProcessContext(Context context) {
SecurityConstraint securityConstraint = new SecurityConstraint();
securityConstraint.setUserConstraint("CONFIDENTIAL");
SecurityCollection collection = new SecurityCollection();
collection.addPattern("/*");
securityConstraint.addCollection(collection);
context.addConstraint(securityConstraint);
}
};
tomcat.addAdditionalTomcatConnectors(initiateHttpConnector());
return tomcat;
}
private Connector initiateHttpConnector() {
Connector connector = new Connector("org.apache.coyote.http11.Http11NioProtocol");
connector.setScheme("http");
connector.setPort(httpPort);
connector.setSecure(false);
connector.setRedirectPort(httpsPort);
return connector;
}
}
结合配置文件和配置类表达的意思:
当你使用8890端口请求的时候使用http协议
当你使用8080端口请求的时候使用https协议