首頁 > 軟體

springboot如何將http轉https

2023-04-02 06:02:22

springboot http轉https

一、安全證書的生成

可以使用jdk自帶的證書生成工具,jdk自帶一個叫keytool的證書管理工具,可以用它來實現簽名的證書。

1、進入cmd命令控制終端

2、生成一個證書
別名:alias = tomcat
密碼:keypass = 123456
生成位置:keystore = D:/keys
keys資料夾需要自己先建立好

cmd命令:

keytool -genkey -alias tomcat -keypass 123456 -keyalg RSA -keysize 1024 -validity 365 -keystore D:/keys/tomcat.keystore -storepass 123456
 

 3、獲取tomcat.keystore檔案,放入專案根目錄下面

 二,設定yml檔案

server:
  port: 8443
  ssl:
    key-store: server.keystore
    key-alias: tomcat
    enabled: true
    key-store-type: JKS
    key-store-password: 123456

三、springbootApplication啟動類設定

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.web.embedded.tomcat.TomcatServletWebServerFactory;
import org.springframework.boot.web.servlet.server.ServletWebServerFactory;
import org.springframework.context.annotation.Bean;
 
@SpringBootApplication
public class WeijingApplication {
 
    public static void main(String[] args) {
        SpringApplication.run(WeijingApplication.class, args);
    }
    @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(redirectConnector());
        return tomcat;
    }
 
    private Connector redirectConnector() {
        Connector connector = new Connector("org.apache.coyote.http11.Http11NioProtocol");
        connector.setScheme("http");
        connector.setPort(8080);
        connector.setSecure(false);
        connector.setRedirectPort(8443);
        return connector;
    }

啟動成功

另外:springboot2.xx版本以上可以用上面的方法 如果2.xx以下的 就要換成

EmbeddedServletContainerFactory

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.context.annotation.Bean;
 
@SpringBootApplication
public class WeijingApplication {
 
    public static void main(String[] args) {
        SpringApplication.run(WeijingApplication.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;
    }
 
    @Bean
    public Connector httpConnector() {
        Connector connector = new Connector("org.apache.coyote.http11.Http11NioProtocol");
        connector.setScheme("http");
        //Connector監聽的http的埠號
        connector.setPort(8080);
        connector.setSecure(false);
        //監聽到http的埠號後轉向到的https的埠號
        connector.setRedirectPort(8443);
        return connector;
    }

另外:報錯埠被佔用的話可以看下這個

報錯是因為不能讀取組態檔的埠,那個埠是要被用的

部署到Linux伺服器 https啟動失敗報錯 原因:

部署到伺服器的時候 需要用再linux伺服器上面 重新用Linux的JDK生成證書 (不能用windows生成的證書) 並放再固定的資料夾位置

更改yml檔案設定 

 更改成伺服器資料夾路徑:/usr/local/xxx/server.keystore

到此這篇關於springboot如何將http轉https的文章就介紹到這了,更多相關springboot http轉https內容請搜尋it145.com以前的文章或繼續瀏覽下面的相關文章希望大家以後多多支援it145.com!


IT145.com E-mail:sddin#qq.com