springboot项目升级为https协议,并且兼容http协议

4 篇文章 0 订阅

1 取得证书

下载证书时选择tomcat方式

2 将证书(pfx)放入项目resource目录下

在这里插入图片描述
需要与application.yml同级
在这里插入图片描述

3 配置application.yml

在这里插入图片描述

server:
  port: 8443
  ssl:
    key-store: classpath:4720629__starhorde.com.pfx
    #密钥库密码
    key-store-password: 下载文件中的密码
    #密钥类型,需要与证书后缀所属类型相符
    keyStoreType: PKCS12
    enabled: true

至此springboot已经升级为https进行访问

4 兼容http访问配置

在这里插入图片描述
在启动类中添加如下配置,将http请求转发到https中

@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(initiateHttpConnector());
        return tomcat;
    }

    @Bean
    public Connector initiateHttpConnector() {
        Connector connector = new Connector("org.apache.coyote.http11.Http11NioProtocol");
        connector.setScheme("http");
        connector.setPort(8087);
        connector.setSecure(false);
        connector.setRedirectPort(8443);
        return connector;
    }

全部配置完成

5 可能出现的问题

  1. 配置后访问,显示连接不安全,点击查看后显示证书无效
    在这里插入图片描述
    原因: 证书绑定了域名,访问的地址需换为域名才可以。 例如(https://www.xxxx.com:8433)
         此时若服务不在域名上,配置一下host映射即可。例如(127.0.0.1     www.xxxx.com)
  2. 启动时报错,出现 Could not load key store
    解决方案: 在pom文件中配置如下代码,清理一下target,执行clean,重新启动
          若还是报错,直接看3
<resource>
       <directory>src/main/resources</directory>
       <includes>
            <include>**/*.pfx</include>
       </includes>
       <filtering>false</filtering>
</resource>
  1. 启动时报错。java.lang.IllegalArgumentException: DerInputStream.getLength(): lengthTag=111, too big.
    在这里插入图片描述
    原因: 证书文件放在项目的src/main/resources路径下由于使用了
       maven插件maven-resources-plugin
       该插件会修改管理目录下的资源文件,造成证书文件发生变更
    解决方案: 修改插件配置,设置证书文件例外
    在这里插入图片描述
<plugin>
     <groupId>org.apache.maven.plugins</groupId>
     <artifactId>maven-resources-plugin</artifactId>
     <configuration>
           <encoding>UTF-8</encoding>
                <!-- 过滤后缀为pem、pfx的证书文件 -->
                <nonFilteredFileExtensions>
                      <nonFilteredFileExtension>pem</nonFilteredFileExtension>
                      <nonFilteredFileExtension>pfx</nonFilteredFileExtension>
                	  <nonFilteredFileExtension>p12</nonFilteredFileExtension>
                </nonFilteredFileExtensions>
     </configuration>
</plugin>

6 证书类型与后缀辨别

证书主要的文件类型和协议有: PEM、DER、PFX、JKS、KDB、CER、KEY、CSR、CRT、CRL 、OCSP、SCEP等。

PKCS#7常用的后缀是: .P7B .P7C .SPC
PKCS#12常用的后缀有: .P12 .PFX
X.509 DER编码(ASCII)的后缀是: .DER .CER .CRT
X.509 PAM编码(Base64)的后缀是: .PEM .CER .CRT
.cer/.crt是用于存放证书,它是2进制形式存放的,不含私钥。
.pem跟crt/cer的区别是它以Ascii来表示。
pfx/p12用于存放个人证书/私钥,他通常包含保护密码,2进制方式
p10是证书请求
p7r是CA对证书请求的回复,只用于导入
p7b以树状展示证书链(certificate chain),同时也支持单个证书,不含私钥。

介绍连接

7 证书转换方法

转换 pem证书为 pkcs12证书

例如:openssl pkcs12 -export -in fullchain.pem -inkey privkey.pem -out springboot.p12

openssl pkcs12 -export -in [公钥] -inkey [私钥] -out [pkcs12证书文件]
  • 5
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

ZHAIKEsir

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值