欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

springboot 同時(shí)啟用http/https的配置方法

 更新時(shí)間:2021年05月14日 09:59:00   作者:路過君_P  
本文給大家分享springboot 同時(shí)啟用http/https的配置方法,通過修改配置文件、增加java配置的方法來實(shí)現(xiàn)此操作,具體內(nèi)容詳情跟隨小編通過本文學(xué)習(xí)下吧

1. 啟用HTTPS

修改配置

application.yml

server:
# port: 80
  port: 443
  ssl:
    enabled: true
    key-store: /key_store.jks
    key-store-password: key_store_pwd

2. 添加http協(xié)議連接器

增加JAVA配置

@Bean
public ServletWebServerFactory servletWebServerFactory() {
    TomcatServletWebServerFactory tomcat = new TomcatServletWebServerFactory();
    Connector connector = new Connector(TomcatServletWebServerFactory.DEFAULT_PROTOCOL);
    connector.setPort(80);
    tomcat.addAdditionalTomcatConnectors(connector);
    return tomcat;
}

擴(kuò)展知識點(diǎn):springboot如何配置,同時(shí)支持https和http

使用jdk自帶的keytools創(chuàng)建證書

keytool -genkey -alias tomcat -keyalg RSA -keystore ./server.keystore

按照提示完成操作

輸入密鑰庫口令:123456
再次輸入新口令:123456
您的名字與姓氏是什么?
  [Unknown]:  kaibowang
您的組織單位名稱是什么?
  [Unknown]:  yuxuelian
您的組織名稱是什么?
  [Unknown]:  yuxuelian
您所在的城市或區(qū)域名稱是什么?
  [Unknown]:  chengdu
您所在的省/市/自治區(qū)名稱是什么?
  [Unknown]:  chengdushi
該單位的雙字母國家/地區(qū)代碼是什么?
  [Unknown]:  china
CN=kaibowang, OU=yuxuelian, O=yuxuelian, L=chengdu, ST=chengdushi, C=china是否正確?
  [否]:  y

輸入 <tomcat> 的密鑰口令
        (如果和密鑰庫口令相同, 按回車):
再次輸入新口令:

Warning:
JKS 密鑰庫使用專用格式。建議使用 "keytool -importkeystore -srckeystore C:\Users\Administrator\.keystore -destkeystore C:\Users\Administrator\.keystore -deststoretype pkcs12" 遷移到行業(yè)標(biāo)準(zhǔn)格式 PKCS12。

創(chuàng)建完成后,可在用戶根目錄查看生成的keystore文件

將生成的keystore文件復(fù)制到項(xiàng)目的根目錄下

keystore放在根目錄下

在application.yml中添加配置

server:
  port: 443
  ssl:
    key-store: server.keystore
    key-store-password: 生成server.keystore時(shí)輸入的密碼
    key-alias: tomcat
    key-store-type: JKS

在application啟動(dòng)文件中添加配置

package com.cisdi.info.simple;

import org.apache.catalina.connector.Connector;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.domain.EntityScan;
import org.springframework.boot.autoconfigure.security.servlet.SecurityAutoConfiguration;
import org.springframework.boot.web.embedded.tomcat.TomcatServletWebServerFactory;
import org.springframework.boot.web.servlet.server.ServletWebServerFactory;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.context.annotation.Bean;


/**
 *
 * @author CISDI
 * @date 2018/04/27
 */
@SpringBootApplication(scanBasePackages = {"com.cisdi.info.simple.*"}, exclude = {SecurityAutoConfiguration.class})
@EntityScan("com.cisdi.info.simple.*")
@EnableDiscoveryClient(autoRegister = false)
public class Application {

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

    //配置http
    @Bean
    public ServletWebServerFactory servletContainer() {
        TomcatServletWebServerFactory tomcat = new TomcatServletWebServerFactory();
        tomcat.addAdditionalTomcatConnectors(createStandardConnector()); // 添加http
         return tomcat;
    }

    
    private Connector createStandardConnector() {
        Connector connector = new Connector("org.apache.coyote.http11.Http11NioProtocol");
        connector.setPort(8080);
        return connector;
    }

}

完成配置

以上就是springboot 同時(shí)啟用http/https的配置方法的詳細(xì)內(nèi)容,更多關(guān)于springboot啟用http/https的資料請關(guān)注腳本之家其它相關(guān)文章!

相關(guān)文章

最新評論