springboot部署到阿里雲,配置https,springboot項目同時支持http和https請求,阿里雲配置https

最近在學習springboot,感受springboot開發後臺,提供api接口太方便了。簡直是傻瓜式開發,一直都是本地在跑springboot項目。夢想着有一天,項目能在阿里雲上跑。只有在阿里雲上跑纔是真正的java服務器項目。這裏就帶你們一塊兒把springboot項目部署到阿里雲,而且支持https ####準備工做java

  • 阿里雲ecs一個
  • 域名一個(個人是https://30paotui.com)
  • ca證書一份(用來支持https)
  • 本地打包好的springboot項目。我這裏用jar不用war
  • ftp客戶端一個,用來把jar傳到阿里雲服務器上,我用的是filezilla客戶端,能夠百度下載。

一,購買阿里雲ecs配置安全組規則

  • 若是不配置安全組規則,咱們將無法訪問咱們阿里雲服務器 ,下圖中的80/80和443/443必須配置,由於只有這裏配置了才能支持http和https訪問咱們的網站
    阿里雲安全組規則.png
  • 配置以下,受權對象哪裏最好填0.0.0.0/0
    添加安全組規則.png

二,買域名

至於域名怎麼買,我就不囉嗦了,不會的自行百度 30paotui.com我買的域名 linux

域名.png

三,經過filezilla鏈接阿里雲服務器,運行項目

ftp鏈接服務器.png
我在個人服務器home目錄下新建一個jar文件,把打包好的springboot的jar包放到這裏,個人是qcl80.jar
home:jar.png
而後就能夠經過 java -jar qcl80.jar 運行springboot項目
運行springboot項目.png
注意:這樣運行springboot項目,若是你關閉當前shell窗口,就會致使服務器的springboot關閉。由於咱們如今用的是springboot自帶的tomcat,不能在後臺運行。

springboot生成的jar在阿里雲的linux服務器後臺運行,不會在shell客戶端關閉時關閉

後臺運行腳本.png
經過建立stop.sh , start.sh ,run.sh這三個腳本文件來實現後臺長久運行springboot 這裏我把運行的qcl80.jar ,start .sh,stop.sh ,run.sh都放在home下的jar目錄下 1,建立stop.sh vim stop.sh 建立文件而後把下面內容複製進去,必定要把qcl80.jar替換成你的jar

#!/bin/bash
PID=$(ps -ef | grep qcl80.jar | grep -v grep | awk '{ print $2 }')
if [ -z "$PID" ]
then
    echo Application is already stopped
else
    echo kill $PID
    kill $PID
fi
複製代碼

2,建立start.sh,這裏咱們用80端口,這樣能夠直接經過ip訪問,不用再輸端口了 vim start.sh 輸入這個命令後而後把下面的內容複製進去web

#!/bin/bash
nohup java -jar qcl80.jar --server.port=80 &
複製代碼

3,建立run.shspring

整合了關閉和啓動的腳本:run.sh,因爲會先執行關閉應用,而後再啓動應用,這樣不會引發端口衝突等問題,適合在持續集成系統中進行反覆調用。 把下面內容複製進去,必定要注意複製時不能少東西shell

#!/bin/bash
echo stop application
source stop.sh
echo start application
source start.sh
複製代碼

4,start .sh,stop.sh ,run.sh都建立後 ./run.sh 運行run.sh腳本 若是遇到沒有權限運行的問題,就在run.sh所在目錄下執行 chmod u+x *.sh 這樣就ok了。 執行完之後,咱們能夠去nohup.out文件中查看啓動的log cat nohup.out 這個命令能夠查看jar啓動的logapache

run.sh啓動jar項目.png

到此咱們的springboot項目就啓動了,能夠經過你阿里雲的公網ip訪問你的網站了 vim

ip訪問.png

https訪問.png
因爲我配置了https,因此這裏用IP訪問會顯示不安全,接下來給你們講解怎麼配置https訪問。

四,配置https

1,申請阿里雲免費的ca證書,ca證書是實現https必不可少的

ca證書購買.png
ca免費購買.png
ca補全信息.png
一般審覈10分鐘左右就行,若是資料不全或者不真實可能就久些。

  • 購買完ca證書,而且審覈經過後,就去下載相應的ca證書,因爲咱們springboot內置的是tomcat,因此咱們這裏下載tomcat對於的ca證書
    下載證書.png

下載後解壓 api

pfx證書.png

而後在咱們的springboot配置文件中配置 tomcat

ca配置.png

注意:214590826650132.pfx還須要在咱們能阿里雲的home/jar目錄下放一份,即和咱們的打包jar放在同一個目錄下 安全

ca證書放到阿里雲.png

HTTP自動轉向HTTPS

實現http轉https就是咱們訪問 30paotui.com www.30paotui.com 30paotui.com www.30paotui.com 都會指向30paotui.com

  • 實現上面的功能須要咱們在springboot的application中配置以下代碼
package com.qcl;

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.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.context.embedded.ConfigurableEmbeddedServletContainer;
import org.springframework.boot.context.embedded.EmbeddedServletContainerCustomizer;
import org.springframework.boot.context.embedded.EmbeddedServletContainerFactory;
import org.springframework.boot.context.embedded.tomcat.TomcatEmbeddedServletContainerFactory;
import org.springframework.context.annotation.Bean;

@SpringBootApplication
public class SellApplication implements EmbeddedServletContainerCustomizer {

    public static void main(String[] args) {
        SpringApplication.run(SellApplication.class, args);
    }


    //攔截全部請求
    @Bean
    public EmbeddedServletContainerFactory servletContainer() {
        TomcatEmbeddedServletContainerFactory tomcat = new TomcatEmbeddedServletContainerFactory() {
            @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;
    }

    //配置http轉https
    @Bean
    public Connector httpConnector() {
        Connector connector = new Connector(TomcatEmbeddedServletContainerFactory.DEFAULT_PROTOCOL);
        connector.setScheme("http");
        //Connector監聽的http的端口號
        connector.setPort(80);
        connector.setSecure(false);
        //監聽到http的端口號後轉向到的https的端口號
        connector.setRedirectPort(443);
        return connector;
    }

    //這裏設置默認端口爲443,即https的,若是這裏不設置,會https和http爭奪80端口
    @Override
    public void customize(ConfigurableEmbeddedServletContainer container) {
        container.setPort(443);
    }
}

複製代碼

至此,咱們的springboot就能夠在阿里雲上運行了,同時支持http和https的訪問

https.png
相關文章
相關標籤/搜索