让我们加密保护的Spring Boot

Spring Boot Secured By Let's Encrypt

在本文中,我们想知道如何(1)免费生成有效证书; (2)使用它配置一个Spring Boot应用; 最后(3)到期后如何续订。

在我以前的博客文章中,我们熟悉了带有自签名证书的Spring Boot Application的配置。 自签名证书可用于特定目的,例如测试和开发。 但是,如果需要将其应用程序交付生产,则应由已知且合法的证书颁发机构(CA)签署证书。

这些类型的证书通常很昂贵。 如果要使用TLS加强应用程序,则需要购买其中之一。 应用服务器的价格和复杂的配置为许多Web应用程序使用安全连接设置了障碍。

在后斯诺登时代,没有人需要说服我们使用HTTPS进行安全连接是必须的。 为了提高开发人员和IT管理员对他们制作的每个网站都采用这种技术的努力,人们付出了很多努力。 但是如何

让我们的Encryptprojects旨在不仅免费将HTTPS引入万维网,而且还采用最简单的配置方式。

在本文中,我们涵盖:

  • 颁发证书和Spring Boot集成

    颁发证书和Spring Boot集成

  • 如何使用Let's Encrypt-生成证书

    如何使用Let's Encrypt-生成证书

    如何从PEM文件生成PKCS#12文件

    Spring Boot应用程序的配置

    续签(即将到期)证书

    <。>

  • <。>
    续签流程

    续签流程

    准备Spring启动

    如何通过加密来生成证书

    让我们加密有一些用于某些应用程序服务器(例如Apache和Nginx)的插件。 在本节中,我们的目标是Spring Boot Application(具有嵌入式Jetty / Tomcat),我们只生成证书,然后与我们的应用程序集成。

    端口80应该是开放的并且可以免费使用,因为我们加密在后台运行一个小型http服务器,以证明您是否控制自己的域名(ACME协议)。

  • You need to fetch the source code of Let's Encrypton your server which your domain address is pointing to.This step may take a couple minutes.

    1
    2
    3
    $ git clone https://github.com/certbot/certbot
    $ cd certbot
    $ ./certbot-auto --help

    1
    2
    3
    $ git clone https://github.com/certbot/certbot
    $ cd certbot
    $ ./certbot-auto --help

    1
    2
    $ ./certbot-auto certonly -a standalone \
         -d seeld.eu -d www.seeld.eu

    如何从PEM文件生成PKCS12文件

    证书和私钥分两步免费生成,显示了"加密"的简单性。 所有这些生成的材料都带有Spring Boot不支持的PEM扩展.Spring-Boot不支持Let's Encrypt生成的PEM文件。 Spring Boot支持PKCS12扩展。 使用OpenSSL,我们将证书和私钥转换为PKCS12。

    要将PEM文件转换为PKCS12版本:

  • Go to /etc/letsencrypt/live/seeld.eu
  • We convert the keys to PKCS12 using OpenSSL in the terminal as follows.

    1
    2
    3
    4
    5
    6
    $ openssl pkcs12 -export -in fullchain.pem \
                     -inkey privkey.pem \
                     -out keystore.p12
                     -name tomcat \
                     -CAfile chain.pem \
                     -caname root

    1
    2
    3
    4
    5
    6
    $ openssl pkcs12 -export -in fullchain.pem \
                     -inkey privkey.pem \
                     -out keystore.p12
                     -name tomcat \
                     -CAfile chain.pem \
                     -caname root

    现在在" /etc/letsencrypt/live/seeld.eu"中生成带有PKCS12的文件" keystore.p12"。

    Spring Boot应用程序的配置

    现在我们要配置我们的Spring Boot应用程序以从证书和私钥中受益; 并最终准备好HTTPS。目前,我们已经生成了证书和私钥。 然后,我们将密钥转换为可用于Spring应用程序的PKCS12扩展名。

  • Open your 'application.properties'
  • Put this configuration there.

    1
    2
    3
    4
    5
    6
    server.port: 8443
    security.require-ssl=true
    server.ssl.key-store:/etc/letsencrypt/live/seeld.eu/keystore.p12
    server.ssl.key-store-password: <your-password>
    server.ssl.keyStoreType: PKCS12
    server.ssl.keyAlias: tomcat

    1
    2
    3
    4
    5
    6
    server.port: 8443
    security.require-ssl=true
    server.ssl.key-store:/etc/letsencrypt/live/seeld.eu/keystore.p12
    server.ssl.key-store-password: <your-password>
    server.ssl.keyStoreType: PKCS12
    server.ssl.keyAlias: tomcat

    如果您访问https://seeld.eu:8443,则可以看到HTTPS已成功配置并且最重要。 为了我们的项目,我们做了一些额外的步骤来使HTTPS与端口80一起使用,您可以使用https://seeld.euURL浏览它。

    Seeld secured by Lets Encrypt

    续签流程

    让我们加密证书仅在90天内有效。 有人可能说3个月与其他提供商提供的证书的有效期相比太短了。 他们做出这一严格决定的动机有两个:(1)限制因密钥泄露或误发布而造成的损失; (2)鼓励自动化。 因此,让我们开始吧!

  • Open your Let's Encrypt client directory, I mean thecertbot. Remarks: On the same machine that certificates and keys are located. Please read all of the remarks from sections, such as having python installed, having port 80 open, etc.
  • Run the renew command as follows.

    1
    $ sudo ./certbot-auto renew

    1
    $ sudo ./certbot-auto renew

    我们拥有新的证书,就这么简单!

    如本节所述:Spring-Boot不支持Let's Encrypt生成的PEM文件。 Spring Boot支持PKCS12扩展。 使用OpenSSL,我们将证书和私钥转换为PKCS12。

    准备Spring启动

    让我们创建一个PKCS#12密钥存储区!

  • Go to /etc/letsencrypt/live/seeld.eu
  • We convert the keys to PKCS12 using OpenSSL in the terminal as follows.

    1
    2
    3
    4
    5
    6
    $ openssl pkcs12 -export -in fullchain.pem \
                     -inkey privkey.pem \
                     -out keystore.p12
                     -name tomcat \
                     -CAfile chain.pem \
                     -caname root

    1
    2
    3
    4
    5
    6
    $ openssl pkcs12 -export -in fullchain.pem \
                     -inkey privkey.pem \
                     -out keystore.p12
                     -name tomcat \
                     -CAfile chain.pem \
                     -caname root

    现在在" /etc/letsencrypt/live/seeld.eu"中生成带有PKCS12的文件" keystore.p12"。

    但是等等!

    我假设您正在唤醒的机器是运行Spring Boot的机器。 这意味着我们还没有完成! 先前的" keystore.p12"仍在内存中,这意味着您需要重新启动应用程序!

    仅重新启动正在运行的应用程序并不总是可行的。 可能有其他方法可以在不重新启动的情况下对其进行更新,但这不在本文讨论范围之内。

    带回家的消息

    在本文中,我们了解了如何颁发,更新Let's Encrypt证书,最重要的是,将其与Spring Boot集成在一起。 如果您确实不需要使用配置,则只需花不到5分钟的时间即可完成所有准备工作。

    对我来说,主要的收获是,无论您管理多少服务,Let's Encrypt都使每个人都(令人难以置信地)更快,更轻松,更便宜地(重新)颁发证书! 您应该尽快开始使用HTTPS。