在controller中协议一般为http,有些时候需要https协议。
生成keystore文件
在jdk安装目录下找到keytool.exe文件
进入cmd,输入命令
keytool -genkey -alias tomcat(自定义名称) -keypass 123456(自定义密码1)?-keyalg RSA -keysize 1024 -validity 365 -keystore D:/keys/tomcat.keystore -storepass 123456(自定义密码2)
keytool -genkey -alias tomcat -keypass 123456 -keyalg RSA -keysize 1024 -validity 365 -keystore D:/keys/tomcat.keystore -storepass 123456
注意:存放keystore文件的目录需提前创建好
?
?将keystore文件复制到项目的resources文件夹下
?application.yml配置文件
server:
port: 8189
ssl:
key-store: src\main\resources\tomcat.keystore
key-store-password: 123456
key-store-type: JKS
key-alias: tomcat
启动类
@SpringBootApplication
@MapperScan("com.safe.edu.dao")
public class StartSafeEduApp {
public static void main(String[] args) throws Exception {
SpringApplication.run(StartSafeEduApp.class, args);
}
// 配置tomcat
@Bean
public ServletWebServerFactory servletContainer() {
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;
}
/**
* 让我们的应用支持HTTP是个好想法,但是需要重定向到HTTPS,
* 但是不能同时在application.yml中同时配置两个connector,
* 所以要以编程的方式配置HTTP connector,然后重定向到HTTPS connector
*
* @return Connector
*/
private Connector initiateHttpConnector() {
Connector connector = new Connector("org.apache.coyote.http11.Http11NioProtocol");
connector.setScheme("http");
connector.setPort(8190); // http端口
connector.setSecure(false);
connector.setRedirectPort(8189); // application.yml中配置的https端口
return connector;
}
}
?启动项目
?
?
|