Spring Boot 支持 HTTPS (步骤配置)

Spring Boot中启动HTTPS

若是你使用Spring Boot,而且想在内嵌tomcat中添加HTTPS,须要有一个证书。java

  • 两种方式算法

    • 本身经过keytool生成spring

    • 经过证书受权机构购买

这里采用第一种方式,采用keytool生成。tomcat

-genkey 生成秘钥
-alias 别名
-keyalg 秘钥算法
-keysize 秘钥长度
-validity 有效期
-keystore 生成秘钥库的存储路径和名称
-keypass 秘钥口令
-storepass 秘钥库口令
-dname 拥有者信息,CN:姓名;OU:组织单位名称;O:组织名称;L:省/市/自治区名称;C:国家/地区代码

  • 第一步
C:\Users\ThinkPad-S3> keytool -genkey -alias https -keyalg RSA -keystore javastack.keystore
输入密钥库口令:
再次输入新口令:
您的名字与姓氏是什么?
  [Unknown]:  test
您的组织单位名称是什么?
  [Unknown]:  test
您的组织名称是什么?
  [Unknown]:  test
您所在的城市或区域名称是什么?
  [Unknown]:  test
您所在的省/市/自治区名称是什么?
  [Unknown]:  test
该单位的双字母国家/地区代码是什么?
  [Unknown]:  test
CN=test, OU=test, O=test, L=test, ST=test, C=test是否正确?
  [否]:  y
  输入 <https> 的密钥口令
        (若是和密钥库口令相同, 按回车):

因此秘钥生成在C:\Users\ThinkPad-S3目录下javastack.keystore这个文件。上面的密码咱们用javastackapp


  • 第二步:application.yml 中添加ssl相关
    server: 
      ssl:
        protocol: TLS
        key-store: classpath:javastack.keystore
        key-store-password: javastack
        key-store-type: JKS

    这里面填写上面的信息便可。并将javastack.keystore 放到resource目录下。maven


  • 第三步:若是出现 Could not load key store 错误,在pom中添加
<build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
        <resources>
        <resource>
            <directory>src/main/resources</directory>
            <filtering>true</filtering>
            <excludes>
                <exclude>*.keystore</exclude>
            </excludes>
        </resource>
        <resource>
            <directory>src/main/resources</directory>
            <filtering>false</filtering>
            <includes>
                <include>*.keystore</include>
            </includes>
        </resource>
    </resources>
    </build>

  • 第四步

验证发现已经支持https了。ide


good luckkkkkkk
相关文章
相关标签/搜索