spring boot項目配置ssl證書


spring boot項目配置ssl證書

1. 生成所需的證書

本教程參考了其他教程因此, 證書類型統一為jks,先將現在通用的pfx證書轉換為jks

cd /d %JAVA_HOME%/bin

keytool -importkeystore -srckeystore *****.pfx -destkeystore ****.jks -srcstoretype PKCS12 -deststoretype JKS

# 67f764daed912a95b603b8128b03d043

#Warning:
#JKS 密鑰庫使用專用格式。建議使用 "keytool -importkeystore -srckeystore ****.jks -destkeystore ****.jks -deststoretype pkcs12" 遷移到行業標准格式 PKCS12。

2. 配置yaml

#https加密端口號 443
server.port=443
#SSL證書路徑 一定要加上classpath:
#改成自己需要的證書名
server.ssl.key-store=classpath:*****.jks
#SSL證書密碼
server.ssl.key-store-password=12345678
#證書類型
server.ssl.key-store-type=JKS
#證書別名
server.ssl.key-alias=1

server.ssl.enabled=true

3. 更改啟動類

package com.barry.login;

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.context.annotation.Bean;



@SpringBootApplication

public class DiyApplication  {

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

    /**
     * http重定向到https
     * @return
     */
    @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監聽的http的端口號
        connector.setPort(8080);
        connector.setSecure(false);
        //監聽到http的端口號后轉向到的https的端口號
        connector.setRedirectPort(443);//可調整到自己所需要的端口號
        return connector;
    }
}

4. pom配置

編譯之后會進行壓縮等算法, 會破壞密鑰, 需要設置

配置完之后, 先clean

<build>
    <resources>
        <resource>
            <directory>src/main/resources</directory>
            <filtering>true</filtering>
            <excludes>
                <exclude>*.jks</exclude>
            </excludes>
        </resource>
        <resource>
            <directory>src/main/resources</directory>
            <filtering>false</filtering>
            <includes>
                <include>*.jks</include>
            </includes>
        </resource>
    </resources>

</build>

5. 參考

證書配置

https://blog.csdn.net/sinat_40399893/article/details/79860942   

maven配置

https://blog.csdn.net/kevin_mails/article/details/84590449

filtering已經是false了,沒毛病,但是還是不行,這是為啥?(certificate目錄下是.jks文件),看到個別有的文章說配置了filtering=false 沒效,估計跟我這配置應該是差不多的,參考下面的方法:

后來調整了一下配置:


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM