全民https时代,Let's Encrypt免费SSL证书的申请及使用(Tomcat版)

本文转载自

https://blog.csdn.net/lyq8479/article/details/79022888

近几年,在浏览器厂商的强力推动下,HTTPS的使用率大增。据统计,Firefox加载的网页中启用HTTPS的占比为67%,谷歌搜索结果中HTTPS站点占比已达50%,HTTPS网站已获得浏览器和搜索引擎的共同青睐。据悉,浏览器开发商Mozilla,Google准备采取下一步措施:将所有的HTTP网站标记为不安全。


近两年,微信小程序也异常火爆,小程序相关话题频频在朋友圈刷屏。如果你想开发微信小程序,一样绕不开HTTPS。根据微信小程序开发文档介绍,小程序与服务器交互必须使用HTTPS协议。


不管是大势所趋,还是实际项目开发需要,掌握如何给网站添加HTTPS协议支持(Web服务器设置使用SSL证书后,网站就能支持HTTPS访问了)都是非常有必要的。


一般来说,很多公司都会直接购买由GlobalSignGeoTrustVerisign等全球公认的数字证书颁发机构颁发的SSL证书。购买?没错,大多数SSL证书都需要按年付费使用,而且价格不菲。很多小公司、创业团队或个人开发者往往不愿意承担这笔费用,那有没有免费的SSL证书可以使用?


Let's Encrypt 是一个免费、开放,自动化的证书颁发机构,由 ISRG(Internet Security Research Group)运作。ISRG 是一个关注网络安全的公益组织,其赞助商包括 Mozilla、Akamai、Cisco、EFF、Chrome、IdenTrust、Facebook等公司。ISRG 的目的是消除资金和技术领域的障碍,全面推进网站从HTTP到HTTPS过度的进程。


目前,包括FireFox、Chrome在内的主流浏览器都支持Let's Encrypt证书,已经有不少用户在真实项目中使用Let's Encrypt证书。Let's Encrypt免费SSL证书的有效期是90天,到期后可以再续期,这样也就可以变相长期使用了。 


官方网站:https://letsencrypt.org/

项目主页:https://github.com/letsencrypt/letsencrypt


实验环境

  1. Ubuntu 14 .04 .2 LTS
  2. Python 2 .7 .6
  3. tomcat-7 .0 .68

PS:所有不说明实验环境的技术文章都是耍流氓!

笔者使用的是一台 Ubuntu 测试机,上面没有跑 Apache 和 Nginx,单独运行的 Tomcat,然后通过 iptables 进行端口转发(将 80 端口的请求转发到 8080,将 443 端口的请求转发到 8443),并且已完成域名解析。


获取Let's Encrypt免费SSL证书

首先,我们需要从 https://github.com/letsencrypt/letsencrypt 上下载 letsencrypt-auto 脚本,使用它能够很方便地完成域名验证和SSL证书的获取。

  1. # 使用git下载脚本
  2. $ git clone https://github.com/letsencrypt/letsencrypt
  3. # 进入到脚本所在目录
  4. $ cd letsencrypt
  5. # 查看 letsencrypt-auto 工具的用法
  6. $ ./letsencrypt-auto -- help
脚本下载好了,来看看怎么使用吧。letsencrypt-auto工具的用法如下:
  1. root@iZ28425ljejZ:~/letsencrypt # ./letsencrypt-auto --help
  2. -------------------------------------------------------------------------------
  3. letsencrypt- auto [SUBCOMMAND] [options] [-d DOMAIN] [-d DOMAIN] ...
  4. Certbot can obtain and install HTTPS/TLS/SSL certificates. By default,
  5. it will attempt to use a webserver both for obtaining and installing the
  6. certificate. The most common SUBCOMMANDS and flags are:
  7. obtain, install, and renew certificates:
  8. ( default) run Obtain & install a certificate in your current webserver
  9. certonly Obtain or renew a certificate, but do not install it
  10. renew Renew all previously obtained certificates that are near expiry
  11. -d DOMAINS Comma-separated list of domains to obtain a certificate for
  12. --apache Use the Apache plugin for authentication & installation
  13. --standalone Run a standalone webserver for authentication
  14. --nginx Use the Nginx plugin for authentication & installation
  15. --webroot Place files in a server 's webroot folder for authentication
  16. --manual Obtain certificates interactively, or using shell script hooks
  17. -n Run non-interactively
  18. --test-cert Obtain a test certificate from a staging server
  19. --dry-run Test "renew" or "certonly" without saving any certificates to disk
  20. manage certificates:
  21. certificates Display information about certificates you have from Certbot
  22. revoke Revoke a certificate (supply --cert-path)
  23. delete Delete a certificate
  24. manage your account with Let 's Encrypt:
  25. register Create a Let 's Encrypt ACME account
  26. --agree-tos Agree to the ACME server 's Subscriber Agreement
  27. -m EMAIL Email address for important account notifications
  28. More detailed help:
  29. -h, --help [TOPIC] print this message, or detailed help on a topic;
  30. the available TOPICS are:
  31. all, automation, commands, paths, security, testing, or any of the
  32. subcommands or plugins (certonly, renew, install, register, nginx,
  33. apache, standalone, webroot, etc.)
  34. -------------------------------------------------------------------------------
这里只对几个重要的命令参数进行说明:
  1. run:获取并安装证书到当前的Web服务器
  2. certonly:获取或续期证书,但是不安装
  3. renew:在证书快过期时,续期之前获取的所有证书
  4. -d DOMAINS:一个证书支持多个域名,用逗号分隔
  5. --apache:使用 Apache 插件来认证和安装证书
  6. --standalone:运行独立的 web server 来验证
  7. --nginx:使用 Nginx 插件来认证和安装证书
  8. --webroot:如果目标服务器已经有 web server 运行且不能关闭,可以通过往服务器的网站根目录放置文件的方式来验证
  9. --manual:通过交互式方式,或 Shell 脚本手动获取证书
关于域名验证和证书的获取安装,上面提到了5种方式:--apache, --standalone, --nginx, --webroot 和 --manual,请根据实际情况选择其一。再次重申,笔者使用的是公司的其中一台测试机,上面没有跑 Apache 和 Nginx,单独运行的 Tomcat,然后通过 iptables 进行端口转发(将 80 端口的请求转发到 8080,将 443 端口的请求转发到 8443),并且已完成域名解析。因此,笔者选择采用 --standalone 方式进行域名验证和证书 获取
  1. # 获取证书
  2. $ ./letsencrypt-auto certonly --standalone --email 58472399@qq.com -d quqianzhao.top -d www.quqianzhao.top

注意将上面的邮箱和域名替换成自己的。上面命令中的 certonly 表示只获取证书,不安装;-d 有两个,表示将要获取的SSL证书绑定两个域名。


上面的命令在执行过程中,会有两次确认。命令执行完成后,如果看到提示信息"Congratulations! Your certificate and chain..."就说明证书创建成功了,如下图所示。


Tomcat7配置Let's Encrypt免费SSL证书

从上图(证书获取成功的提示信息)可以看出,获取到的 Let's Encrypt 证书保存在 /etc/letsencrypt/live/quqianzhao.top/ 目录下,相关的证书文件有4个:

  1. cert .pem
  2. chain .pem
  3. fullchain .pem
  4. privkey .pem
我们需要用到的是后面两个证书文件: fullchain.pem 和 privkey.pem。接下来,还需要用到 Linux 下的 openssl 和 keytool 工具,将SSL证书由 .pem 格式转换成 Tomcat 所支持的 .jks 格式。

  1. # 导出.p12格式的证书
  2. $ openssl pkcs12 -export - in fullchain.pem -inkey privkey.pem -out zyxx_letsencrypt.p12 -name tomcat_letsencrypt
  3. # 再将证书由.p12格式转换成.jks格式
  4. $ keytool -importkeystore -deststorepass 'zxxx_123' -destkeypass 'zxxx_123' -destkeystore zyxx_letsencrypt.jks -srckeystore zyxx_letsencrypt.p12 -srcstoretype PKCS12 -srcstorepass 'zxxx_123' - alias tomcat_letsencrypt
说明:执行 openssl 命令导出 .p12 格式证书时会要求设置密钥,执行 keytool 命令时也有3处要写密钥,最简单的方式就是所有需要密钥的地方,都使用同一个,这样也不会搞混。


经过上面的两步操作,/etc/letsencrypt/live/quqianzhao.top/ 目录下就生成了一个新的证书 zyxx_letsencrypt.jks。接下来,修改 %tomcat%/conf/server.xml 文件,添加 keystoreFile 和 keystorePass 两行配置。其中,keystoreFile 指向 jks 证书文件,而 keystorePass 则为证书的密钥。修改后的关键配置如下:

  1. <Connector port="8443" protocol="org.apache.coyote.http11.Http11Protocol" SSLEnabled="true"
  2. maxThreads = "150" scheme = "https" secure = "true"
  3. clientAuth = "false" sslProtocol = "TLS"
  4. keystoreFile = "/home/test/zyxx_letsencrypt.jks"
  5. keystorePass = "zxxx_123"
  6. />
OK,到这里 Tomcat 的配置工作就完成了。接下来,重启 Tomcat,然后使用浏览器访问 https://www.quqianzhao.top 来验证服务器证书是否生效。

查看服务器证书信息:

        


Let's Encrypt 证书续期

出于安全原因,Let's Encrypt 颁发的 SSL 证书有效期为90天,我们可以通过自动续期来解决。如果到期没有更新证书,Let's Encrypt 会向申请证书时提交的email发送提醒邮件。

进入到 letsencrypt-auto 脚本所在目录,执行下面的命令即可完成 SSL 证书的续期。

./letsencrypt-auto renew

默认情况下,在证书即将到期之前才能执行续期操作,否则会提示“Cert not yet due for renewal”,即证书尚未到期。如果需要强制执行续期操作,可以加上参数 --force-renew ,命令如下:

./letsencrypt-auto renew --force-renew

以下是笔者强制执行证书续期的操作结果:

  1. root@iZ28425ljejZ:~/letsencrypt# ./letsencrypt-auto renew --force-renew
  2. Saving debug log to /var/ log/letsencrypt/letsencrypt. log
  3. -------------------------------------------------------------------------------
  4. Processing /etc/letsencrypt/renewal/quqianzhao.top.conf
  5. -------------------------------------------------------------------------------
  6. Plugins selected: Authenticator standalone, Installer None
  7. Renewing an existing certificate
  8. Performing the following challenges:
  9. tls-sni -01 challenge for quqianzhao.top
  10. tls-sni -01 challenge for www.quqianzhao.top
  11. Waiting for verification...
  12. Cleaning up challenges
  13. -------------------------------------------------------------------------------
  14. new certificate deployed without reload, fullchain is
  15. /etc/letsencrypt/live/quqianzhao.top/fullchain.pem
  16. -------------------------------------------------------------------------------
  17. -------------------------------------------------------------------------------
  18. Congratulations, all renewals succeeded. The following certs have been renewed:
  19. /etc/letsencrypt/live/quqianzhao.top/fullchain.pem (success)
  20. -------------------------------------------------------------------------------

考虑到有可能会忘记续期,或者每3个月就要续期一次太麻烦,可以使用 linux crontab 每两个月(0 0 1 */2 *)执行一次证书续期操作


猜你喜欢

转载自blog.csdn.net/lizhengyu891231/article/details/81050099