一.SSL證書
在阿里云申請免費SSL證書壶运,并下載tomcat版本舒裤,解壓后會得到兩個文件淘这,一個是ca證書(pfx后綴的文件)掉伏,另一個是ca密碼
二.阿里云上配置安全組規(guī)則
三.代碼部分
1.application.properties
server.port = 443
server.ssl.key-store=C:/v1.0/123.com.pfx
server.ssl.key-store-password=你的密碼
server.port = 443是https的默認端口
其中server.ssl.key-store的值是ca證書路徑
server.ssl.key-store-password是證書密碼
2.在springboot啟動類里面添加代碼
public static void main(String[] args) {
SpringApplication.run(QbtApplication.class, args);
}
@Bean
public TomcatServletWebServerFactory servletContainer() {
TomcatServletWebServerFactory tomcat = new TomcatServletWebServerFactory() {
@Override
protected void postProcessContext(Context context) {
SecurityConstraint constraint = new SecurityConstraint();
constraint.setUserConstraint("CONFIDENTIAL");
SecurityCollection collection = new SecurityCollection();
collection.addPattern("/*");
constraint.addCollection(collection);
context.addConstraint(constraint);
}
};
tomcat.addAdditionalTomcatConnectors(httpConnector());
return tomcat;
}
@Bean
public Connector httpConnector() {
Connector connector = new Connector("org.apache.coyote.http11.Http11NioProtocol");
connector.setScheme("http");
//Connector監(jiān)聽的http的端口號
connector.setPort(80);
connector.setSecure(false);
//監(jiān)聽到http的端口號后轉向到的https的端口號
connector.setRedirectPort(443);
return connector;
}
四.測試
發(fā)布到阿里云服務器卿吐,測試如下
成功V住T印稽物!