文章轉載至:https://www.cnblogs.com/hhhshct/p/10276319.html
阿里雲域名申請
域名申請比較簡單,使用微信注冊阿里雲賬號並登陸,點擊產品,選擇域名注冊
輸入你想注冊的域名
進入域名購買頁面,搜索可用的后綴及價格,越熱門的后綴(.com,.cn)越貴一般,並且很可能已經被注冊。
最后,付款購買即可。
申請ssl證書
還是進入首頁,點擊產品按鈕,在下拉菜單中選擇ssl證書,進入后點立即購買,在下圖中做如下選擇
ssl證書是要與域名綁定的,按要求填好域名和郵箱,密碼可以不填
填寫好,選擇下一步,然后選擇手動dns,提交,然后查看證書詳情,如果域名列表有填寫的域名,並且域名解析是騰訊雲,那么會出現自動DNS驗證。
進入域名解析頁面,找到你剛創建的域名,點擊解析,添加上面的記錄
稍等1分鍾,審核就會通過,然后就可以下載ssl證書,加壓后有對應nginx、tomcat、apache等的證書,我們配置springboot,所以選擇tomcat。
springboot配置https
新建一個springboot項目,加入web模塊,將我們的證書copy到resrouce目錄下,同時在application.yml中添加如下配置。
server: port: 443 ssl: enabled: true key-store-password: zhanjian1514 key-store: classpath:zhanjianmxcz.cn.jks key-store-type: JKS condition: http2https: true http: port: 80
修改啟動器,使其支持將http請求自動轉化為https請求
package io.powerx; 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.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; import org.springframework.boot.web.embedded.tomcat.TomcatServletWebServerFactory; import org.springframework.context.annotation.Bean;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@SpringBootApplication public class TestsslApplication {
@RequestMapping(value = "/test")
public String test() {
return "hello https";
}
public static void main(String[] args) { SpringApplication.run(TestsslApplication.class, args); } // 如果沒有使用默認值80 @Value("${http.port:80}") Integer httpPort; // 正常啟用的https端口 如443 @Value("${server.port}") Integer httpsPort; // springboot2 寫法 @Bean @ConditionalOnProperty(name = "condition.http2https", havingValue = "true", matchIfMissing = false) 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 @ConditionalOnProperty(name = "condition.http2https", havingValue = "true", matchIfMissing = false) public Connector httpConnector() { System.out.println("啟用http轉https協議,http端口:" + this.httpPort + ",https端口:" + this.httpsPort); Connector connector = new Connector("org.apache.coyote.http11.Http11NioProtocol"); connector.setScheme("http"); // Connector監聽的http的端口號 connector.setPort(httpPort); connector.setSecure(false); // 監聽到http的端口號后轉向到的https的端口號 connector.setRedirectPort(httpsPort); return connector; } }
最后也是最重要的,如果你使用的是雲服務器,那么只需要在騰訊雲平台配置下域名解析(選擇快速添加網站,配置雲服務器的公網ip,按照提示添加下面兩條記錄),注意配置安全組,開放80和443端口,然后將項目打包部署到雲服務器上即可。
如果想要長期在后端運行,操作如下:
- 創建腳本文件startDemo.sh,內容為:
,&不能省略,demo.jar為所要運行的jar,注意路徑
- 運行腳本,
- 查看ip和端口表示成功,
如果你是在本地服務器部署,則首先需要進行外網映射配置,我這里就是將我要部署的服務器映射到公網ip的1234端口(也想用80端口,無奈沒有開通),然后再去騰訊雲配置域名解析,最后部署即可,貼上我的安全標示,哈哈