騰訊雲域名申請+ssl證書申請+springboot配置https
阿新 • • 發佈:2019-01-16
enabled 進入 rect 修改 cti 支持 main 項目打包 integer
阿裏雲域名申請
域名申請比較簡單,使用微信註冊阿裏雲賬號並登陸,點擊產品,選擇域名註冊
輸入你想註冊的域名
進入域名購買頁面,搜索可用的後綴及價格,越熱門的後綴(.com,.cn)越貴一般,並且很可能已經被註冊。
最後,付款購買即可。
申請ssl證書
還是進入首頁,點擊產品按鈕,在下拉菜單中選擇ssl證書,進入後點立即購買,在下圖中做如下選擇
ssl證書是要與域名綁定的,按要求填好域名和郵箱,密碼可以不填
填寫好,選擇下一步,然後選擇手動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: o7yp31t85gu key-store: classpath:hehang.xyz.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; importorg.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; @SpringBootApplication public class TestsslApplication { 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端口,然後將項目打包部署到雲服務器上即可。
如果你是在本地服務器部署,則首先需要進行外網映射配置,我這裏就是將我要部署的服務器映射到公網ip的1234端口(也想用80端口,無奈沒有開通),然後再去騰訊雲配置域名解析,最後部署即可,貼上我的安全標示,哈哈
騰訊雲域名申請+ssl證書申請+springboot配置https