SpringBoot 配置加密證書


1、在雲平台下載證書

        1、登錄阿里雲SSL證書控制台。

    2、在SSL證書頁面,單擊已簽發標簽,定位到需要下載的證書並單擊證書卡片右下角的下載。下載

       3、定位到您需要安裝證書的服務器類型並單擊右側操作欄的下載將證書壓縮包下載到本地。

   4、將證書解壓縮后安裝到您的Web服務器上。

    阿里雲SSL證書支持安裝到以下類型的Web服務器:Tomcat(筆者這里下載的是這個證書,以下都是基於此證書進行操作)ApacheNginxIIS其他

2、將下載的證書通過jdk自帶工具生成jks文件

  1、進入到jdk的bin目錄,進行cmd操作

  2、 以下為操作命令,其中第一處標紅的字,為你下載的證書的路徑,第二處標紅為你給自己的證書取的名字

 keytool -importkeystore -srckeystore D:\3358419__xftm.com_tomcat\3358419__xftm.com.pfx  -destkeystore xftmapp.jks -srcstoretype PKCS12 -
deststoretype JKS

 

  3、具體操作如下,筆者紅色框框標出來的為筆者操作過程中的錯誤,以及需要注意的事項

 

 

 

 

 3、SpringBoot代碼

 
  1、將步驟2生成的.jks文件復制到項目中,與application同目錄

 

 

  2、application啟動類(去掉紅色代碼兼容http和https,否則只兼容https)

package org.xftm.app;

 
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.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.Banner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.context.embedded.EmbeddedServletContainerFactory;
import org.springframework.boot.context.embedded.tomcat.TomcatEmbeddedServletContainerFactory;
import org.springframework.boot.web.client.RestTemplateBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.web.client.RestTemplate;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

@SpringBootApplication
@EnableSwagger2//Swaggerapi集成,入不需要可直接刪除
public class Application {


    @Value("${server.httpPort}")
    private String httpPort;//在yml文件中自定義的http端口
    @Value("${server.httpsPort}")
    private String httpsPort; //在yml文件中自定義https端口


    @Autowired
    RestTemplateBuilder restTemplateBuilider;

    public static void main(String[] args) {
        SpringApplication application = new SpringApplication(Application.class);
        application.setBannerMode(Banner.Mode.CONSOLE);//筆者自定義的banner 此處可按照Springboot默認啟動方式啟動
        application.run(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");
        System.out.println("httpport="+httpPort);
        System.out.println("httpsPort="+httpsPort);

        // Connector監聽的http的端口號
        connector.setPort(httpPort ==null?8080:Integer.valueOf(httpPort));
        connector.setSecure(false);
        // 監聽到http的端口號后轉向到的https的端口號
        connector.setRedirectPort(httpsPort ==null?443:Integer.valueOf(httpPort));         return connector;
    }

    @Bean
    public RestTemplate restTemplate() {
        // 使用build()方法進行獲取
        return restTemplateBuilider.build();
    }

}

 

3、ssl的配置

server:
  #htpp轉發到https的端口
  port: 8443
  #http請求端口
  httpPort: 8767
  #https請求端口
  httpsPort: 8443
  ssl:
    key-store:  classpath:xftmapp.jks
    key-store-password:  TVPxezV9
    key-store-type:  JKS
    key-alias:  alias
  context-path: /XftmApp
    #address: 0.0.0.0
  tomcat:
      #uri-encoding: UTF-8
    max-threads: 2000
    max-connections: 20000

4、測試(筆者不演示自己的路徑)

請求方式為:http://ip:httpPort/

請求方式為:https://ip:httpsPort/


免責聲明!

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



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