zoukankan      html  css  js  c++  java
  • 也来聊下Spring Boot开启SSL

    这部分的内容在官方文档中有提到。链接在这儿:Configure SSL

    网络上关于Spring Boot开启SSL访问的文章有很多。希望这篇文章能带来一点不一样的。

    首先,开启SSL访问得有证书,因为是本地访问,那么我们就使用jdk自带的keytool生成一个。

    PS C:Program FilesJavajdk1.8.0_241in> .keytool -genkey -keyalg RSA -keysize 2048 -keystore D:keystore.jks
    输入密钥库口令:
    再次输入新口令:
    您的名字与姓氏是什么?
      [Unknown]:  hanbin
    您的组织单位名称是什么?
      [Unknown]:  home
    您的组织名称是什么?
      [Unknown]:  hanbin-pc
    您所在的城市或区域名称是什么?
      [Unknown]:  Xi'an
    您所在的省/市/自治区名称是什么?
      [Unknown]:  Shaanxi
    该单位的双字母国家/地区代码是什么?
      [Unknown]:  China
    CN=hanbin, OU=home, O=hanbin-pc, L=Xi'an, ST=Shaanxi, C=China是否正确?
      [否]:  y
    
    输入 <mykey> 的密钥口令
            (如果和密钥库口令相同, 按回车):
    
    Warning:
    JKS 密钥库使用专用格式。建议使用 "keytool -importkeystore -srckeystore D:keystore.jks -destkeystore D:keystore.jks -deststoretype pkcs12" 迁移到行业标准格式 PKCS12。

    本文示例项目使用 如何编写自己的Spring Boot的Starter_程序员韩斌的博客-CSDN博客 中的demo application。将D盘下面生成的keystore.jks复制到项目的resources目录下。

    按照官方文档中的示例在application.properties中配置如下:

    启动的时候可以看到如图1所示日志。

    此图像的alt属性为空;文件名为image-5-1024x255.png
    图1

    可以看到,系统启动的时候只启动了8443端口,且是https协议的。

    访问https://localhost:8443/get_name,结果如图2所示。

    此图像的alt属性为空;文件名为image-6.png
    图2

    server.ssl.enabled默认就是true,配置了证书信息后,SSL就算是被激活了。这个时候server.port配置的就不是http的端口了。官方有如下说明:

    Using configuration such as the preceding example means the application no longer supports a plain HTTP connector at port 8080. Spring Boot does not support the configuration of both an HTTP connector and an HTTPS connector through application.properties. If you want to have both, you need to configure one of them programmatically. We recommend using application.properties to configure HTTPS, as the HTTP connector is the easier of the two to configure programmatically.

    如果只配置了上面的信息,就相当于开启了https,禁用了http。

    如果还需要支持http,需要通过代码实现。官方文档在这儿说明了如何开启多个Connector。

    You can add an org.apache.catalina.connector.Connector to the TomcatServletWebServerFactory, which can allow multiple connectors, including HTTP and HTTPS connectors, as shown in the following example:

    上面是官方提供的示例代码,它开启了一个SSL Connector。我们要在默认开启https的基础上至此http访问。应该对上面的代码进行改造。在启动类中添加如下代码:

    在application.properties中添加配置项: server.http.port=8080

    重新启动应用。可以看到日志和刚才不一样了,如图3。

    此图像的alt属性为空;文件名为image-7-1024x268.png
    图3

    通过启动日志可以看到多出了8080的http支持。这个时候使用http://localhost:8080/get_name 访问也能正常拿到结果了。

    最后,说说http转发到https, 直接在代码中添加下面的代码是不会自动跳转的。

    http的redirectPort只有在所使用的接口一定要使用https访问的时候才会跳转。所以还要再添加一些代码,指定默认Https Connector的拦截url。修改前面的servletContainer方法

    接下来访问http://localhost:8080/get_name 会直接跳转到 https://localhost:8443/get_name

  • 相关阅读:
    HandlerThread
    handler原理
    死锁简析
    Android序列化
    AsyncTask原理
    【java线程池】
    java创建线程的三种方式
    service相关
    【hashMap】详谈
    【activity任务栈】浅析
  • 原文地址:https://www.cnblogs.com/hanbin/p/14224671.html
Copyright © 2011-2022 走看看